Public repository: timo / mitch

Reader

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

docs: README for newcomers, guide, home on ourforge public

Timo Stolz committed

76a2c3d332a1
NameSizeLast change
Directory .vscode
Directory docs
Directory src
Directory tests
.gitignore 151 bytes
.pylintrc 460 bytes
AGENTS.md 1.0 KB
CONTEXT.md 5.5 KB
LICENSE 11.1 KB
NOTICE 111 bytes
README.md 6.9 KB
pyproject.toml 1.3 KB
uv.lock 64.2 KB

README.md

mitch

A migration stitcher for PostgreSQL, SQLite, MariaDB and other databases.

Most migration tools keep a single, linear list of migrations per codebase. Mitch treats migrations as a graph: each migration names the migrations it depends on, and mitch works out the order. Several codebases can then share a subset of the same migrations, for example user management with or without groups, instead of each keeping its own sequence.

Mitch is plain SQL: a migration is an up script, an optional down script and an optional TOML file with metadata. No ORM models, no Python migration code.

Status: early development (0.1.0). The command line and the bookkeeping tables may still change.

Features

  • Dependency graph instead of a sequence. Migrations declare dependencies. mitch up applies everything pending in dependency order, mitch drop reverts a migration together with everything that builds on it.
  • Shared projects. A project can use migrations from other projects, nested in its directory or checked out next to it (for example as git submodules). Only the linked migrations it actually needs are applied.
  • Several projects per database. Bookkeeping is scoped by a globally unique project ID, so several codebases can deploy to the same database.
  • Modified-migration detection. Mitch stores a raw and a normalized hash of every applied up script. Reformatting or commenting a script is not a change; editing its SQL is. mitch up then asks whether to revert and redeploy it, or aborts when it runs unattended.
  • Reworks. mitch rework turns an evolving migration (a view, a function) into a new version that every deployment, old or fresh, applies in the same order.
  • Rebuilds. A migration can ask mitch to revert its applied dependants, apply itself and re-apply them in one transaction, for changes that are impossible while other objects depend on it.
  • Ordering without requiring. after runs a migration after others when they are present, without pulling them onto the deployment.
  • Branch-aware checkout. mitch checkout switches a git or Mercurial branch and reverts and re-applies migrations so the database matches the branch you land on. mitch stash does the same for uncommitted changes.
  • Transactions. Migrations run in transactions; migrations marked transactional = false (such as CREATE INDEX CONCURRENTLY) run alone in autocommit mode.
  • Watch mode. mitch up --watch redeploys migrations as you edit them.
  • CI checks. mitch status fails with distinct exit codes when applied migrations are missing or modified.

Mitch talks to databases through SQLAlchemy, so any engine with a SQLAlchemy dialect can work. The test suite covers SQLite and PostgreSQL.

Installation

Mitch needs Python 3.14 or newer. Install it as a command-line tool with uv, together with the database driver you need:

uv tool install --with psycopg2-binary git+https://ourforge.nullachtvierzehn.de/git/timo/mitch

SQLite needs no extra driver. For other databases, replace psycopg2-binary with the driver of your SQLAlchemy dialect (for example pymysql for MariaDB/MySQL). pipx install works the same way.

Quick start

mkdir shop-db && cd shop-db
mitch init org.example.shop            # creates mitch.toml with the project ID

export MITCH_DATABASE_URL=sqlite:///shop.db

mitch create customers <<'SQL'
-- up.sql
create table customers (id integer primary key, name text not null);
-- down.sql
drop table customers;
SQL

mitch create orders --dependency customers <<'SQL'
-- up.sql
create table orders (id integer primary key, customer_id integer not null references customers (id));
-- down.sql
drop table orders;
SQL

mitch status        # A customers / A orders: available, not applied yet
mitch up            # applies customers, then orders
mitch log           # lists applied migrations with time and status
mitch drop customers   # reverts orders, then customers

Each mitch create writes three files next to each other:

orders.up.sql       # applies the migration
orders.down.sql     # reverts it (optional)
orders.config.toml  # id, created_at, dependencies, ... (optional)

The database URL is any SQLAlchemy URL, for example postgresql+psycopg2://user@localhost/shop. Pass it with --database/-d, or set MITCH_DATABASE_URL or DATABASE_URL.

Commands

Command What it does
mitch init <project-id> Create mitch.toml in the current directory.
mitch create <id> Create the files of a new migration, optionally filled from stdin.
mitch status [<id>...] Show which migrations are available, modified or missing on the database.
mitch up Apply all pending migrations, plus the linked migrations they require.
mitch add <id>... Apply the given migrations and their dependencies.
mitch drop <id>... Revert the given migrations and their dependants.
mitch down Revert all applied migrations of the project.
mitch log List applied migrations with time of deployment and on-disk status.
mitch rework <id> Start a new version of a migration and archive the current one.
mitch checkout <branch> Switch branches and revert/re-apply migrations to match.
mitch stash Stash uncommitted changes after reverting what they applied.

mitch <command> --help lists every option. The command reference explains them, including the exit codes of mitch status.

Documentation

Development

jj git clone https://ourforge.nullachtvierzehn.de/git/timo/mitch   # or git clone
cd mitch
uv sync
uv run pytest
uv run ruff check src tests

The PostgreSQL tests run only when MITCH_TEST_POSTGRES_URL points to a server where the user may create databases, for example MITCH_TEST_POSTGRES_URL=postgresql+psycopg2:///postgres uv run pytest.

Mitch's home is ourforge.nullachtvierzehn.de/timo/mitch. Issues and merge requests live there. The project is developed with Jujutsu: push your changes as a bookmark other than main, and it is reviewed as a stack.

License

Licensed under the Apache License, Version 2.0.