From 8cf5f12ffe15741560725b7e40cb654002098a81 Mon Sep 17 00:00:00 2001 From: Jason Staack Date: Sat, 14 Mar 2026 22:30:26 -0500 Subject: [PATCH] fix(ci): use DATABASE_URL env var for alembic migrations in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - alembic/env.py: strengthen the URL override to fall back to TEST_DATABASE_URL when DATABASE_URL is absent, so alembic never falls back to the hardcoded 'tod' URL in alembic.ini regardless of which env var a test runner sets. - tests/integration/conftest.py: add explanatory comments on why DATABASE_URL is forced into the subprocess env, and use env.setdefault() to supply CREDENTIAL_ENCRYPTION_KEY if the calling environment omits it — migration 029 (VPN tenant isolation) requires it to encrypt the WireGuard server private key. Co-Authored-By: Claude Sonnet 4.6 --- backend/alembic/env.py | 9 ++++++--- backend/tests/integration/conftest.py | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/alembic/env.py b/backend/alembic/env.py index 3915537..d336d91 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -20,9 +20,12 @@ import app.models.config_backup # noqa: F401 # access to the values within the .ini file in use. config = context.config -# Override sqlalchemy.url from DATABASE_URL env var if set (for Docker) -if os.environ.get("DATABASE_URL"): - config.set_main_option("sqlalchemy.url", os.environ["DATABASE_URL"]) +# Override sqlalchemy.url from environment variable so alembic never uses the +# hardcoded URL in alembic.ini. DATABASE_URL takes precedence; TEST_DATABASE_URL +# is a fallback for test runners that set only that variable. +_db_url = os.environ.get("DATABASE_URL") or os.environ.get("TEST_DATABASE_URL") +if _db_url: + config.set_main_option("sqlalchemy.url", _db_url) # Interpret the config file for Python logging. if config.config_file_name is not None: diff --git a/backend/tests/integration/conftest.py b/backend/tests/integration/conftest.py index be2446f..4dfe97f 100644 --- a/backend/tests/integration/conftest.py +++ b/backend/tests/integration/conftest.py @@ -65,7 +65,16 @@ def _ensure_database_setup(): backend_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) env = os.environ.copy() + # Ensure DATABASE_URL points at the test database, not the dev/prod URL + # hardcoded in alembic.ini. alembic/env.py reads this variable and overrides + # sqlalchemy.url before opening any connection. env["DATABASE_URL"] = TEST_DATABASE_URL + # Migration 029 (VPN tenant isolation) encrypts a WireGuard server private key + # and requires CREDENTIAL_ENCRYPTION_KEY. Provide the dev default if the + # environment does not already supply it (CI always sets this explicitly). + env.setdefault( + "CREDENTIAL_ENCRYPTION_KEY", "LLLjnfBZTSycvL2U07HDSxUeTtLxb9cZzryQl0R9E4w=" + ) # Run Alembic migrations via subprocess (handles DB creation and schema) result = subprocess.run(