FeistelCipher.Migration (feistel_cipher v0.1.0)

View Source

Migrations create functions FeistelCipher needs to function.

Usage

To use migrations in your application you'll need to generate an Ecto.Migration that wraps calls to FeistelCipher.Migration:

mix ecto.gen.migration add_feistel_cipher

Open the generated migration in your editor and call the up and down functions on FeistelCipher.Migration:

defmodule MyApp.Repo.Migrations.AddFeistelCipher do
  use Ecto.Migration

  def up, do: FeistelCipher.Migration.up()

  def down, do: FeistelCipher.Migration.down()
end

This will run all of FeistelCipher's versioned migrations for your database.

Now, run the migration to create the table:

mix ecto.migrate

Isolation with Prefixes

FeistelCipher supports namespacing through PostgreSQL schemas, also called "prefixes" in Ecto. With prefixes your jobs table can reside outside of your primary schema (usually public) and you can have multiple separate job tables.

To use a prefix you first have to specify it within your migration:

defmodule MyApp.Repo.Migrations.AddPrefixedFeistelIdJobsTable do
  use Ecto.Migration

  def up, do: FeistelCipher.Migration.up(prefix: "private")

  def down, do: FeistelCipher.Migration.down(prefix: "private")
end

In some cases, for example if your "private" schema already exists and your database user in production doesn't have permissions to create a new schema, trying to create the schema from the migration will result in an error. In such situations, it may be useful to inhibit the creation of the "private" schema:

defmodule MyApp.Repo.Migrations.AddPrefixedFeistelIdJobsTable do
  use Ecto.Migration

  def up, do: FeistelCipher.Migration.up(prefix: "private", create_schema: false)

  def down, do: FeistelCipher.Migration.down(prefix: "private")
end

Migrating Without Ecto

If your application uses something other than Ecto for migrations, be it an external system or another ORM, it may be helpful to create plain SQL migrations for FeistelCipher database schema changes.

The simplest mechanism for obtaining the SQL changes is to create the migration locally and run mix ecto.migrate --log-migrations-sql. That will log all of the generated SQL, which you can then paste into your migration system of choice.

Alternatively, if you'd like a more automated approach, try using the feistel_id_migations_sql project to generate up and down SQL migrations for you.

Summary

Functions

Run the down changes.

Returns the SQL for dropping a trigger for a table to encrypt a source field to a target field.

Run the up changes.

Returns the SQL for creating a trigger for a table to encrypt a source field to a target field.

Functions

down(opts \\ [])

Run the down changes.

Example

Run migrations in an alternate prefix:

FeistelCipher.Migration.down(prefix: "payments")

down_sql_for_table(table, opts \\ [])

Returns the SQL for dropping a trigger for a table to encrypt a source field to a target field.

Example

FeistelCipher.Migration.down_sql_for_table("posts", source: "seq", target: "id")

up(opts \\ [])

Run the up changes.

Example

Run migrations in an alternate prefix:

FeistelCipher.Migration.up(prefix: "payments")

up_sql_for_table(table, opts \\ [])

Returns the SQL for creating a trigger for a table to encrypt a source field to a target field.

Example

FeistelCipher.Migration.up_sql_for_table("posts", source: "seq", target: "id")