test(postgres): allow Testcontainers to override the migration sqlalchemy.url

alembic/env.py previously always overwrote sqlalchemy.url from
Settings().postgres.dsn, which made it impossible for a test fixture to
point Alembic at a Testcontainers-managed database. Now env.py only
sets it when unset, and a new integration suite runs `alembic upgrade
head` against a real Postgres container per ADR-0016 (no create_all()).
This commit is contained in:
2026-08-18 10:21:56 +03:30
parent d71dd1bd0c
commit 80ed5b1577
4 changed files with 104 additions and 6 deletions

View File

@@ -19,10 +19,13 @@ if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Application models' MetaData, used for 'autogenerate' support. The database
# URL is likewise sourced from application settings, not alembic.ini, so both
# migrations and the app read the same env-derived configuration (ADR-0009).
# URL is likewise sourced from application settings by default, so both
# migrations and the app read the same env-derived configuration (ADR-0009) —
# unless a caller (e.g. a test fixture pointing at a Testcontainers database)
# has already set sqlalchemy.url on this Config before invoking Alembic.
target_metadata = Base.metadata
config.set_main_option("sqlalchemy.url", Settings().postgres.dsn)
if not config.get_main_option("sqlalchemy.url"):
config.set_main_option("sqlalchemy.url", Settings().postgres.dsn)
def run_migrations_offline() -> None: