Skip to content

Commit

Permalink
Better support for running tests in branches with different DB schemas
Browse files Browse the repository at this point in the history
Delete the DB's "public" schema directly.

We do this instead of using SQLAlchemy's drop_all because we want to delete
all tables in the current DB, not only the ones the current code base
is aware off.

This is useful while switching between branches that include DB
migrations.
  • Loading branch information
marcospri committed Sep 2, 2024
1 parent 395eff5 commit c40f712
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lms/scripts/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def delete(engine: Engine) -> None:
else:
pre_delete(engine)

Base.metadata.drop_all(engine)
with engine.connect() as connection:
# Delete the DB "public" schema directly.
# We do this instead of using SQLAlchemy's drop_all because we want to delete all tables in the current DB.
# For example, this will delete tables created by migrations in other branches, not only the ones SQLAlchemy know about in the current code base.
connection.execute(text("DROP SCHEMA PUBLIC CASCADE;"))
connection.execute(text("CREATE SCHEMA PUBLIC;"))

try:
from lms.db import post_delete # noqa: PLC0415
Expand Down

0 comments on commit c40f712

Please sign in to comment.