| 1 | #!/usr/bin/env bash |
| 2 | # Capture immutable golden fixtures from the real Litestream binary and sqlite3. |
| 3 | # Run this only to replace the complete committed tests/fixtures/golden/ set. |
| 4 | # |
| 5 | # These are reader fixtures: the celld-ltx parser must decode them and assert |
| 6 | # their fields. Writer byte-fidelity is verified by differential test D1, NOT by |
| 7 | # byte comparison — LTX headers embed timestamps, so bytes are not reproducible. |
| 8 | # |
| 9 | # Deterministic method: drive replication with `replicate -once` (no background |
| 10 | # timing/sleep races). Produces an L0-only tree because the round-trip property is |
| 11 | # what matters. The binary can be an official release or a build |
| 12 | # from the pinned tag (reference/litestream-go) — both are "the real v0.5.11". |
| 13 | set -euo pipefail |
| 14 | |
| 15 | REQ_VERSION="0.5.11" |
| 16 | OUT_DIR="tests/fixtures/golden" |
| 17 | |
| 18 | command -v litestream >/dev/null || { echo "litestream not on PATH"; exit 1; } |
| 19 | command -v sqlite3 >/dev/null || { echo "sqlite3 not on PATH"; exit 1; } |
| 20 | |
| 21 | ver=$(litestream version 2>/dev/null | tr -d 'v ' || true) |
| 22 | case "$ver" in |
| 23 | "$REQ_VERSION") : ;; # official release |
| 24 | *"development build"*|"") |
| 25 | echo "WARN: litestream reports '$ver' — assuming a from-source build of tag v$REQ_VERSION." ;; |
| 26 | *) echo "ERROR: need litestream $REQ_VERSION, found '$ver'"; exit 1 ;; |
| 27 | esac |
| 28 | |
| 29 | work=$(mktemp -d); trap 'rm -rf "$work"' EXIT |
| 30 | db="$work/state.db"; replica="$work/replica" |
| 31 | mkdir -p "$OUT_DIR" |
| 32 | |
| 33 | echo "== seed a deterministic SQL sequence (WAL mode) ==" |
| 34 | sqlite3 "$db" <<'SQL' |
| 35 | PRAGMA journal_mode=WAL; |
| 36 | CREATE TABLE kv (k TEXT PRIMARY KEY, v TEXT NOT NULL); |
| 37 | INSERT INTO kv VALUES ('a','1'),('b','2'),('c','3'); |
| 38 | SQL |
| 39 | |
| 40 | echo "== initial snapshot ==" |
| 41 | litestream replicate -once "$db" "file://$replica" >/dev/null |
| 42 | |
| 43 | echo "== 5 distinct transactions, each flushed (=> multiple L0 LTX files) ==" |
| 44 | for i in $(seq 1 5); do |
| 45 | sqlite3 "$db" "INSERT INTO kv VALUES ('k$i','v$i'); UPDATE kv SET v='upd$i' WHERE k='a';" |
| 46 | litestream replicate -once "$db" "file://$replica" >/dev/null |
| 47 | done |
| 48 | |
| 49 | echo "== verify the real binary restores this L0 tree (sanity) ==" |
| 50 | litestream restore -o "$work/restored.db" "file://$replica" >/dev/null |
| 51 | scripts/db_equal.sh A "$db" "$work/restored.db" |
| 52 | |
| 53 | echo "== capture replica tree ==" |
| 54 | rm -rf "$OUT_DIR/replica"; cp -R "$replica" "$OUT_DIR/replica" |
| 55 | |
| 56 | echo "== capture a raw SQLite WAL sample (pinned open so it isn't checkpointed) ==" |
| 57 | python3 - "$OUT_DIR" <<'PY' |
| 58 | import sqlite3, shutil, sys, os |
| 59 | gold = sys.argv[1]; work = "/tmp/.walgen"; os.makedirs(work, exist_ok=True) |
| 60 | db = os.path.join(work, "w.db") |
| 61 | for p in (db, db+"-wal", db+"-shm"): |
| 62 | if os.path.exists(p): os.remove(p) |
| 63 | c = sqlite3.connect(db) |
| 64 | c.execute("PRAGMA journal_mode=WAL"); c.execute("PRAGMA wal_autocheckpoint=0") |
| 65 | c.execute("CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT, n INTEGER)") |
| 66 | for i in range(1, 21): c.execute("INSERT INTO t(name,n) VALUES(?,?)", (f"row{i}", i*7)) |
| 67 | c.commit(); c.execute("UPDATE t SET n=n+1 WHERE id<=10"); c.commit() |
| 68 | pin = sqlite3.connect(db); pin.execute("BEGIN"); pin.execute("SELECT count(*) FROM t").fetchone() |
| 69 | shutil.copyfile(db+"-wal", os.path.join(gold, "sample.wal")) |
| 70 | pin.rollback(); pin.close(); c.close() |
| 71 | print("sample.wal:", os.path.getsize(os.path.join(gold, "sample.wal")), "bytes") |
| 72 | PY |
| 73 | |
| 74 | echo "Captured into $OUT_DIR — review, then commit. (See MANIFEST.md.)" |