fix: migrate DB schema with ALTER TABLE instead of dropping old data
Legacy records get new columns with sensible defaults and are preserved as markers. No more data loss on schema upgrade. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -225,34 +225,47 @@ class ProcessedDB:
|
|||||||
self._enabled = False
|
self._enabled = False
|
||||||
|
|
||||||
def _migrate(self) -> None:
|
def _migrate(self) -> None:
|
||||||
"""Create or recreate table if schema is outdated."""
|
"""Create table if missing, then add any new columns for old DBs."""
|
||||||
cols = {
|
cols = {
|
||||||
row[1]
|
row[1]
|
||||||
for row in self._con.execute("PRAGMA table_info(processed)").fetchall()
|
for row in self._con.execute("PRAGMA table_info(processed)").fetchall()
|
||||||
}
|
}
|
||||||
required = {"start_time", "output_path", "label", "category",
|
if not cols:
|
||||||
"short_side", "portrait_ratio", "crop_center", "format",
|
# Fresh DB — create from scratch
|
||||||
"clip_count", "spread"}
|
self._con.execute(
|
||||||
needs_recreate = not required.issubset(cols)
|
"CREATE TABLE IF NOT EXISTS processed ("
|
||||||
if needs_recreate:
|
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
self._con.execute("DROP TABLE IF EXISTS processed")
|
" filename TEXT NOT NULL,"
|
||||||
self._con.execute(
|
" start_time REAL NOT NULL,"
|
||||||
"CREATE TABLE IF NOT EXISTS processed ("
|
" output_path TEXT NOT NULL,"
|
||||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
" label TEXT NOT NULL DEFAULT '',"
|
||||||
" filename TEXT NOT NULL,"
|
" category TEXT NOT NULL DEFAULT '',"
|
||||||
" start_time REAL NOT NULL,"
|
" short_side INTEGER,"
|
||||||
" output_path TEXT NOT NULL,"
|
" portrait_ratio TEXT NOT NULL DEFAULT '',"
|
||||||
" label TEXT NOT NULL DEFAULT '',"
|
" crop_center REAL NOT NULL DEFAULT 0.5,"
|
||||||
" category TEXT NOT NULL DEFAULT '',"
|
" format TEXT NOT NULL DEFAULT 'MP4',"
|
||||||
" short_side INTEGER,"
|
" clip_count INTEGER NOT NULL DEFAULT 3,"
|
||||||
" portrait_ratio TEXT NOT NULL DEFAULT '',"
|
" spread REAL NOT NULL DEFAULT 3.0,"
|
||||||
" crop_center REAL NOT NULL DEFAULT 0.5,"
|
" processed_at TEXT NOT NULL"
|
||||||
" format TEXT NOT NULL DEFAULT 'MP4',"
|
")"
|
||||||
" clip_count INTEGER NOT NULL DEFAULT 3,"
|
)
|
||||||
" spread REAL NOT NULL DEFAULT 3.0,"
|
else:
|
||||||
" processed_at TEXT NOT NULL"
|
# Add missing columns to legacy tables
|
||||||
")"
|
new_cols = {
|
||||||
)
|
"label": "TEXT NOT NULL DEFAULT ''",
|
||||||
|
"category": "TEXT NOT NULL DEFAULT ''",
|
||||||
|
"short_side": "INTEGER",
|
||||||
|
"portrait_ratio": "TEXT NOT NULL DEFAULT ''",
|
||||||
|
"crop_center": "REAL NOT NULL DEFAULT 0.5",
|
||||||
|
"format": "TEXT NOT NULL DEFAULT 'MP4'",
|
||||||
|
"clip_count": "INTEGER NOT NULL DEFAULT 3",
|
||||||
|
"spread": "REAL NOT NULL DEFAULT 3.0",
|
||||||
|
}
|
||||||
|
for col, typedef in new_cols.items():
|
||||||
|
if col not in cols:
|
||||||
|
self._con.execute(
|
||||||
|
f"ALTER TABLE processed ADD COLUMN {col} {typedef}"
|
||||||
|
)
|
||||||
self._con.execute(
|
self._con.execute(
|
||||||
"CREATE INDEX IF NOT EXISTS idx_filename ON processed(filename)"
|
"CREATE INDEX IF NOT EXISTS idx_filename ON processed(filename)"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user