-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from lsst-sqre/tickets/DM-47262b
DM-47262: Document testing Alembic config against schema
- Loading branch information
Showing
6 changed files
with
129 additions
and
9 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,7 @@ | ||
### New features | ||
|
||
- Add `safir.database.drop_database` utility function to drop all database tables mentioned in a SQLAlchemy ORM schema and delete the Alembic version information if it is present. This is primarily useful for tests. | ||
|
||
### Other changes | ||
|
||
- Document how to test an application's database schema against its Alembic migrations to ensure that the schema hasn't been modified without adding a corresponding migration. |
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
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
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
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
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
create_database_engine, | ||
datetime_from_db, | ||
datetime_to_db, | ||
drop_database, | ||
initialize_database, | ||
is_database_current, | ||
retry_async_transaction, | ||
|
@@ -36,7 +37,7 @@ | |
|
||
|
||
@pytest.mark.asyncio | ||
async def test_database_init( | ||
async def test_initialize_database( | ||
database_url: str, database_password: str | ||
) -> None: | ||
logger = structlog.get_logger(__name__) | ||
|
@@ -72,6 +73,27 @@ async def test_database_init( | |
await engine.dispose() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_drop_database( | ||
database_url: str, database_password: str | ||
) -> None: | ||
logger = structlog.get_logger(__name__) | ||
engine = create_database_engine(database_url, database_password) | ||
await initialize_database(engine, logger, schema=BaseV2.metadata) | ||
session = await create_async_session(engine, logger) | ||
async with session.begin(): | ||
session.add(UserV2(username="someuser")) | ||
await session.remove() | ||
|
||
await drop_database(engine, BaseV2.metadata) | ||
session = await create_async_session(engine, logger) | ||
with pytest.raises(ProgrammingError): | ||
async with session.begin(): | ||
await session.scalars(select(UserV2.username)) | ||
await session.remove() | ||
await engine.dispose() | ||
|
||
|
||
def test_build_database_url(database_url: str) -> None: | ||
url = build_database_url("postgresql://[email protected]/foo", None) | ||
assert url == "postgresql+asyncpg://[email protected]/foo" | ||
|