소스 검색

fix: improve SQLite table size retrieval with error handling and logging

Manuel 3 달 전
부모
커밋
2dff01fe83
1개의 변경된 파일과 16개의 추가작업 그리고 4개의 파일을 삭제
  1. 16 4
      services/database/admin_helpers.py

+ 16 - 4
services/database/admin_helpers.py

@@ -1,5 +1,6 @@
 import os
 
+from loguru import logger
 from sqlalchemy import MetaData, Table, func, select, text
 
 
@@ -96,10 +97,21 @@ def get_all_tables_stats(session, engine, db_type):
 
     if db_type == "SQLITE":
         # Batch-fetch all table sizes in one query
-        size_rows = session.execute(
-            text("SELECT name, COALESCE(SUM(pgsize), 0) FROM dbstat GROUP BY name")
-        ).fetchall()
-        sizes = {row[0]: int(row[1]) for row in size_rows}
+        try:
+            size_rows = session.execute(
+                text("SELECT name, COALESCE(SUM(pgsize), 0) FROM dbstat GROUP BY name")
+            ).fetchall()
+            sizes = {row[0]: int(row[1]) for row in size_rows}
+        except Exception as exc:
+            # dbstat is an optional SQLite virtual table.  Some builds do not
+            # include it; table row counts should still be available to the
+            # admin UI, with size shown as zero when it cannot be measured.
+            session.rollback()
+            logger.warning(
+                "SQLite dbstat is unavailable; table sizes will be reported as 0: {}",
+                exc,
+            )
+            sizes = {}
 
         # Reflect all tables once and run COUNT(*) per table
         metadata = MetaData()