"""Offline database replacement, run as the compose service's uid. The app container must be stopped. Only stdlib is used so this can be sent over stdin to either the old or the new image during a deployment. Two things to edit for your service: `DATABASE` and `REQUIRED_TABLES`. The table check is what stops a right-shaped-but-wrong database — last project's, or a half-downloaded one — from landing on top of a good one. """ from __future__ import annotations import gzip import os import shutil import sqlite3 import tempfile from contextlib import closing from datetime import UTC, datetime from pathlib import Path DATABASE = Path("/data/app.db") INCOMING = Path("/data/incoming.db.gz") BACKUPS = Path("/data/backups") #: Any one of these missing means this is not our database. Name tables that #: have existed since the first schema, not ones a recent migration added. REQUIRED_TABLES = {"items"} def replace_database(database: Path, archive: Path, backups: Path) -> Path | None: """Validate first, snapshot the stopped database, then atomically replace it.""" fd, name = tempfile.mkstemp(prefix=".incoming-", suffix=".db", dir=database.parent) staged = Path(name) backup = None try: with os.fdopen(fd, "wb") as output, gzip.open(archive, "rb") as source: shutil.copyfileobj(source, output) output.flush() os.fsync(output.fileno()) with closing(sqlite3.connect(staged)) as candidate: if candidate.execute("PRAGMA integrity_check").fetchall() != [("ok",)]: raise ValueError("Incoming database failed integrity_check") tables = {row[0] for row in candidate.execute( "SELECT name FROM sqlite_master WHERE type='table'")} if not REQUIRED_TABLES <= tables: raise ValueError("Incoming file is not this application's database") if database.exists(): backups.mkdir(parents=True, exist_ok=True) stamp = datetime.now(UTC).strftime("%Y%m%d-%H%M%S-%f") snapshot = backups / f"{database.stem}-{stamp}.db" backup = snapshot.with_suffix(".db.gz") with closing(sqlite3.connect(database)) as current: current.execute("VACUUM INTO ?", (str(snapshot),)) # Refuse a competing connection instead of removing a live WAL. busy, _, _ = current.execute("PRAGMA wal_checkpoint(TRUNCATE)").fetchone() if busy: raise RuntimeError("Database is busy; stop every writer before importing") # Keep the uncompressed recovery copy if compression fails. with snapshot.open("rb") as source, gzip.open(backup, "xb") as output: shutil.copyfileobj(source, output) with backup.open("r+b") as saved: os.fsync(saved.fileno()) snapshot.unlink() for suffix in ("-wal", "-shm"): Path(str(database) + suffix).unlink(missing_ok=True) os.replace(staged, database) return backup finally: staged.unlink(missing_ok=True) if __name__ == "__main__": saved = replace_database(DATABASE, INCOMING, BACKUPS) print(f"Database replaced; previous snapshot: {saved or 'none (first import)'}")