Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

mitch / docs / guide / changing-applied-migrations.md · 5.0 KB · at 76a2c3d332a1 · raw

Changing applied migrations

Once a migration is applied somewhere, changing its files has consequences for every deployment that already ran it. Mitch offers several tools for this, from simply redeploying during development to reworks that keep the history of every deployment reproducible.

Modified migrations

For every applied migration, mitch stores two hashes of the up script: one of the raw file and one of the SQL after normalizing whitespace, comments and keyword case. A migration counts as modified only when both differ. Reformatting or commenting a script is therefore not a change, while editing its SQL is.

A migration also counts as modified when its dependencies changed, for example when a dependency was added after it was applied.

mitch status marks modified migrations with M. mitch up and mitch add decide what to do with them via --if-modified/-m:

Value Behaviour
prompt Ask for each modified migration: revert and redeploy, ignore, or exit. Default in an interactive terminal.
redeploy Revert the applied version with its stored down script, together with its dependants, and apply the new one. Default with --watch.
ignore Leave modified migrations as they are.
abort Stop with an error. Default when mitch runs unattended, for example in CI.

Redeploying is fine on a development database. On shared deployments, a modified migration usually means the history is about to diverge: prefer a new migration or a rework.

--late-dependencies treats a migration whose only change is a new dependency as applied, and applies the new dependency afterwards instead of redeploying. Mitch warns that the order then deviates from the graph.

Watch mode

mitch up --watch keeps running and redeploys migrations of the own project whenever their files change. It implies --if-modified redeploy. Changes in linked projects are not watched.

Reworking a migration

Some migrations keep evolving, for example a view or a function. mitch rework <id> creates a new version of such a migration:

  • The current files are archived next to it as <name>@1.* under the original ID, so deployments that applied it still match.
  • The files at the original path become the new version <id>@2. It depends on the archived version and is ordered after the migrations that built on it. Its down script is prefilled with the archived up script, so reverting it restores the previous version.
  • Edit the up script and review the change with your version-control system's diff.

Every deployment, old or fresh, runs the archived version first and the new one on top of it. The next mitch rework <id> archives @2 and creates @3. mitch status and mitch log group the versions of a migration.

Editing an applied migration without rework still counts as modified. For idempotent migrations, mitch status suggests rework.

Why mitch keeps every version instead of letting fresh deployments run only the newest: ADR cli/rework/rework-keeps-the-previous-version-in-every-history.

Rebuilding dependent objects

Some changes are impossible while objects depend on them, such as a view losing a column that a materialized view or a policy uses. A migration can request a rebuild:

rebuild_dependants_of = ["order-summary"]

When this migration is applied, mitch reverts the applied migrations that build on order-summary, applies this migration, and re-applies them with their current up scripts, all in one transaction. mitch rework prepares this line as a comment in the new version's config.

Mitch refuses a rebuild when a dependant has no down script, belongs to a project that is not loaded, or is non-transactional. --allow-autocommit allows non-transactional participants; if the rebuild then fails halfway, mitch prints the mitch add command that completes it once the cause is fixed.

Rebuilds happen in mitch up, mitch add and mitch checkout --and-up. Other paths that apply or revert migrations, such as checkout without --and-up and stash, do not rebuild yet; see the roadmap.

If a rebuild fails with a database error about dependent objects, a dependency is probably not declared in mitch. Declaring it fixes the rebuild and improves the graph.

Ordering without requiring

after = ["seed-data"]

after runs a migration after seed-data whenever seed-data is applied on the deployment or part of the same run, without pulling seed-data onto the deployment. It separates "must exist first" (dependencies) from "if it exists, go first", like Requires= and After= in systemd. Reworks use it to run the new version after the optional dependants of the previous one.

If seed-data arrives later than the migration that declared the constraint, mitch applies it anyway and warns. --restore-order reverts the later migration and re-applies it around the newcomer instead, under the same rules as a rebuild.