Quellcode durchsuchen

Add schema migration for birthplace column

- CREATE TABLE without birthplace (base schema)
- ALTER TABLE ADD COLUMN if missing (migration v2)
- Works for both fresh installs and existing DBs
Lukas Goldschmidt vor 1 Monat
Ursprung
Commit
d7a78a84e9
1 geänderte Dateien mit 5 neuen und 2 gelöschten Zeilen
  1. 5 2
      src/astro_mcp/storage.py

+ 5 - 2
src/astro_mcp/storage.py

@@ -33,7 +33,7 @@ def _get_conn() -> sqlite3.Connection:
 
 
 def _init_db_sync() -> None:
-    """Synchronous database initialization."""
+    """Synchronous database initialization with non-destructive migrations."""
     DB_PATH.parent.mkdir(parents=True, exist_ok=True)
     conn = _get_conn()
     conn.executescript("""
@@ -42,7 +42,6 @@ def _init_db_sync() -> None:
             name        TEXT NOT NULL,
             nickname    TEXT UNIQUE,
             birth_datetime  TEXT NOT NULL,
-            birthplace  TEXT,
             latitude    REAL NOT NULL,
             longitude   REAL NOT NULL,
             elevation   REAL DEFAULT 0.0,
@@ -52,6 +51,10 @@ def _init_db_sync() -> None:
         CREATE INDEX IF NOT EXISTS idx_persons_nickname ON persons(nickname);
         CREATE INDEX IF NOT EXISTS idx_persons_name ON persons(name);
     """)
+    # Non-destructive migrations (ALTER TABLE ADD COLUMN)
+    cols = [row[1] for row in conn.execute("PRAGMA table_info(persons)")]
+    if "birthplace" not in cols:
+        conn.execute("ALTER TABLE persons ADD COLUMN birthplace TEXT")
     conn.commit()
     conn.close()