PhoenixMountable.Migrations.Test (PhoenixMountable v0.2.1)

Copy Markdown View Source

Checks for a library's migrations, run against a real database in the library's own test suite.

test "migrations", %{repo: repo} do
  PhoenixMountable.Migrations.Test.run_suite(repo, MyLib.Migrations, prefix: "my_lib_test")
end

run_suite/3 runs:

  • assert_round_trip/3 — every version goes up, down and up again, and down gives back exactly the previous schema;
  • assert_baseline_matches/3 — when the library has a baseline, a fresh install through it gives the same schema as running every version;
  • assert_upgrades/3 — an install at each released version upgrades to the latest and ends with the same schema as a fresh install;
  • assert_preserves_host_changes/3 — the "hostile host": after each version, a column and an index are added to every library table, as a host might, and they must survive every later version.

and, with idempotent: true (recommended for MySQL, whose DDL is not transactional), assert_rerunnable/3: each version can run again after failing halfway.

Each check starts from an empty schema: a Postgres schema or MySQL database named by :prefix (dropped and recreated), or an emptied SQLite database. Use a database and prefix reserved for these tests.

Options

  • :prefix — the prefix to use. Defaults to "phoenix_mountable_test" (ignored on SQLite).
  • :install — install options for up/1, such as primary_key: :binary_id.
  • :seed — fn version, ctx -> ... end, run by assert_upgrades/3 after installing at version, to insert data.
  • :verify — fn version, ctx -> ... end, run by assert_upgrades/3 after upgrading from version, to check that data.

Summary

Functions

A fresh install through the baseline matches running every version.

After each version, a host adds a column and an index to every library table; they must survive every later version.

Each version runs again after failing halfway: its tracking is reset to the previous version and it runs a second time.

Every version goes up, down and up again, with matching schemas.

An install at each version upgrades to the latest schema.

Runs migrations.down(opts) inside a real Ecto migration.

Empties the prefix (or the SQLite database).

Runs fun as the up/0 of a one-off Ecto migration.

Runs every check. See the module documentation.

The comparable schema of the prefix, without tracking tables.

Runs migrations.up(opts) inside a real Ecto migration, outside a transaction when the versions need it.

Writes the schema of a fresh install at the latest version to the library's priv/phoenix_mountable, for verify/1 at runtime.

Functions

assert_baseline_matches(repo, migrations, opts \\ [])

A fresh install through the baseline matches running every version.

assert_preserves_host_changes(repo, migrations, opts \\ [])

After each version, a host adds a column and an index to every library table; they must survive every later version.

assert_rerunnable(repo, migrations, opts \\ [])

Each version runs again after failing halfway: its tracking is reset to the previous version and it runs a second time.

assert_round_trip(repo, migrations, opts \\ [])

Every version goes up, down and up again, with matching schemas.

assert_upgrades(repo, migrations, opts \\ [])

An install at each version upgrades to the latest schema.

down(repo, migrations, opts \\ [])

Runs migrations.down(opts) inside a real Ecto migration.

reset!(repo, opts \\ [])

Empties the prefix (or the SQLite database).

run(repo, fun, opts \\ [])

Runs fun as the up/0 of a one-off Ecto migration.

run_suite(repo, migrations, opts \\ [])

Runs every check. See the module documentation.

schema(repo, opts \\ [])

The comparable schema of the prefix, without tracking tables.

up(repo, migrations, opts \\ [])

Runs migrations.up(opts) inside a real Ecto migration, outside a transaction when the versions need it.

write_snapshot!(repo, migrations, opts \\ [])

Writes the schema of a fresh install at the latest version to the library's priv/phoenix_mountable, for verify/1 at runtime.