feat: Refactor database configuration to use granular environment variables; update Docker and CI/CD workflows accordingly
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from typing import Generator
|
||||
from typing import Dict, Generator
|
||||
|
||||
import pytest
|
||||
# type: ignore[import]
|
||||
from playwright.sync_api import Browser, Page, Playwright, sync_playwright
|
||||
|
||||
import httpx
|
||||
from sqlalchemy.engine import make_url
|
||||
|
||||
# Use a different port for the test server to avoid conflicts
|
||||
TEST_PORT = 8001
|
||||
@@ -16,6 +18,8 @@ BASE_URL = f"http://localhost:{TEST_PORT}"
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def live_server() -> Generator[str, None, None]:
|
||||
"""Launch a live test server in a separate process."""
|
||||
env = _prepare_database_environment(os.environ.copy())
|
||||
|
||||
process = subprocess.Popen(
|
||||
[
|
||||
"uvicorn",
|
||||
@@ -26,7 +30,7 @@ def live_server() -> Generator[str, None, None]:
|
||||
],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
env=os.environ.copy(),
|
||||
env=env,
|
||||
)
|
||||
|
||||
deadline = time.perf_counter() + 30
|
||||
@@ -85,3 +89,35 @@ def page(browser: Browser, live_server: str) -> Generator[Page, None, None]:
|
||||
page.wait_for_load_state("networkidle")
|
||||
yield page
|
||||
page.close()
|
||||
|
||||
|
||||
def _prepare_database_environment(env: Dict[str, str]) -> Dict[str, str]:
|
||||
"""Ensure granular database env vars are available for the app under test."""
|
||||
|
||||
required = ("DATABASE_HOST", "DATABASE_USER", "DATABASE_NAME")
|
||||
if all(env.get(key) for key in required):
|
||||
return env
|
||||
|
||||
legacy_url = env.get("DATABASE_URL")
|
||||
if not legacy_url:
|
||||
return env
|
||||
|
||||
url = make_url(legacy_url)
|
||||
env.setdefault("DATABASE_DRIVER", url.drivername)
|
||||
if url.host:
|
||||
env.setdefault("DATABASE_HOST", url.host)
|
||||
if url.port:
|
||||
env.setdefault("DATABASE_PORT", str(url.port))
|
||||
if url.username:
|
||||
env.setdefault("DATABASE_USER", url.username)
|
||||
if url.password:
|
||||
env.setdefault("DATABASE_PASSWORD", url.password)
|
||||
if url.database:
|
||||
env.setdefault("DATABASE_NAME", url.database)
|
||||
|
||||
query_options = dict(url.query) if url.query else {}
|
||||
options = query_options.get("options")
|
||||
if isinstance(options, str) and "search_path=" in options:
|
||||
env.setdefault("DATABASE_SCHEMA", options.split("search_path=")[-1])
|
||||
|
||||
return env
|
||||
|
||||
Reference in New Issue
Block a user