diff --git a/core/database.py b/core/database.py index d19e89c..bff89c7 100644 --- a/core/database.py +++ b/core/database.py @@ -96,7 +96,7 @@ class NFOGuardDatabase: ) """) - # Add has_video_file columns if they don't exist (migration) + # Add missing columns if they don't exist (migration) try: cursor.execute("ALTER TABLE episodes ADD COLUMN has_video_file BOOLEAN DEFAULT FALSE") except sqlite3.OperationalError: @@ -109,6 +109,18 @@ class NFOGuardDatabase: # Column already exists pass + # Check if movies table has path column, add if missing + cursor.execute("PRAGMA table_info(movies)") + columns = [row[1] for row in cursor.fetchall()] + if 'path' not in columns: + cursor.execute("ALTER TABLE movies ADD COLUMN path TEXT") + # Update existing records with a placeholder path + cursor.execute("UPDATE movies SET path = '/unknown/path/' || imdb_id WHERE path IS NULL") + + # Make path NOT NULL after adding it + # Note: SQLite doesn't support ALTER COLUMN, so we'd need to recreate table + # For now, just ensure new records have paths + # Create indexes cursor.execute("CREATE INDEX IF NOT EXISTS idx_episodes_imdb ON episodes(imdb_id)") cursor.execute("CREATE INDEX IF NOT EXISTS idx_episodes_video ON episodes(has_video_file)") @@ -140,10 +152,20 @@ class NFOGuardDatabase: """Insert or update movie record""" with self.get_connection() as conn: cursor = conn.cursor() - cursor.execute(""" - INSERT OR REPLACE INTO movies (imdb_id, path, last_updated) - VALUES (?, ?, ?) - """, (imdb_id, path, datetime.utcnow().isoformat())) + try: + cursor.execute(""" + INSERT OR REPLACE INTO movies (imdb_id, path, last_updated) + VALUES (?, ?, ?) + """, (imdb_id, path, datetime.utcnow().isoformat())) + except sqlite3.OperationalError as e: + if "no column named path" in str(e): + # Fallback for databases without path column - just insert imdb_id + cursor.execute(""" + INSERT OR REPLACE INTO movies (imdb_id, last_updated) + VALUES (?, ?) + """, (imdb_id, datetime.utcnow().isoformat())) + else: + raise def upsert_movie_dates(self, imdb_id: str, released: Optional[str], dateadded: Optional[str], source: str, has_video_file: bool = False):