Public repository: timo / mitch

Reader

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

mitch / docs / guide / commands.md · 5.8 KB · at 76a2c3d332a1 · raw

Command reference

mitch <command> --help prints the options of each command. mitch --install-completion installs shell completion.

Common options

Option Meaning
--project, -p <dir> Project directory. Default: the current directory.
--database, -d <url> Database as a SQLAlchemy URL. Default: MITCH_DATABASE_URL, then DATABASE_URL.

Every command that talks to the database accepts both. init, create and rework only touch files. They take --project but no --database, and init always works in the current directory.

Migrations are named by ID. Migrations of a linked project are named by qualified ID alias:migration (see Linked projects).

Setting up

mitch init <project-id>

Creates mitch.toml with the given project ID in the current directory. Refuses if the file exists.

mitch create <id>

Creates <id>.up.sql, <id>.down.sql and <id>.config.toml in the project. SQL on stdin fills the scripts; the comment lines -- up.sql and -- down.sql separate them.

Option Meaning
--dependency, -d <id> Dependency of the new migration. Repeatable. The ID must exist.
--skip-config, -sc Do not create the config file.
--skip-downscript, -sd Do not create the down script.

Note that -d means --dependency here, not --database.

mitch rework <id>

Archives the current version of a migration and turns the files at its path into a new version. See Reworking a migration.

Applying

mitch up

Applies all pending migrations of the project, plus the linked migrations they require, in dependency order.

mitch add <id>...

Applies the given migrations of the own project and their dependencies.

Options of up and add

Option Meaning
--if-modified, -m <policy> What to do with modified migrations: prompt, redeploy, ignore or abort.
--watch Keep running and redeploy on file changes. Implies --if-modified redeploy.
--late-dependencies Apply dependencies that were added to applied migrations, instead of treating those migrations as modified.
--restore-order When a migration arrives after migrations ordered after it, revert and re-apply those instead of warning.
--allow-autocommit Allow rebuilds and order restoration when non-transactional migrations take part, without the safety of one transaction.

Reverting

mitch drop <id>...

Reverts the given migrations and every applied migration that depends on them, dependants first. Accepts qualified IDs. Linked migrations that nothing requires any more are reverted too.

mitch down

Reverts all applied migrations of the project, and the linked migrations that were pulled in for them.

A revert is refused while an applied migration of a project that is not loaded depends on it, and when a migration has no down script.

Inspecting

mitch status [<id>...]

Compares the project with the database and prints one line per migration:

Mark Meaning
A Available: not applied yet.
M Modified since it was applied.
R Applied, but its files are gone from the project.
- With --all: a migration of a directly linked project that nothing requires.

With IDs, only those migrations are reported.

In CI mode, status fails with an exit code:

Exit code Cause
0 Nothing to report.
2 Applied migrations are missing.
3 Migrations are modified (only with --strict).
4 Both of the above (only with --strict).
Option Meaning
--ci / --no-ci Enable or disable CI mode. Default: enabled when one of CI, GITHUB_ACTIONS, GITLAB_CI, JENKINS_URL, BUILDKITE, TF_BUILD or CIRCLECI is set.
--strict Count modified migrations as failures in CI mode.
--all Also list unrequired migrations of directly linked projects.

mitch log

Lists the applied migrations with the time they were applied and their status on disk (applied, modified or missing).

Working with branches

Both commands support git and Mercurial and detect the system from the project root. --vcs git or --vcs hg picks one explicitly. Jujutsu is not supported yet: in a colocated jj repository, mitch would drive git directly (#3).

mitch checkout <branch>

Switches to another branch or revision and keeps the database in step:

  1. Reverts the migrations that are missing or different on the target, together with their dependants.
  2. Checks out the target.
  3. Re-applies the reverted migrations from the files of the target, where they still exist.

Migrations that are new on the target are not applied unless --and-up is given.

Option Meaning
--allow-dirty Allow uncommitted changes: revert what they applied, stash them around the checkout, and restore them afterwards. Without it, a dirty working copy is an error.
--and-up Apply all pending migrations after the checkout, like mitch up.

Linked projects are not reverted or applied by checkout yet. Mitch warns when a linked project differs on the target.

Details: ADR cli/checkout/restore-applied-migrations-but-do-not-up.

mitch stash

Reverts the migrations whose local files differ from the current commit, stashes the uncommitted changes, and re-applies the committed versions. Afterwards, the database matches the current commit again.