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
endThe 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.:contractfor 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:expandversion.: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 whydown/1cannot 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 forcreate_concurrently/1,drop_concurrently/1(Postgres) andrebuild_table/3(SQLite).:baseline— marks the module as the library's baseline: the whole schema at the given version, for fresh installs.
Summary
Callbacks
@callback down(PhoenixMountable.Migrations.Context.t()) :: any()
@callback up(PhoenixMountable.Migrations.Context.t()) :: any()