caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

164 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# verify.sh: prove two Menivor databases hold the same data (MG9).
#
# verify.sh --source <endpoint> --target <endpoint> [--report <file>] [--allow-drift]
#
# Checks, on both sides:
# * the same set of user tables;
# * the same row count in every table;
# * the same md5 over every row (rendered as text, sorted with COLLATE "C") of
# each money table in NCUT_MONEY_TABLES (credit ledger, wallet, payments,
# users, alembic_version...) and of every other table up to
# NCUT_FINGERPRINT_MAX_ROWS rows;
# * every sequence on the target at or past the source value, so the next
# ledger id can never collide with a copied one.
# Session settings (TimeZone, DateStyle, float digits...) are pinned in lib.sh so
# identical rows render identically on box Postgres and on Neon.
#
# --allow-drift is for rehearsals against a live source that keeps writing:
# counts may grow on the source, fingerprints are not compared, and only a
# missing table or a target count above the source fails.
#
# Exit: 0 PASS, 3 mismatch, 1 error. Endpoints: see lib.sh.
set -Eeuo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
. "$HERE/lib.sh"
# sort, comm and join must agree on one byte order.
export LC_ALL=C
usage() { sed -n '2,24p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 2; }
SRC="" TGT="" REPORT="" DRIFT=0
while (($#)); do
case "$1" in
--source) SRC="$2"; shift ;;
--target) TGT="$2"; shift ;;
--report) REPORT="$2"; shift ;;
--allow-drift) DRIFT=1 ;;
-h | --help) usage ;;
*) echo "unknown argument: $1" >&2; usage ;;
esac
shift
done
[[ -n "$SRC" && -n "$TGT" ]] || usage
ncut_endpoint_check "$SRC"
ncut_endpoint_check "$TGT"
[[ "$NCUT_FINGERPRINT_MAX_ROWS" =~ ^[0-9]+$ ]] || ncut_die "NCUT_FINGERPRINT_MAX_ROWS must be a number"
WORK="$(mktemp -d "${TMPDIR:-/tmp}/ncut-verify.XXXXXX")"
trap 'rm -rf "$WORK"' EXIT
# counts <endpoint> <out>: "schema.table<TAB>rows" for every user table, one call.
counts() {
local ep="$1" out="$2" t sql=""
ncut_user_tables "$ep" >"$WORK/tables"
while IFS= read -r t; do
[[ -n "$t" ]] || continue
sql+="SELECT $(sql_lit "$t"), count(*) FROM $(sql_ident "$t");"$'\n'
done <"$WORK/tables"
if [[ -z "$sql" ]]; then : >"$out"; return; fi
printf '%s' "$sql" | ncut_pg "$ep" psql -X -q -v ON_ERROR_STOP=1 -At -F $'\t' >"$out"
}
# fingerprints <endpoint> <tables-file> <out>: "schema.table<TAB>md5".
fingerprints() {
local ep="$1" list="$2" out="$3" t sql=""
while IFS= read -r t; do
[[ -n "$t" ]] || continue
sql+="SELECT $(sql_lit "$t"), coalesce(md5(string_agg(r.x, E'\\n' ORDER BY r.x COLLATE \"C\")), 'empty')"
sql+=" FROM (SELECT t::text AS x FROM $(sql_ident "$t") t) r;"$'\n'
done <"$list"
if [[ -z "$sql" ]]; then : >"$out"; return; fi
printf '%s' "$sql" | ncut_pg "$ep" psql -X -q -v ON_ERROR_STOP=1 -At -F $'\t' >"$out"
}
sequences() {
ncut_sql "$1" "SELECT schemaname || '.' || sequencename, coalesce(last_value, 0)
FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'restore_drill')
ORDER BY 1" >"$2"
}
# Identifiers come from pg_class, but quote them anyway: "schema"."table".
sql_ident() { local s="${1%%.*}" t="${1#*.}"; printf '"%s"."%s"' "${s//\"/\"\"}" "${t//\"/\"\"}"; }
sql_lit() { printf "'%s'" "${1//\'/\'\'}"; }
ncut_log "counting rows on the source"
counts "$SRC" "$WORK/src.counts"
ncut_log "counting rows on the target"
counts "$TGT" "$WORK/tgt.counts"
FAILS=()
DRIFTS=()
fail() { FAILS+=("$*"); ncut_log "MISMATCH: $*"; }
# Same table set.
cut -f1 "$WORK/src.counts" | sort >"$WORK/src.names"
cut -f1 "$WORK/tgt.counts" | sort >"$WORK/tgt.names"
while IFS= read -r t; do fail "$t: missing on target"; done < <(comm -23 "$WORK/src.names" "$WORK/tgt.names")
while IFS= read -r t; do fail "$t: only on target"; done < <(comm -13 "$WORK/src.names" "$WORK/tgt.names")
# Same counts.
join -t $'\t' <(sort "$WORK/src.counts") <(sort "$WORK/tgt.counts") >"$WORK/both.counts"
while IFS=$'\t' read -r t s g; do
if [[ "$s" == "$g" ]]; then continue; fi
if ((DRIFT)) && ((g <= s)); then
DRIFTS+=("$t: source $s, target $g")
else
fail "$t: source $s rows, target $g rows"
fi
done <"$WORK/both.counts"
# Fingerprints (exact mode only).
FP_TABLES=0
if ((!DRIFT)); then
: >"$WORK/fp.tables"
while IFS=$'\t' read -r t s g; do
base="${t#*.}"
if [[ " $NCUT_MONEY_TABLES " == *" $base "* ]] || ((s <= NCUT_FINGERPRINT_MAX_ROWS)); then
printf '%s\n' "$t" >>"$WORK/fp.tables"
fi
done <"$WORK/both.counts"
FP_TABLES="$(wc -l <"$WORK/fp.tables" | tr -d ' ')"
ncut_log "fingerprinting $FP_TABLES tables on both sides"
fingerprints "$SRC" "$WORK/fp.tables" "$WORK/src.fp"
fingerprints "$TGT" "$WORK/fp.tables" "$WORK/tgt.fp"
while IFS=$'\t' read -r t a b; do
[[ "$a" == "$b" ]] || fail "$t: row contents differ (md5 $a vs $b)"
done < <(join -t $'\t' <(sort "$WORK/src.fp") <(sort "$WORK/tgt.fp"))
fi
# Sequences.
sequences "$SRC" "$WORK/src.seq"
sequences "$TGT" "$WORK/tgt.seq"
while IFS= read -r s; do fail "sequence $s: missing on target"; done \
< <(comm -23 <(cut -f1 "$WORK/src.seq" | sort) <(cut -f1 "$WORK/tgt.seq" | sort))
while IFS=$'\t' read -r s a b; do
((b >= a)) || fail "sequence $s: target $b is behind source $a"
done < <(join -t $'\t' <(sort "$WORK/src.seq") <(sort "$WORK/tgt.seq"))
TABLES="$(wc -l <"$WORK/both.counts" | tr -d ' ')"
ROWS="$(awk -F'\t' '{n += $3} END {print n + 0}' "$WORK/both.counts")"
VERDICT=PASS
((${#FAILS[@]} == 0)) || VERDICT=FAIL
if [[ -n "$REPORT" ]]; then
jq -n --arg verdict "$VERDICT" --arg source "$SRC" --arg target "$TGT" \
--argjson tables "$TABLES" --argjson rows "$ROWS" --argjson fingerprinted "$FP_TABLES" \
--argjson drift "$DRIFT" \
--rawfile counts "$WORK/both.counts" \
--arg fails "$(printf '%s\n' "${FAILS[@]+"${FAILS[@]}"}")" \
--arg drifts "$(printf '%s\n' "${DRIFTS[@]+"${DRIFTS[@]}"}")" \
'{verdict:$verdict, source:$source, target:$target, tables:$tables, rows:$rows,
fingerprinted_tables:$fingerprinted, allow_drift:($drift == 1),
counts:($counts | split("\n") | map(select(length > 0) | split("\t")
| {(.[0]): {source: (.[1] | tonumber), target: (.[2] | tonumber)}}) | add // {}),
mismatches:($fails | split("\n") | map(select(length > 0))),
drift:($drifts | split("\n") | map(select(length > 0)))}' >"$REPORT"
fi
DRIFT_NOTE=""
((DRIFT)) && DRIFT_NOTE=" drift=${#DRIFTS[@]}"
echo "VERIFY $VERDICT tables=$TABLES rows=$ROWS fingerprinted=$FP_TABLES mismatches=${#FAILS[@]}$DRIFT_NOTE"
[[ "$VERDICT" == PASS ]] || exit 3