M1-T03: unify data layer, models, deps and routes onto single app DB
Collapse the three data layers into one. app/db.py now exposes a single Base, a cached engine bound to app_database_url with SQLite WAL enabled, and get_engine/get_session_local/reset_db_caches/get_db_session. Delete app/auth_db.py, app/poo_db.py and app/models/base.py. All models (auth, config, public_ip, location, poo) inherit the one Base and register on a single metadata. Dependencies converge to a single get_db; all routes use it. Also update the alembic env.py files (app/location/poo) and tests that imported the removed modules so the suite stays green, and drop the obsolete test_legacy_style_location_db test whose flow (app reading a separate location DB) no longer exists. Location/poo Alembic chains, adopt scripts and adoption tests remain for M1-T04; config fields remain for M1-T05. pytest 109 passed; ruff clean (pre-existing only); WAL verified; single Base.metadata holds all seven tables.
This commit is contained in:
+13
-13
@@ -5,7 +5,7 @@ import pytest
|
||||
from alembic import command
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.auth_db import reset_auth_db_caches
|
||||
from app.db import reset_db_caches
|
||||
from app.config import get_settings
|
||||
from app.main import create_app
|
||||
from scripts.app_db_adopt import APP_BASELINE_REVISION, adopt_or_initialize_app_db
|
||||
@@ -49,7 +49,7 @@ def test_app_start_fails_when_app_db_missing(tmp_path, monkeypatch: pytest.Monke
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{location_database_path}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
with pytest.raises(RuntimeError, match="Run 'python scripts/app_db_adopt.py' first"):
|
||||
@@ -58,7 +58,7 @@ def test_app_start_fails_when_app_db_missing(tmp_path, monkeypatch: pytest.Monke
|
||||
assert not missing_app_path.exists()
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
|
||||
def test_app_db_adoption_initializes_new_database(tmp_path) -> None:
|
||||
@@ -108,7 +108,7 @@ def test_app_start_seeds_missing_config_from_env_without_overwriting_existing_va
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{location_database_path}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
anyio.run(_run_lifespan, app)
|
||||
@@ -124,7 +124,7 @@ def test_app_start_seeds_missing_config_from_env_without_overwriting_existing_va
|
||||
assert rows["AUTH_SESSION_COOKIE_NAME"] == "home_automation_session"
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
|
||||
def test_app_start_syncs_app_hostname_from_env_even_when_db_has_old_value(
|
||||
@@ -152,7 +152,7 @@ def test_app_start_syncs_app_hostname_from_env_even_when_db_has_old_value(
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{location_database_path}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
anyio.run(_run_lifespan, app)
|
||||
@@ -166,7 +166,7 @@ def test_app_start_syncs_app_hostname_from_env_even_when_db_has_old_value(
|
||||
assert rows["APP_HOSTNAME"] == "new.example.com"
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
|
||||
def test_app_start_fails_when_location_db_missing(
|
||||
@@ -182,14 +182,14 @@ def test_app_start_fails_when_location_db_missing(
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{tmp_path / 'missing.db'}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
with pytest.raises(RuntimeError, match="Run 'python scripts/location_db_adopt.py' first"):
|
||||
anyio.run(_run_lifespan, app)
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
|
||||
def test_app_start_fails_when_location_db_exists_but_is_not_adopted(
|
||||
@@ -223,14 +223,14 @@ def test_app_start_fails_when_location_db_exists_but_is_not_adopted(
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{database_path}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
with pytest.raises(RuntimeError, match="is not yet Alembic-managed"):
|
||||
anyio.run(_run_lifespan, app)
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
|
||||
def test_app_start_fails_when_location_db_revision_mismatches(
|
||||
@@ -254,11 +254,11 @@ def test_app_start_fails_when_location_db_revision_mismatches(
|
||||
monkeypatch.setenv("LOCATION_DATABASE_URL", f"sqlite:///{database_path}")
|
||||
monkeypatch.setenv("POO_DATABASE_URL", f"sqlite:///{poo_database_path}")
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
app = create_app()
|
||||
with pytest.raises(RuntimeError, match="Location DB revision mismatch"):
|
||||
anyio.run(_run_lifespan, app)
|
||||
|
||||
get_settings.cache_clear()
|
||||
reset_auth_db_caches()
|
||||
reset_db_caches()
|
||||
|
||||
Reference in New Issue
Block a user