db improvments
Local Docker Build (Dev) / build-dev (push) Successful in 4s

This commit is contained in:
2025-10-17 21:18:37 -04:00
parent d552d97440
commit 95945f7189
2 changed files with 26 additions and 9 deletions
+21 -8
View File
@@ -79,6 +79,15 @@ class NFOGuardDatabase:
self._local.connection.row_factory = sqlite3.Row
return self._local.connection
def _get_first_value(self, row):
"""Get first value from row, handling both dict and tuple types"""
if self.db_type == "postgresql":
# RealDictCursor returns dict-like objects
return list(row.values())[0] if row else None
else:
# SQLite returns tuples
return row[0] if row else None
@contextmanager
def get_connection(self):
"""Context manager for database connections"""
@@ -141,7 +150,7 @@ class NFOGuardDatabase:
dateadded TIMESTAMP,
source VARCHAR(100),
last_updated TIMESTAMP NOT NULL,
has_video_file INTEGER DEFAULT 0
has_video_file BOOLEAN DEFAULT FALSE
)
""")
@@ -427,29 +436,33 @@ class NFOGuardDatabase:
def get_stats(self) -> Dict[str, Any]:
"""Get database statistics"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Use regular cursor for simple COUNT queries (not RealDictCursor)
if self.db_type == "postgresql":
cursor = conn.cursor() # Regular cursor for PostgreSQL
else:
cursor = conn.cursor() # SQLite cursor
# Series stats
cursor.execute("SELECT COUNT(*) FROM series")
series_count = cursor.fetchone()[0]
series_count = self._get_first_value(cursor.fetchone())
# Episode stats
cursor.execute("SELECT COUNT(*) FROM episodes")
episodes_total = cursor.fetchone()[0]
episodes_total = self._get_first_value(cursor.fetchone())
cursor.execute("SELECT COUNT(*) FROM episodes WHERE has_video_file = TRUE")
episodes_with_video = cursor.fetchone()[0]
episodes_with_video = self._get_first_value(cursor.fetchone())
# Movie stats
cursor.execute("SELECT COUNT(*) FROM movies")
movies_total = cursor.fetchone()[0]
movies_total = self._get_first_value(cursor.fetchone())
cursor.execute("SELECT COUNT(*) FROM movies WHERE has_video_file = TRUE")
movies_with_video = cursor.fetchone()[0]
movies_with_video = self._get_first_value(cursor.fetchone())
# Processing history
cursor.execute("SELECT COUNT(*) FROM processing_history")
history_count = cursor.fetchone()[0]
history_count = self._get_first_value(cursor.fetchone())
return {
"series_count": series_count,