#!/bin/bash # Regenerate the sqlite3 interop traces for ocaml-sqlite's query engine. # # Requires: the sqlite3 CLI on PATH (the independent oracle). # Usage: dune build @traces (or run this script directly) set -euo pipefail command -v sqlite3 >/dev/null || { echo "error: sqlite3 not on PATH" >&2 exit 1 } SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" mkdir -p "$SCRIPT_DIR/../traces/expected" TRACE_DIR="$(cd "${1:-$SCRIPT_DIR/../traces}" && pwd)" DB="$TRACE_DIR/catalog.db" echo "sqlite3: $(sqlite3 --version | cut -d' ' -f1)" echo "traces: $TRACE_DIR" # Build the catalog database from the committed schema + data. rm -f "$DB" "$DB-journal" "$DB-wal" sqlite3 "$DB" <"$SCRIPT_DIR/fixtures.sql" # Capture sqlite3's output for each query (list mode, '|' separator) -- this is # the oracle our engine is checked against. for q in "$SCRIPT_DIR"/queries/*.sql; do name="$(basename "$q" .sql)" sqlite3 -batch -list -separator '|' "$DB" <"$q" >"$TRACE_DIR/expected/$name.txt" echo " $name" done # Storage-layout scenarios: a second database whose tables are mostly WITHOUT # ROWID, which sqlite3 stores in index b-trees rather than rowid table b-trees # (file format 2.4). Same oracle, same '|' list mode. WRDB="$TRACE_DIR/without_rowid.db" mkdir -p "$TRACE_DIR/without_rowid_expected" rm -f "$WRDB" "$WRDB-journal" "$WRDB-wal" sqlite3 "$WRDB" <"$SCRIPT_DIR/without_rowid.sql" for q in "$SCRIPT_DIR"/without_rowid_queries/*.sql; do name="$(basename "$q" .sql)" sqlite3 -batch -list -separator '|' "$WRDB" <"$q" \ >"$TRACE_DIR/without_rowid_expected/$name.txt" echo " without_rowid: $name" done # Write-path scenarios: each mutations//setup.sql runs against a fresh # database, then verify.sql dumps the final table state -- the oracle our # Sqlite.Raw.exec is checked against. The scratch DB is not committed. mkdir -p "$TRACE_DIR/mutations_expected" for d in "$SCRIPT_DIR"/mutations/*/; do name="$(basename "$d")" mdb="$TRACE_DIR/scratch.db" rm -f "$mdb" "$mdb-journal" "$mdb-wal" sqlite3 "$mdb" <"$d/setup.sql" sqlite3 -batch -list -separator '|' "$mdb" <"$d/verify.sql" \ >"$TRACE_DIR/mutations_expected/$name.txt" echo " mutation: $name" done rm -f "$TRACE_DIR/scratch.db" "$TRACE_DIR/scratch.db-journal" \ "$TRACE_DIR/scratch.db-wal" # Integrity guard: a file ocaml-sqlite writes must pass sqlite3's strict # PRAGMA integrity_check, including after deletes -- the B-tree layout checks # our own (lenient) reader skips, which is exactly what masked the index # divider / B+tree / freeblock / empty-child bugs. Build each scenario with the # OCaml `sql` CLI, then validate with sqlite3; a malformed file fails regen. # The dune rule passes the built CLI in SQL, because a nested `dune build` from # inside a dune action resolves its root to _build/default -- a partial tree with # no package scopes -- and would in any case contend for the build lock. Fall # back to building it when this script is run by hand from a checkout. if [ -n "${SQL:-}" ]; then # dune hands it to us relative to the build context, and the loops below run # from a scratch directory, so resolve it now while the cwd is still right. SQL="$(cd "$(dirname "$SQL")" && pwd)/$(basename "$SQL")" else ROOT="$(cd "$SCRIPT_DIR/../../../../.." && pwd)" (cd "$ROOT" && dune build ocaml-sqlite/bin/sql.exe) SQL="$ROOT/_build/default/ocaml-sqlite/bin/sql.exe" fi IWORK="$(mktemp -d)" for spec in "prefix:1:300" "suffix:301:600" "middle:200:450" "scatter:even" \ "all:1:600"; do name="${spec%%:*}" rest="${spec#*:}" lo="${rest%%:*}" hi="${rest##*:}" { echo "CREATE TABLE t (id INTEGER PRIMARY KEY, k TEXT UNIQUE);" for i in $(seq 1 600); do printf "INSERT INTO t VALUES (%d,'k%05d');\n" "$i" "$i"; done if [ "$lo" = even ]; then for i in $(seq 2 2 600); do printf "DELETE FROM t WHERE id=%d;\n" "$i"; done else for i in $(seq "$lo" "$hi"); do printf "DELETE FROM t WHERE id=%d;\n" "$i"; done fi } >"$IWORK/in.sql" (cd "$IWORK" && rm -f g.db g.db-wal && "$SQL" script g.db in.sql >/dev/null) res="$(sqlite3 "$IWORK/g.db" 'PRAGMA integrity_check;' | head -1)" if [ "$res" != "ok" ]; then echo "integrity guard FAILED ($name): $res" >&2 exit 1 fi echo " integrity: $name ok" done # Multi-page schema: enough CREATE TABLE/INDEX rows spill sqlite_master past # page 1 (an interior root at offset 100). sqlite3 must read the whole schema # and the file must pass integrity_check. { for i in $(seq 1 12); do printf "CREATE TABLE queue_%02d (id INTEGER PRIMARY KEY, name TEXT NOT NULL, state TEXT NOT NULL, priority INTEGER, submitted_at TEXT, started_at TEXT, finished_at TEXT, worker TEXT, retries INTEGER, payload BLOB);\n" "$i" printf "CREATE INDEX queue_%02d_by_state ON queue_%02d (state);\n" "$i" "$i" printf "CREATE INDEX queue_%02d_by_worker ON queue_%02d (worker);\n" "$i" "$i" printf "INSERT INTO queue_%02d (name, state) VALUES ('job-%02d', 'queued');\n" "$i" "$i" done } >"$IWORK/in.sql" (cd "$IWORK" && rm -f g.db g.db-wal && "$SQL" script g.db in.sql >/dev/null) res="$(sqlite3 "$IWORK/g.db" 'PRAGMA integrity_check;' | head -1)" if [ "$res" != "ok" ]; then echo "integrity guard FAILED (master-spill): $res" >&2 exit 1 fi # 12 tables and their 24 indexes, and nothing else: a database this engine # lays out holds no schema row the script did not ask for. rows="$(sqlite3 "$IWORK/g.db" 'SELECT count(*) FROM sqlite_master;')" if [ "$rows" != 36 ]; then echo "integrity guard FAILED (master-spill): sqlite3 sees $rows of 36 schema rows" >&2 exit 1 fi echo " integrity: master-spill ok" # Index overflow churn: UNIQUE keys past the index max_local (~1 KB at 4 KiB # pages) spill to overflow chains; inserts split leaves and interiors around # them and deletes replace separators. The file must pass integrity_check, # which also reports leaked pages as "never used". pad="$(printf 'k%.0s' $(seq 1 1500))" { echo "CREATE TABLE items (k TEXT UNIQUE, v INTEGER);" for i in $(seq 1 30); do printf "INSERT INTO items VALUES ('key-%03d-%s', %d);\n" "$i" "$pad" "$i"; done for i in $(seq 2 2 30); do printf "DELETE FROM items WHERE v=%d;\n" "$i"; done } >"$IWORK/in.sql" (cd "$IWORK" && rm -f g.db g.db-wal && "$SQL" script g.db in.sql >/dev/null) res="$(sqlite3 "$IWORK/g.db" 'PRAGMA integrity_check;' | head -1)" if [ "$res" != "ok" ]; then echo "integrity guard FAILED (index-overflow): $res" >&2 exit 1 fi rows="$(sqlite3 "$IWORK/g.db" 'SELECT count(*) FROM items;')" if [ "$rows" != 15 ]; then echo "integrity guard FAILED (index-overflow): sqlite3 sees $rows of 15 rows" >&2 exit 1 fi echo " integrity: index-overflow ok" rm -rf "$IWORK" echo "done. review with: git diff $TRACE_DIR"