"""Read the backup in a fresh directory without the source or its WAL files."""
from contextlib import closing
from pathlib import Path
import shutil
import sqlite3
import tempfile

backup = Path("sqlite-backup-demo/snapshot.sqlite").resolve(strict=True)
with tempfile.TemporaryDirectory(prefix="sqlite-restore-") as work:
    restored = Path(work) / "restored.sqlite"
    shutil.copyfile(backup, restored)
    with closing(sqlite3.connect(restored.as_uri() + "?mode=ro", uri=True,
                                 isolation_level=None)) as con:
        assert con.execute("PRAGMA quick_check").fetchall() == [("ok",)]
        assert con.execute("PRAGMA journal_mode").fetchone()[0] == "delete"
        rows = con.execute("SELECT id, status FROM jobs ORDER BY id").fetchall()
        assert rows == [(1, "done"), (2, "done")], rows
print("RESTORE OK: [(1, 'done'), (2, 'done')]")
