diff --git a/server.py b/server.py index a027a19..db72ed7 100644 --- a/server.py +++ b/server.py @@ -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