Skip to main content

Catch database migration rollback failures before they reach production

Project description

pytest-mrt

PyPI Downloads Used by CI Coverage Coverage 81% Production/Stable Python MIT License

A pytest plugin that catches database migration rollback failures before they reach production.


alembic downgrade -1 ran clean. No errors. Your monitoring went green.

But the users' phone numbers are gone. The column came back. The data didn't.


What it does

Most tools verify that migrations run without errors.
pytest-mrt verifies that your data survives a rollback.

It seeds real rows before each migration, rolls back, and checks nothing was lost. It also statically scans migration files for 44 known dangerous patterns across both Alembic and Django migrations.

Install

pip install pytest-mrt

Setup (2 minutes)

1. Create conftest.py in your project root:

# conftest.py
import os
from pytest_mrt import MRTConfig


def pytest_configure(config):
    config._mrt_config = MRTConfig(
        alembic_ini="alembic.ini",                               # path to your alembic.ini
        db_url=os.environ.get("TEST_DATABASE_URL", "sqlite:///test.db"),  # test database
    )

2. Write a test:

# tests/test_migrations.py


def test_migrations_are_safe(mrt):
    mrt.assert_all_reversible()

3. Run:

pytest tests/test_migrations.py -s

mrt is a pytest fixture — just add it as a parameter and it works. No import needed in test files.

Static analysis (no database needed)

mrt check migrations/versions/
╭──────────┬──────────────────────────┬─────────┬──────┬─────────┬────────────────────────────────────╮
│ Revision │ Pattern                  │ Sev     │ Line │ Code    │ Message                            │
├──────────┼──────────────────────────┼─────────┼──────┼─────────┼────────────────────────────────────┤
│ 004      │ DROP COLUMN in upgrade   │ error   │   12 │ MRT103  │ Data permanently lost on rollback  │
│ 005      │ No-op downgrade          │ error   │    8 │ MRT102  │ downgrade() does nothing           │
│ 006      │ INDEX without CONCURR.   │ warning │   19 │ MRT207  │ Locks table during index build     │
╰──────────┴──────────────────────────┴─────────┴──────┴─────────┴────────────────────────────────────╯
2 error(s), 1 warning(s)

What gets caught

Errors (will cause data loss or a broken rollback):

  • op.drop_column() in upgrade — data is gone even if downgrade re-adds the column
  • op.drop_table() in upgrade — all rows permanently lost
  • TRUNCATE in migration
  • def downgrade(): pass — rollback silently does nothing
  • No downgrade() function
  • rename_table / rename_column without reverse
  • DROP VIEW without recreating in downgrade
  • ALTER TYPE ... ADD VALUE (PostgreSQL ENUM) — can't roll back once rows use the new value
  • Add column + migrate data + drop original in one migration

Warnings (review before deploying):

  • NOT NULL without server_default
  • Column type change
  • Raw op.execute() / context.execute() without reverse
  • op.execute(sa.text(...)) — SQL inside sa.text() wrapper now fully analyzed
  • op.bulk_insert() without corresponding DELETE in downgrade
  • Bulk UPDATE without a reverse UPDATE in downgrade
  • ON DELETE CASCADE added
  • CREATE INDEX without CONCURRENTLY (PostgreSQL)
  • ADD COLUMN with DEFAULT on large tables
  • CREATE UNIQUE CONSTRAINT on existing data
  • DROP INDEX without recreating
  • DROP CONSTRAINT without recreating
  • ALTER SEQUENCE / setval
  • NOT NULL via raw SQL without reverse
  • NOT NULL without restoring nullable in downgrade

Databases

Static analysis Dynamic verification
PostgreSQL Yes Yes
SQLite Yes Yes
MySQL / MariaDB Yes Yes
Oracle Yes Yes
SQL Server Yes Yes
pip install pytest-mrt[mysql]    # PyMySQL
pip install pytest-mrt[oracle]   # python-oracledb
pip install pytest-mrt[mssql]    # pymssql

Auto-fix missing reverse operations (v1.3.0)

mrt fix generates missing reverse operations for both Alembic and Django migrations.

Alembic — generates a missing or stub downgrade():

mrt fix migrations/versions/0042_drop_phone.py --apply

Django — adds reverse_sql, reverse_code, and full backup/restore scaffolding for data-loss operations (RemoveField, DeleteModel):

mrt fix myapp/migrations/0042_remove_user_phone.py --apply

For RemoveField and DeleteModel, the generated code backs up data to a _mrt_backups table before the migration runs, and restores it on rollback. After deployment is confirmed stable, clean up the backup rows:

mrt clean-backups --db $DATABASE_URL
mrt clean-backups --db $DATABASE_URL --label 0042_remove_user_phone --yes

pre-commit integration (v1.3.0)

Add to .pre-commit-config.yaml to run mrt check automatically before every push:

- repo: https://github.com/croc100/pytest-mrt
  rev: v1.3.0
  hooks:
    - id: mrt-check

Incremental CI — --since (v1.3.0)

Check only migrations added since a given revision. Keeps CI fast on large codebases:

mrt check migrations/versions/ --since main
mrt check myapp/migrations/ --since v1.2.0

CI/CD integration

Drop mrt check into any pipeline as a pre-deploy gate:

# GitHub Actions — blocks merge if unsafe migrations are detected
- name: Migration safety check
  run: mrt check alembic/versions/ --strict

Full examples for GitHub Actions, GitLab CI, Jenkins, and pre-commit hooks are in examples/ci-integration/.

Docker

Run tests locally against PostgreSQL or MySQL without installing anything:

docker compose run test-postgres
docker compose run test-mysql

See docker-compose.yml for the full configuration.

Performance

10 migrations 50 migrations 100 migrations
mrt check (static, no DB) 22 ms 108 ms 216 ms
mrt fixture (SQLite) 0.33 s 4.3 s 15.6 s

Safe to run mrt check on every commit. Dynamic suite fits comfortably for projects up to ~200 migrations. For larger codebases, use MRTConfig(skip={...}) to exclude already-reviewed revisions. See benchmarks for methodology and PostgreSQL/MySQL numbers.

Built-in default tests (v1.1.0)

pytest-mrt automatically injects 6 safety tests into your suite when the mrt fixture is configured — no test files needed:

Test What it checks
test_mrt_single_head Migration history has exactly one head
test_mrt_upgrade alembic upgrade head completes without error
test_mrt_downgrade_base alembic downgrade base then re-upgrade completes cleanly
test_mrt_up_down_consistency Every migration is safely reversible (per-revision rollback)
test_mrt_static_no_errors Zero static analysis errors in all migration files
test_mrt_schema_matches_models Database schema matches ORM models after upgrade (requires target_metadata)

To opt out of specific tests, use MRTConfig(skip_default_tests={...}).

Suppress known risks (v1.2.0)

Use # noqa: MRTxxx on any line to suppress a specific warning — the same convention as ruff and flake8:

def upgrade():
    op.drop_column("users", "phone")  # noqa: MRT103

To suppress all MRT warnings on a line:

    op.drop_column("users", "legacy_col")  # noqa

Legacy syntax # mrt: ignore is still supported for backward compatibility.

What's new in v1.3.0

  • Django-aware mrt fix — auto-generates reverse operations and data-safe backup/restore code for RemoveField and DeleteModel
  • mrt clean-backups — removes _mrt_backups rows after a deployment is confirmed stable
  • mrt check --since <revision> — incremental scan; only checks migrations added since a given git ref
  • pre-commit hook — add two lines to .pre-commit-config.yaml and mrt check runs automatically before every push

Changelog

See CHANGELOG.md for the full release history.

Documentation

Full docs at croc100.github.io/pytest-mrt

Sponsorship

pytest-mrt is MIT-licensed and free to use. If it saves you from a production incident, consider sponsoring development:

github.com/sponsors/croc100

Sponsorship directly funds:

  • New pattern development (Oracle, SQL Server, more Django patterns)
  • Maintained compatibility with new Alembic and SQLAlchemy releases

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_mrt-1.3.0.tar.gz (147.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_mrt-1.3.0-py3-none-any.whl (68.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_mrt-1.3.0.tar.gz.

File metadata

  • Download URL: pytest_mrt-1.3.0.tar.gz
  • Upload date:
  • Size: 147.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pytest_mrt-1.3.0.tar.gz
Algorithm Hash digest
SHA256 73ae05699e43bff7981e2df0668399bb0c7b8b2d776e627c6fe4c0fc3e849c95
MD5 1976402e5bfbbf8f527831c07f86d2c2
BLAKE2b-256 99a4f0a3d13588405c43c68d6cbbd0a30d6929b796350e10ff4be67d1f0760dc

See more details on using hashes here.

File details

Details for the file pytest_mrt-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_mrt-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 68.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pytest_mrt-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bd4d54e330a1e4c375a9daf56e591439b593cd75fb2573cfdedd648f88d8414
MD5 747554df91d3e4daa7173133ecac7f48
BLAKE2b-256 acad816d0ba92146289464a9146f8513b3824fa37554a809de7e9ab0421f1d95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page