fix(storage): handle rollback failures in batch operations

Adds nested exception handling in add_files() and _migrate_fts_to_external()
to catch and log rollback failures. Uses exception chaining to preserve both
transaction and rollback errors, preventing silent database inconsistency.

Solution-ID: SOL-1735385400010
Issue-ID: ISS-1766921318981-10
Task-ID: T1
This commit is contained in:
catlog22
2025-12-29 19:08:49 +08:00
parent 76ab4d67fe
commit 3fdd52742b
2 changed files with 101 additions and 5 deletions

View File

@@ -330,8 +330,14 @@ class SQLiteStore:
)
conn.commit()
except Exception:
conn.rollback()
except Exception as exc:
try:
conn.rollback()
except Exception as rollback_exc:
logger.error(
"Rollback failed after add_files() error (%s): %s", exc, rollback_exc
)
raise exc.with_traceback(exc.__traceback__) from rollback_exc
raise
def remove_file(self, path: str | Path) -> bool:
@@ -619,11 +625,14 @@ class SQLiteStore:
conn.execute("INSERT INTO files_fts(files_fts) VALUES('rebuild')")
conn.execute("DROP TABLE files_fts_legacy")
conn.commit()
except sqlite3.DatabaseError:
except sqlite3.DatabaseError as exc:
try:
conn.rollback()
except Exception:
pass
except Exception as rollback_exc:
logger.error(
"Rollback failed during FTS schema migration (%s): %s", exc, rollback_exc
)
raise exc.with_traceback(exc.__traceback__) from rollback_exc
try:
conn.execute("DROP TABLE IF EXISTS files_fts")