Skip to content

Commit

Permalink
feat(migrations): add test to validate autogenerated migrations
Browse files Browse the repository at this point in the history
Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Feb 8, 2025
1 parent 9db9a3e commit 03e0511
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/migrations_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
from pathlib import Path

from alembic import command
from alembic.config import Config


def test_migrations():
# TODO assert that no tables exist in the DB

test_root = Path(__file__).parent
migrations_path = test_root / "migrations"
versions_path = migrations_path / "versions"

# remove all existing migration files
for file in os.listdir(str(versions_path)):
os.remove(str(versions_path / file))

# Configure Alembic
alembic_cfg = Config(str(migrations_path / "alembic.ini"))
alembic_cfg.set_main_option("script_location", str(test_root / "migrations"))

# Generate a revision with autogenerate enabled
command.revision(alembic_cfg, message="autogenerated migration", autogenerate=True)

# Inspect the migration file if needed
migration_files = os.listdir(str(versions_path))
migration_path = os.path.join(str(versions_path), sorted(migration_files)[-1])

with open(migration_path, "r") as f:
migration_contents = f.read()

# Now you can perform assertions on migration_contents
assert "CREATE TABLE" in migration_contents

0 comments on commit 03e0511

Please sign in to comment.