feat(backup): snapshot SQLite cohérent et contrôle d'intégrité (#89)

Implémente les trois fonctions de gestion de snapshots SQLite:
- make_snapshot: crée un snapshot gzippé cohérent via sqlite3.Connection.backup()
- write_snapshot: écrit le snapshot décompressé à destination
- verify_integrity: valide l'intégrité d'une base avec PRAGMA integrity_check

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Colin Maudry
2026-06-24 17:10:22 +02:00
parent 2a691e37b7
commit efefc3f5b0
2 changed files with 76 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
import gzip
import sqlite3
import tempfile
from pathlib import Path
def make_snapshot(db_path: Path) -> bytes:
with tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) as tmp:
tmp_path = Path(tmp.name)
try:
src = sqlite3.connect(str(db_path))
try:
dst = sqlite3.connect(str(tmp_path))
try:
src.backup(dst)
finally:
dst.close()
finally:
src.close()
return gzip.compress(tmp_path.read_bytes())
finally:
tmp_path.unlink(missing_ok=True)
def write_snapshot(gzipped: bytes, dest: Path) -> None:
dest.write_bytes(gzip.decompress(gzipped))
def verify_integrity(db_path: Path) -> bool:
try:
conn = sqlite3.connect(str(db_path))
except sqlite3.DatabaseError:
return False
try:
row = conn.execute("PRAGMA integrity_check").fetchone()
return row is not None and row[0] == "ok"
except sqlite3.DatabaseError:
return False
finally:
conn.close()