update db
Local Docker Build (Dev) / build-dev (push) Successful in 6s

This commit is contained in:
2025-11-03 15:05:43 -05:00
parent d078decbb4
commit 2d5688d313
4 changed files with 40 additions and 13 deletions
+27 -7
View File
@@ -108,6 +108,8 @@ class NFOGuardDatabase:
cursor.execute("""
CREATE TABLE IF NOT EXISTS movies (
imdb_id VARCHAR(20) PRIMARY KEY,
title TEXT,
year INTEGER,
path TEXT NOT NULL,
released DATE,
dateadded TIMESTAMP,
@@ -116,6 +118,21 @@ class NFOGuardDatabase:
has_video_file BOOLEAN DEFAULT FALSE
)
""")
# Add title and year columns if they don't exist (migration for existing databases)
cursor.execute("""
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name='movies' AND column_name='title') THEN
ALTER TABLE movies ADD COLUMN title TEXT;
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name='movies' AND column_name='year') THEN
ALTER TABLE movies ADD COLUMN year INTEGER;
END IF;
END $$;
""")
# Processing history table
cursor.execute("""
@@ -254,26 +271,29 @@ class NFOGuardDatabase:
last_updated = EXCLUDED.last_updated
""", (imdb_id, path, timestamp))
def upsert_movie_dates(self, imdb_id: str, released: Optional[str],
dateadded: Optional[str], source: str, has_video_file: bool = False):
def upsert_movie_dates(self, imdb_id: str, released: Optional[str],
dateadded: Optional[str], source: str, has_video_file: bool = False,
title: Optional[str] = None, year: Optional[int] = None):
"""Insert or update movie date record"""
import os
if os.environ.get("DEBUG", "false").lower() == "true":
print(f"🔍 DATABASE UPSERT: imdb_id={imdb_id}, dateadded={dateadded}, source={source}")
print(f"🔍 DATABASE UPSERT: imdb_id={imdb_id}, title={title}, dateadded={dateadded}, source={source}")
with self.get_connection() as conn:
cursor = conn.cursor()
timestamp = datetime.utcnow()
cursor.execute("""
INSERT INTO movies (imdb_id, path, released, dateadded, source, has_video_file, last_updated)
VALUES (%s, COALESCE((SELECT path FROM movies WHERE imdb_id = %s), 'unknown'), %s, %s, %s, %s, %s)
INSERT INTO movies (imdb_id, title, year, path, released, dateadded, source, has_video_file, last_updated)
VALUES (%s, %s, %s, COALESCE((SELECT path FROM movies WHERE imdb_id = %s), 'unknown'), %s, %s, %s, %s, %s)
ON CONFLICT (imdb_id) DO UPDATE SET
title = EXCLUDED.title,
year = EXCLUDED.year,
released = EXCLUDED.released,
dateadded = EXCLUDED.dateadded,
source = EXCLUDED.source,
has_video_file = EXCLUDED.has_video_file,
last_updated = EXCLUDED.last_updated
""", (imdb_id, imdb_id, released, dateadded, source, has_video_file, timestamp))
""", (imdb_id, title, year, imdb_id, released, dateadded, source, has_video_file, timestamp))
# Debug: Check what was actually saved
cursor.execute("SELECT dateadded, source FROM movies WHERE imdb_id = %s", (imdb_id,))
+8 -3
View File
@@ -131,10 +131,15 @@ class DatabasePopulator:
stats['skipped'] += 1
continue
# Insert into database
self.db.upsert_movie_dates(imdb_id, released, dateadded, source, has_video_file=True)
# Insert into database with title and year
title = movie.get('title')
year = movie.get('year')
self.db.upsert_movie_dates(
imdb_id, released, dateadded, source,
has_video_file=True, title=title, year=year
)
stats['added'] += 1
_log("DEBUG", f"Added movie {imdb_id}: {movie.get('title')} (source: {source})")
_log("DEBUG", f"Added movie {imdb_id}: {title} ({year}) (source: {source})")
except Exception as e:
_log("ERROR", f"Error processing movie {movie.get('title', 'unknown')}: {e}")