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:
2026-04-12 02:53:12 +02:00
parent 74e8656335
commit c174d891fb
+20 -7
View File
@@ -225,17 +225,13 @@ 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"}
needs_recreate = not required.issubset(cols)
if needs_recreate:
self._con.execute("DROP TABLE IF EXISTS processed")
self._con.execute( self._con.execute(
"CREATE TABLE IF NOT EXISTS processed (" "CREATE TABLE IF NOT EXISTS processed ("
" id INTEGER PRIMARY KEY AUTOINCREMENT," " id INTEGER PRIMARY KEY AUTOINCREMENT,"
@@ -253,6 +249,23 @@ class ProcessedDB:
" processed_at TEXT NOT NULL" " processed_at TEXT NOT NULL"
")" ")"
) )
else:
# 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)"
) )