Skip to main content

Catch database migration rollback failures before they reach production

Project description

pytest-mrt

PyPI CI 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 24 known dangerous patterns.

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     │ Message                            │
├──────────┼──────────────────────────┼─────────┼────────────────────────────────────┤
│ 004      │ DROP COLUMN in upgrade   │ error   │ Data permanently lost on rollback  │
│ 005      │ No-op downgrade          │ error   │ downgrade() does nothing           │
│ 006      │ INDEX without CONCURR.   │ warning │ 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()
  • 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
SQLite
MySQL / MariaDB 🔜 planned

Documentation

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

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-0.4.0.tar.gz (40.6 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-0.4.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_mrt-0.4.0.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_mrt-0.4.0.tar.gz
Algorithm Hash digest
SHA256 839ab559633f8aa98df1033a6d5e8ea15b79e1e1242121814656baad1424c840
MD5 41b5724751c7eb55c8072ca0679ac044
BLAKE2b-256 388008fe77422c5362095f49b8dc5c08e3da2e4cc386ad82d5567d735ec6cb36

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_mrt-0.4.0.tar.gz:

Publisher: publish.yml on croc100/pytest-mrt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pytest_mrt-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_mrt-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0abc23b728f90c4c1a0cb507561b1b860418a7a4b3bb8a703c057d6f6d70fc8
MD5 158d9e3ad9cd09800805460ca6a1a34f
BLAKE2b-256 7e134aef852e3ec6a9ad7007564f008719d74b3266ea5d1151f64b91d801247d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_mrt-0.4.0-py3-none-any.whl:

Publisher: publish.yml on croc100/pytest-mrt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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