feat(db): gated WAL-safe restore of seismo_relay.db + waveforms

This commit is contained in:
2026-07-02 06:35:24 +00:00
parent 916e6fcabb
commit 7edd0a1265
2 changed files with 108 additions and 0 deletions
+52
View File
@@ -79,3 +79,55 @@ def test_recent_waveforms_zip_bundles_all_per_event_files(tmp_path):
"BE111/BBBB2222.OE0H.a5.pkl",
"BE111/BBBB2222.OE0H.sfm.json",
}
def test_sfm_db_restore_enabled_reads_env(monkeypatch):
from sfm.db_snapshot import sfm_db_restore_enabled
monkeypatch.delenv("SFM_DB_RESTORE_ENABLED", raising=False)
assert sfm_db_restore_enabled() is False
monkeypatch.setenv("SFM_DB_RESTORE_ENABLED", "true")
assert sfm_db_restore_enabled() is True
monkeypatch.setenv("SFM_DB_RESTORE_ENABLED", "0")
assert sfm_db_restore_enabled() is False
def test_restore_swaps_db_and_unpacks_waveforms(tmp_path):
import zipfile
from sfm.db_snapshot import restore_db_and_waveforms
db_path = tmp_path / "seismo_relay.db"
_make_db(db_path, rows=1)
(tmp_path / "seismo_relay.db-wal").write_bytes(b"stale") # must be cleared
new_db = tmp_path / "incoming.db"
_make_db(new_db, rows=9)
wf_root = tmp_path / "waveforms"
wf_root.mkdir()
wf_zip = tmp_path / "wf.zip"
with zipfile.ZipFile(wf_zip, "w") as zf:
zf.writestr("BE111/AAAA.OE0H", b"x")
zf.writestr("BE111/AAAA.OE0H.h5", b"x")
result = restore_db_and_waveforms(db_path, wf_root, new_db, wf_zip)
conn = sqlite3.connect(str(db_path))
assert conn.execute("SELECT COUNT(*) FROM t").fetchone()[0] == 9
conn.close()
assert not (tmp_path / "seismo_relay.db-wal").exists()
assert (wf_root / "BE111" / "AAAA.OE0H.h5").exists()
assert result["ok"] and result["waveforms_unpacked"] == 2
assert result["safety_backup"] is not None
def test_restore_rejects_non_sqlite(tmp_path):
import pytest
from sfm.db_snapshot import restore_db_and_waveforms
db_path = tmp_path / "seismo_relay.db"
_make_db(db_path, rows=1)
bogus = tmp_path / "bogus.db"
bogus.write_bytes(b"not a sqlite file at all")
wf_root = tmp_path / "waveforms"
wf_root.mkdir()
with pytest.raises(ValueError):
restore_db_and_waveforms(db_path, wf_root, bogus, None)