Bring in pipeline, auth, deploy and docs from the boilerplate
roledesk is built on the boilerplate from here on: pipeline 2.0 (queue and scheduler), auth (one password, sessions, passkeys), the deploy kit and its docs, plus the coupling and packaging tests. Downstream copies, unedited. Refreshed from boilerplate/d56949f7d0f6c0bfe5745478e09007bfad67ae87 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1f436104f3
commit
9d666d0937
76 files changed
+15235
No files matched your search
@@ -0,0 +1,77 @@
|
||||
"""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)'}")
|
||||
Reference in new issue
Block a user