diff --git a/backend/tests/test_migrations.py b/backend/tests/test_migrations.py index 9d02b6b6..b09b03c3 100644 --- a/backend/tests/test_migrations.py +++ b/backend/tests/test_migrations.py @@ -59,10 +59,14 @@ def test_head_migration_downgrades_and_reupgrades(db): # it lands, not during a rollback. Leaves the schema back at head for the rest of the session. cfg = Config("alembic.ini") command.downgrade(cfg, "-1") + reupgraded = False try: command.upgrade(cfg, "head") + reupgraded = True assert _structural_drift() == [] finally: - # Always leave the SHARED schema at head, whatever the upgrade/assert above did — otherwise a - # failure here strands the DB one revision behind and cascades into every later DB-lane test. - command.upgrade(cfg, "head") + # If the re-upgrade itself failed, restore the SHARED schema to head so the failure doesn't + # cascade into every later DB-lane test. Skip it when the upgrade already succeeded — re-running + # it would be a redundant no-op and could mask a failing assert with the finally's own error. + if not reupgraded: + command.upgrade(cfg, "head")