Versioned migrations for this package's tables, in the shape
statifier_persistence's StatifierPersistence.Ecto.Migrations uses: the
host writes one ordinary migration that delegates here, and later
package versions ship higher-numbered migration modules the same call
picks up.
defmodule MyApp.Repo.Migrations.AddStatifierRouter do
use Ecto.Migration
def up, do: StatifierRouter.Migrations.up(prefix: "routing")
def down, do: StatifierRouter.Migrations.down(prefix: "routing")
endThe options are the two storage options of StatifierRouter.Config,
:table_prefix and :prefix, resolved the same way, :from and
:version, and the three layout options statifier_persistence's
migrations helper takes, under the same spellings:
:table_prefix- a string prefixed to every table name, default"statifier_router_". Pass the same value the host'sStatifierRouter.Configcarries.:prefix- the Postgres schema the tables live in, defaultnil. When it is set,up/1creates the schema if it does not exist anddown/1leaves it in place: dropping a schema the host may share is not this package's call.:fromand:version- where a call starts and where it ends, in both directions.up/1migrates fromfrom:(default: V01) up throughversion:(default: the newest this package knows), anddown/1rolls back fromfrom:(default: the newest) down throughversion:(default: V01, that is, everything).:leading_columns- host-owned columns placed immediately afteridin every table a version creates, in the order given, default[]. A keyword list ofname: {type, opts}, wheretypeandoptsare whatEcto.Migration.add/3takes:leading_columns: [branch_id: {:text, null: true}]puts a nullablebranch_idat ordinal position 2 on all four tables. The package's schemas do not declare the column, so the package never reads or writes it; a default or aNOT NULLbelongs to a later migration of the host's own. A name a table the call creates already declares - any columnStatifierRouter.Migrations.V01orStatifierRouter.Migrations.V02lists for it - raisesArgumentErrornaming the column and those tables, before any DDL runs, where Postgres would otherwise refuse theCREATE TABLEwith a duplicate column. A name only a table the call does not create declares is a host column like any other:up(from: 2)may lead withexpires_at, which only V01's dedupe table has. The primary key is the repo's:migration_primary_keyand is not checked: a repo that sets it tofalsemay lead with anidof its own.:timestamps_position- whereinserted_atgoes in every table a version creates that has one (the address table, the routing ledger and the subscription table; the dedupe table has none)::trailing(default: the layoutStatifierRouter.Migrations.V01andStatifierRouter.Migrations.V02document) or:leading(immediately afteridand the:leading_columns). The address table'sterminal_seen_atis not a timestamp column in this sense and stays where it is.:column_collations- a collation per package text column, applied wherever a version declares that column in aCREATE TABLE, default[](every column takes the database default). A keyword list ofname: collation, the collation a non-empty string:column_collations: [execution_id: "C"]declaresexecution_idCOLLATE "C"on the address table, the routing ledger and the subscription table. The names are the text columns the versions declare -scope,document,key,execution_id,binding_id,message_id,outcome,reasonandinvoke_id- and the collation must be one the database knows; a host column takes its collation in its own:leading_columnsopts instead.
The three layout options apply to a fresh create only. Each table is
laid out by the version that creates it - V01 the address table, the
dedupe table and the routing ledger, V02 the subscription table - and
no version re-places a column in a table that already exists, so
adding an option later changes nothing in the tables already built.
Left out, every version builds exactly the tables it built before the
options existed. down/1 accepts them too, so one options list serves
both directions, and ignores them.
A host already running an older version writes its next migration with
from: set to the first version it has not run, rather than re-running
V01's CREATE TABLE against tables that already exist. A host whose
first migration is capped with version: N caps its rollback to match
with from: N.
A fault in the options raises ArgumentError: a migration has no caller
to hand an {:error, reason} to.
StatifierRouter.Migrations.V01 records what the first version creates,
and StatifierRouter.Migrations.V02 what the second adds.
:from is inclusive: up(from: 2) runs V02, and a host already on
V01 that writes it gets the subscription table without V01's
CREATE TABLE running a second time (ADR-0007, section 6).
Summary
Functions
Rolls the tables back from from: (default: the newest version this
package knows) down through version: (default: V01, that is,
everything).
Migrates the tables from from: (default: V01) up through version:
(default: the newest).