| 1 | #!/usr/bin/env bash |
| 2 | # The equality oracle: is a restored database correct relative to a source? |
| 3 | # |
| 4 | # db_equal.sh A <db1> <db2> # logical equality (default, version-robust) |
| 5 | # db_equal.sh B <db1> <db2> # physical equality (byte-identical main file) |
| 6 | # |
| 7 | # Oracle A: both pass PRAGMA integrity_check; identical sqlite_master schema; |
| 8 | # identical per-table content hash (rows ordered by every column). |
| 9 | # Oracle B: after a TRUNCATE checkpoint, the main DB files are byte-identical |
| 10 | # (only valid when both were produced by the same SQLite version). |
| 11 | # |
| 12 | # Exit 0 = equal; non-zero = not equal (reason printed to stderr). |
| 13 | set -euo pipefail |
| 14 | |
| 15 | mode="${1:?usage: db_equal.sh <A|B> <db1> <db2>}" |
| 16 | db1="${2:?missing db1}" |
| 17 | db2="${3:?missing db2}" |
| 18 | command -v sqlite3 >/dev/null || { echo "sqlite3 not on PATH" >&2; exit 2; } |
| 19 | |
| 20 | sha() { shasum -a 256 "$1" 2>/dev/null | awk '{print $1}'; } |
| 21 | |
| 22 | integrity() { |
| 23 | local out; out=$(sqlite3 "$1" 'PRAGMA integrity_check;' 2>&1 || true) |
| 24 | [ "$out" = "ok" ] || { echo "integrity_check failed for $1: $out" >&2; return 1; } |
| 25 | } |
| 26 | |
| 27 | schema_hash() { |
| 28 | sqlite3 "$1" \ |
| 29 | "SELECT type,name,tbl_name,sql FROM sqlite_master |
| 30 | WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name,tbl_name;" | shasum -a 256 | awk '{print $1}' |
| 31 | } |
| 32 | |
| 33 | # Hash of all user-table content, each table's rows ordered by every column so |
| 34 | # the digest is independent of physical row/page placement. |
| 35 | content_hash() { |
| 36 | local db="$1" t cols |
| 37 | : > /tmp/.dbeq_content.$$ |
| 38 | while IFS= read -r t; do |
| 39 | [ -z "$t" ] && continue |
| 40 | cols=$(sqlite3 "$db" "SELECT group_concat('\"'||name||'\"', ',') |
| 41 | FROM pragma_table_info('$t');") |
| 42 | { |
| 43 | echo "## TABLE $t" |
| 44 | sqlite3 -noheader -newline $'\n' "$db" \ |
| 45 | ".mode list" \ |
| 46 | "SELECT * FROM \"$t\" ORDER BY ${cols:-1};" |
| 47 | } >> /tmp/.dbeq_content.$$ |
| 48 | done < <(sqlite3 "$db" "SELECT name FROM sqlite_master |
| 49 | WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name;") |
| 50 | shasum -a 256 /tmp/.dbeq_content.$$ | awk '{print $1}' |
| 51 | rm -f /tmp/.dbeq_content.$$ |
| 52 | } |
| 53 | |
| 54 | case "$mode" in |
| 55 | A|a) |
| 56 | integrity "$db1"; integrity "$db2" |
| 57 | s1=$(schema_hash "$db1"); s2=$(schema_hash "$db2") |
| 58 | [ "$s1" = "$s2" ] || { echo "schema differs ($s1 != $s2)" >&2; exit 1; } |
| 59 | c1=$(content_hash "$db1"); c2=$(content_hash "$db2") |
| 60 | [ "$c1" = "$c2" ] || { echo "content differs ($c1 != $c2)" >&2; exit 1; } |
| 61 | echo "OK (Oracle A: schema=$s1 content=$c1)" |
| 62 | ;; |
| 63 | B|b) |
| 64 | for d in "$db1" "$db2"; do |
| 65 | sqlite3 "$d" 'PRAGMA wal_checkpoint(TRUNCATE);' >/dev/null 2>&1 || true |
| 66 | done |
| 67 | h1=$(sha "$db1"); h2=$(sha "$db2") |
| 68 | [ "$h1" = "$h2" ] || { echo "bytes differ ($h1 != $h2)" >&2; exit 1; } |
| 69 | echo "OK (Oracle B: sha256=$h1)" |
| 70 | ;; |
| 71 | *) echo "unknown mode '$mode' (want A or B)" >&2; exit 2 ;; |
| 72 | esac |