-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrations): add test to validate autogenerated migrations
Generated-by: aiautocommit
- Loading branch information
1 parent
9db9a3e
commit 03e0511
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |