PhoenixMountable.Migration behaviour (PhoenixMountable v0.2.1)

Copy Markdown View Source

One version of a mountable library's schema.

defmodule MyLib.Migrations.V4 do
  use PhoenixMountable.Migration,
    safety: :expand,
    description: "Archive items"

  def up(ctx) do
    alter table(:my_lib_items, prefix: ctx.prefix) do
      add :archived_at, :utc_datetime_usec
    end

    create_concurrently index(:my_lib_items, [:archived_at], prefix: ctx.prefix)
  end

  def down(ctx) do
    drop_if_exists index(:my_lib_items, [:archived_at], prefix: ctx.prefix)

    alter table(:my_lib_items, prefix: ctx.prefix) do
      remove :archived_at
    end
  end
end

The Ecto.Migration DSL is imported. up/1 and down/1 receive a PhoenixMountable.Migrations.Context with the repo, the adapter (:postgres, :mysql or :sqlite), the prefix, and the install options recorded when the library was first installed. Pattern match on the adapter for SQL that differs:

def up(%{adapter: :sqlite} = ctx), do: ...
def up(ctx), do: ...

Options

  • :safety — :expand (the default) for additive changes that old code can run against: new tables, nullable columns, new indexes. :contract for changes that break code still reading the old schema: dropping or renaming tables and columns, changing types. A compile-time lint warns about destructive operations in an :expand version.
  • :description — a line about the change, written into the host's migration by the generator.
  • :requires_backfills — backfill modules that must have finished before this version runs (a contract version that drops the column a backfill copied from).
  • :irreversible — a reason why down/1 cannot be written. The generator shows it, and rolling back past this version raises.
  • :no_transaction — adapters on which this version cannot run inside a transaction, up or down. Detected automatically for create_concurrently/1, drop_concurrently/1 (Postgres) and rebuild_table/3 (SQLite).
  • :baseline — marks the module as the library's baseline: the whole schema at the given version, for fresh installs.

Summary

Callbacks

down(t)

(optional)

up(t)