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:
@@ -445,23 +445,36 @@ def init_db(conn: sqlite3.Connection):
|
||||
conn.execute("ALTER TABLE assets ADD COLUMN disney_park TEXT DEFAULT NULL")
|
||||
conn.commit()
|
||||
|
||||
# Ensure service_entrances table exists for existing DBs
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS 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()
|
||||
# Ensure service_entrances table exists with correct schema
|
||||
se_exists = conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='service_entrances'"
|
||||
).fetchone()
|
||||
if se_exists:
|
||||
# Check if name column has NOT NULL constraint
|
||||
se_cols = conn.execute("PRAGMA table_info(service_entrances)").fetchall()
|
||||
se_name_col = next((c for c in se_cols if c[1] == 'name'), None)
|
||||
if se_name_col and not se_name_col[3]: # notnull == 0
|
||||
# Table exists but with wrong schema — recreate it (new table, safe to drop)
|
||||
conn.execute("DROP TABLE service_entrances")
|
||||
se_exists = False
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user