fix(db): generalize schema-drift detection to all M3 tables (slabs, slab_photos, sync_log)

Live host had stale M2 slabs table (no deleted_at/server_rev) -> GET /slabs 500.
Drift check now verifies sentinel (table,column) pairs across settings/slabs/
slab_photos/sync_log; on drift drops all app-owned M3 tables and rebuilds, stamps
schema_version. Drift test now seeds M2 settings + slabs and asserts both rebuilt.
pytest 23 passed; gates exit 0.
This commit is contained in:
2026-09-19 16:30:43 -07:00
parent cc5241f7da
commit 38eebbc8bb
2 changed files with 42 additions and 16 deletions
+9 -2
View File
@@ -25,8 +25,12 @@ def _setup_temp_db_with_m2_schema(tmp_dir: str) -> str:
# M2-era settings shape: NO `value` column (this is the drift signature).
# Use a raw connection (DELETE journal) exactly as an old M2 db would be on disk.
conn = sqlite3.connect(db_path)
# M2-era settings: NO `value` column (drift signature).
conn.execute("CREATE TABLE settings (key TEXT PRIMARY KEY, value_enc BLOB, updated_at TEXT)")
conn.execute("INSERT INTO settings (key, value_enc, updated_at) VALUES ('woo_wp_username', X'00', '2026-01-01')")
# M2-era slabs: NO deleted_at / server_rev (drift signature).
conn.execute("CREATE TABLE slabs (id TEXT PRIMARY KEY, sku TEXT, status TEXT)")
conn.execute("INSERT INTO slabs (id, sku, status) VALUES ('old', 'OLD-1', 'calibrated')")
conn.commit()
conn.close()
# Force any journal sidecars to merge before the WAL-mode factory opens the file.
@@ -50,6 +54,9 @@ def test_drift_detection_and_rebuild(monkeypatch, drift_db_dir):
cols = {row[1] for row in info}
assert "value" in cols
assert "value_enc" in cols
# slabs must also be rebuilt to the M3 shape (deleted_at present)
slab_cols = {row[1] for row in conn.execute("PRAGMA table_info(slabs)").fetchall()}
assert "deleted_at" in slab_cols and "server_rev" in slab_cols
# M2 rows are disposable (M3 supersedes M2 on this dev volume)
rows = conn.execute("SELECT key FROM settings").fetchall()
assert rows == []
assert conn.execute("SELECT key FROM settings").fetchall() == []
assert conn.execute("SELECT id FROM slabs").fetchall() == []