feat(db): WAL-safe seismo_relay.db snapshot helper

This commit is contained in:
serversdown committed 2026-07-02 06:22:46 +00:00
1 parent 25386cab8b
commit 31660d24e9
2 files changed
+71

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
"""
db_snapshot.py — WAL-safe snapshot / restore helpers for seismo_relay.db and
the waveform store, backing the /db/snapshot, /db/waveforms/recent.zip and
/db/restore endpoints in server.py. Kept out of server.py so the logic is
unit-testable without HTTP.
"""
from __future__ import annotations
import sqlite3
from pathlib import Path
def snapshot_db(src_path: Path, dest_path: Path) -> Path:
"""WAL-safe copy of a live SQLite DB via the SQLite online backup API.
Consistent even while the source is being written under WAL, and never
blocks the source's writers."""
src = sqlite3.connect(str(src_path))
dest = sqlite3.connect(str(dest_path))
try:
with dest:
src.backup(dest)
finally:
src.close()
dest.close()
return Path(dest_path)