Diffinery

homeflare/celldpublic

celld/crates/ltx/scripts/capture-golden.sh
74 lines · 3.3 KB · executableRaw History
1#!/usr/bin/env bash
2# Capture IMMUTABLE golden fixtures from the REAL litestream binary + sqlite3.
3# (PLAN.md §6.2, AGENTS.md rule 3.) Run ONCE; then commit tests/fixtures/golden/.
4#
5# These are READER fixtures: rustyriver's 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 (the round-trip property is what
11# matters, see OPEN_QUESTIONS U-2). The binary may be an official release OR built
12# from the pinned tag (reference/litestream-go) — both are "the real v0.5.11".
13set -euo pipefail
14
15REQ_VERSION="0.5.11" # PLAN.md D-2
16OUT_DIR="tests/fixtures/golden"
17
18command -v litestream >/dev/null || { echo "litestream not on PATH"; exit 1; }
19command -v sqlite3 >/dev/null || { echo "sqlite3 not on PATH"; exit 1; }
20
21ver=$(litestream version 2>/dev/null | tr -d 'v ' || true)
22case "$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 ;;
27esac
28
29work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
30db="$work/state.db"; replica="$work/replica"
31mkdir -p "$OUT_DIR"
32
33echo "== seed a deterministic SQL sequence (WAL mode) =="
34sqlite3 "$db" <<'SQL'
35PRAGMA journal_mode=WAL;
36CREATE TABLE kv (k TEXT PRIMARY KEY, v TEXT NOT NULL);
37INSERT INTO kv VALUES ('a','1'),('b','2'),('c','3');
38SQL
39
40echo "== initial snapshot =="
41litestream replicate -once "$db" "file://$replica" >/dev/null
42
43echo "== 5 distinct transactions, each flushed (=> multiple L0 LTX files) =="
44for 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
47done
48
49echo "== verify the real binary restores this L0 tree (sanity) =="
50litestream restore -o "$work/restored.db" "file://$replica" >/dev/null
51scripts/db_equal.sh A "$db" "$work/restored.db"
52
53echo "== capture replica tree =="
54rm -rf "$OUT_DIR/replica"; cp -R "$replica" "$OUT_DIR/replica"
55
56echo "== capture a raw SQLite WAL sample (pinned open so it isn't checkpointed) =="
57python3 - "$OUT_DIR" <<'PY'
58import sqlite3, shutil, sys, os
59gold = sys.argv[1]; work = "/tmp/.walgen"; os.makedirs(work, exist_ok=True)
60db = os.path.join(work, "w.db")
61for p in (db, db+"-wal", db+"-shm"):
62 if os.path.exists(p): os.remove(p)
63c = sqlite3.connect(db)
64c.execute("PRAGMA journal_mode=WAL"); c.execute("PRAGMA wal_autocheckpoint=0")
65c.execute("CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT, n INTEGER)")
66for i in range(1, 21): c.execute("INSERT INTO t(name,n) VALUES(?,?)", (f"row{i}", i*7))
67c.commit(); c.execute("UPDATE t SET n=n+1 WHERE id<=10"); c.commit()
68pin = sqlite3.connect(db); pin.execute("BEGIN"); pin.execute("SELECT count(*) FROM t").fetchone()
69shutil.copyfile(db+"-wal", os.path.join(gold, "sample.wal"))
70pin.rollback(); pin.close(); c.close()
71print("sample.wal:", os.path.getsize(os.path.join(gold, "sample.wal")), "bytes")
72PY
73
74echo "Captured into $OUT_DIR — review, then commit. (See MANIFEST.md.)"