Add database migration for schema updates
When the registry server starts, it now automatically adds any missing columns to existing tables. This prevents 500 errors when publishing to a database created before newer columns were added. Columns added by migration: - scrutiny_status, scrutiny_report (tool scrutiny) - source, source_url, source_json (tool attribution) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
0462750b88
commit
63149aec07
|
|
@ -281,6 +281,39 @@ def connect_db(path: Path | None = None) -> sqlite3.Connection:
|
||||||
def init_db(conn: sqlite3.Connection) -> None:
|
def init_db(conn: sqlite3.Connection) -> None:
|
||||||
conn.executescript(SCHEMA_SQL)
|
conn.executescript(SCHEMA_SQL)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
# Run migrations for existing databases
|
||||||
|
migrate_db(conn)
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_db(conn: sqlite3.Connection) -> None:
|
||||||
|
"""Add missing columns to existing tables (for schema updates)."""
|
||||||
|
# Get existing columns in tools table
|
||||||
|
cursor = conn.execute("PRAGMA table_info(tools)")
|
||||||
|
existing_cols = {row[1] for row in cursor.fetchall()}
|
||||||
|
|
||||||
|
# Columns that may need to be added (column_name, type, default)
|
||||||
|
migrations = [
|
||||||
|
("scrutiny_status", "TEXT", "'pending'"),
|
||||||
|
("scrutiny_report", "TEXT", "NULL"),
|
||||||
|
("source", "TEXT", "NULL"),
|
||||||
|
("source_url", "TEXT", "NULL"),
|
||||||
|
("source_json", "TEXT", "NULL"),
|
||||||
|
]
|
||||||
|
|
||||||
|
for col_name, col_type, default in migrations:
|
||||||
|
if col_name not in existing_cols:
|
||||||
|
try:
|
||||||
|
conn.execute(f"ALTER TABLE tools ADD COLUMN {col_name} {col_type} DEFAULT {default}")
|
||||||
|
conn.commit()
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
pass # Column might already exist or other issue
|
||||||
|
|
||||||
|
# Ensure indexes exist
|
||||||
|
try:
|
||||||
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_tools_owner ON tools(owner)")
|
||||||
|
conn.commit()
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def query_one(conn: sqlite3.Connection, sql: str, params: Iterable | None = None):
|
def query_one(conn: sqlite3.Connection, sql: str, params: Iterable | None = None):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue