M6-T01: add 5 energy tables, models, and migration

- app/models/energy.py: DsmrReading, EnergyContract, EnergyContractVersion,
  TibberPrice, EnergyCostPeriod (per design §3.5; JSON blobs, RESTRICT FKs).
- alembic_app migration 20260623_11_energy_tables (head), env.py imports.
- app_db_adopt APP_BASELINE_REVISION bumped to new head.
- tests/test_energy_models.py + expose_catalog test fixups for new head.
This commit is contained in:
2026-06-23 20:33:35 +02:00
parent 355c8eb8b8
commit 18fe18281b
7 changed files with 1093 additions and 11 deletions
+12 -9
View File
@@ -530,7 +530,13 @@ def test_exposed_entity_toggle_key_unique_index(expose_db):
def test_downgrade_removes_toggle_table(tmp_path: Path):
"""downgrade -1 from head must drop the exposed_entity_toggle table cleanly."""
"""Downgrading to the revision before exposed_entity_toggle must drop that table cleanly.
We downgrade to the explicit down_revision of the expose-toggle migration
(``20260622_09_modbus_tables``) rather than using ``-1`` from head, so the test
remains correct as new revisions are added on top. This is the same pattern
used in test_modbus_models.py for the modbus downgrade test.
"""
db_path = tmp_path / "downgrade_expose_test.db"
db_url = f"sqlite:///{db_path}"
alembic_cfg = _make_app_alembic_config(db_url)
@@ -542,14 +548,15 @@ def test_downgrade_removes_toggle_table(tmp_path: Path):
assert "exposed_entity_toggle" in inspector.get_table_names()
engine.dispose()
# Downgrade one step removes the exposed_entity_toggle revision.
command.downgrade(alembic_cfg, "-1")
# Downgrade to the explicit down_revision of the exposed_entity_toggle migration,
# consistent with how test_modbus_models.py handles its own downgrade test.
command.downgrade(alembic_cfg, "20260622_09_modbus_tables")
engine = create_engine(db_url, connect_args={"check_same_thread": False})
inspector = inspect(engine)
table_names = inspector.get_table_names()
assert "exposed_entity_toggle" not in table_names, (
"exposed_entity_toggle should be gone after downgrade -1"
"exposed_entity_toggle should be gone after downgrade to 20260622_09_modbus_tables"
)
# Previous tables must still exist.
assert "modbus_device" in table_names
@@ -668,7 +675,7 @@ def test_expose_toggle_update(expose_db):
def test_app_baseline_revision_matches_new_head(tmp_path: Path):
"""APP_BASELINE_REVISION must equal the Alembic head after the new migration."""
"""APP_BASELINE_REVISION must equal the Alembic head revision."""
from alembic.script import ScriptDirectory
from scripts.app_db_adopt import APP_BASELINE_REVISION
@@ -683,7 +690,3 @@ def test_app_baseline_revision_matches_new_head(tmp_path: Path):
f"APP_BASELINE_REVISION={APP_BASELINE_REVISION!r} does not match "
f"Alembic head={head!r}"
)
# Confirm the new revision is the head.
assert head == "20260622_10_exposed_entities", (
f"Expected new head to be '20260622_10_exposed_entities', got {head!r}"
)