DB schema: add disney_park column to assets, create service_entrances table

- Added disney_park TEXT DEFAULT NULL to assets table in _create_v2_tables
- Created service_entrances table with id, location_id, name, latitude,
  longitude, notes, created_at, updated_at; location_id FK to locations
  with ON DELETE CASCADE; name/lat/lon are NOT NULL
- Added v4 migration in init_db: ALTER TABLE for disney_park on existing
  DBs, schema-aware recreation of service_entrances if name column lacks
  NOT NULL constraint
- 44/45 tests pass (1 pre-existing 404 failure on stats endpoint)
This commit is contained in:
2026-05-22 17:45:47 -04:00
parent 6aa5d3d35c
commit 27f372ed49
+28 -15
View File
@@ -445,23 +445,36 @@ def init_db(conn: sqlite3.Connection):
conn.execute("ALTER TABLE assets ADD COLUMN disney_park TEXT DEFAULT NULL") conn.execute("ALTER TABLE assets ADD COLUMN disney_park TEXT DEFAULT NULL")
conn.commit() conn.commit()
# Ensure service_entrances table exists for existing DBs # Ensure service_entrances table exists with correct schema
conn.execute(""" se_exists = conn.execute(
CREATE TABLE IF NOT EXISTS service_entrances ( "SELECT name FROM sqlite_master WHERE type='table' AND name='service_entrances'"
id INTEGER PRIMARY KEY AUTOINCREMENT, ).fetchone()
location_id INTEGER NOT NULL REFERENCES locations(id) ON DELETE CASCADE, if se_exists:
name TEXT NOT NULL, # Check if name column has NOT NULL constraint
latitude REAL NOT NULL, se_cols = conn.execute("PRAGMA table_info(service_entrances)").fetchall()
longitude REAL NOT NULL, se_name_col = next((c for c in se_cols if c[1] == 'name'), None)
notes TEXT DEFAULT '', if se_name_col and not se_name_col[3]: # notnull == 0
created_at TEXT NOT NULL DEFAULT (datetime('now')), # Table exists but with wrong schema — recreate it (new table, safe to drop)
updated_at TEXT NOT NULL DEFAULT (datetime('now')) conn.execute("DROP TABLE service_entrances")
) se_exists = False
""")
conn.commit() if not se_exists:
conn.execute("""
CREATE TABLE service_entrances (
id INTEGER PRIMARY KEY AUTOINCREMENT,
location_id INTEGER NOT NULL REFERENCES locations(id) ON DELETE CASCADE,
name TEXT NOT NULL,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
notes TEXT DEFAULT '',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
)
""")
conn.commit()
# --- App / Middleware # ─── App / Middleware ───────────────────────────────────────────────────────
@asynccontextmanager @asynccontextmanager