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
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.
265 lines
18 KiB
Bash
265 lines
18 KiB
Bash
#!/usr/bin/env bash
|
|
# shellcheck disable=SC2016,SC2030,SC2031,SC2034,SC1091
|
|
# (single-quoted checks are eval'd, so their variables look unused; the
|
|
# subshells that source lib.sh change the environment on purpose)
|
|
# Integration tests for the Menivor Neon cutover kit (MG9).
|
|
#
|
|
# bash scripts/neon-cutover/test/cutover.test.sh
|
|
#
|
|
# Real Postgres, fake stack: a postgres:16-alpine container plays box 1's
|
|
# menivor-db-1 and a postgres:16-bookworm container plays Neon (a different libc
|
|
# collation, and a non-superuser database owner like Neon's). docker compose is a
|
|
# fake that records its calls; /healthz is a local HTTP server. Nothing outside
|
|
# the throwaway containers and a temp directory is touched.
|
|
# Needs docker; skips (exit 0) without it. NCUT_TEST_VERBOSE=1 prints outputs.
|
|
set -uo pipefail
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
KIT="$(cd "$HERE/.." && pwd)"
|
|
|
|
if ! docker info >/dev/null 2>&1; then
|
|
echo "skip: docker is not available"
|
|
exit 0
|
|
fi
|
|
|
|
# Under $HOME: Docker on macOS (Colima, Desktop) shares it with the VM by default.
|
|
mkdir -p "$HOME/.cache"
|
|
T="$(mktemp -d "$HOME/.cache/ncut-test.XXXXXX")"
|
|
NET="ncut-test-$$"
|
|
BOX="ncut-test-box-$$"
|
|
NEON="ncut-test-neon-$$"
|
|
HEALTH_PID=""
|
|
cleanup() {
|
|
docker rm -f "$BOX" "$NEON" >/dev/null 2>&1
|
|
docker network rm "$NET" >/dev/null 2>&1
|
|
[[ -n "$HEALTH_PID" ]] && kill "$HEALTH_PID" 2>/dev/null
|
|
rm -rf "$T"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
pass=0 fail=0
|
|
ok() { pass=$((pass + 1)); echo "ok $1"; }
|
|
bad() { fail=$((fail + 1)); echo "FAIL $1"; }
|
|
check() {
|
|
if eval "$2"; then ok "$1"; else
|
|
bad "$1"
|
|
[[ "${NCUT_TEST_VERBOSE:-0}" == 1 && -f "$T/out" ]] && sed 's/^/ | /' "$T/out"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# ---- containers ---------------------------------------------------------------
|
|
docker network create "$NET" >/dev/null
|
|
docker run -d --name "$BOX" --network "$NET" -e POSTGRES_USER=reelforge \
|
|
-e POSTGRES_PASSWORD=boxpw -e POSTGRES_DB=reelforge postgres:16-alpine >/dev/null
|
|
# Neon only takes TLS: the Debian image's snakeoil certificate stands in.
|
|
docker run -d --name "$NEON" --network "$NET" -e POSTGRES_PASSWORD=admin postgres:16-bookworm \
|
|
-c ssl=on -c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem \
|
|
-c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key >/dev/null
|
|
wait_pg() { for _ in $(seq 1 60); do docker exec "$1" pg_isready -U "$2" >/dev/null 2>&1 && return 0; sleep 1; done; return 1; }
|
|
if ! wait_pg "$BOX" reelforge || ! wait_pg "$NEON" postgres; then echo "containers did not start"; exit 1; fi
|
|
sleep 2
|
|
neon_admin() { docker exec -i "$NEON" psql -U postgres -v ON_ERROR_STOP=1 -qAt "$@"; }
|
|
neon_sql() { docker exec -i "$NEON" psql -U neondb_owner -d "${2:-neondb}" -v ON_ERROR_STOP=1 -qAt -c "$1"; }
|
|
box_sql() { docker exec -i "$BOX" psql -U reelforge -d "${2:-reelforge}" -v ON_ERROR_STOP=1 -qAt -c "$1"; }
|
|
neon_reset() {
|
|
neon_admin -c "SET client_min_messages = warning" -c "DROP DATABASE IF EXISTS neondb WITH (FORCE)" -c "DROP DATABASE IF EXISTS rehearsal WITH (FORCE)" \
|
|
-c "CREATE DATABASE neondb OWNER neondb_owner" -c "CREATE DATABASE rehearsal OWNER neondb_owner"
|
|
}
|
|
neon_admin -c "CREATE ROLE neondb_owner LOGIN PASSWORD 'npw' CREATEDB"
|
|
neon_reset
|
|
|
|
# A slice of the real schema: money tables with sequences, numeric, jsonb and
|
|
# timestamptz columns, plus a larger non-money table (count-only path).
|
|
box_sql "
|
|
CREATE TABLE alembic_version (version_num varchar(32) PRIMARY KEY);
|
|
INSERT INTO alembic_version VALUES ('c4e6a8b0d2f3');
|
|
CREATE TABLE users (id varchar(32) PRIMARY KEY, email varchar(255) UNIQUE NOT NULL, credits int NOT NULL DEFAULT 0);
|
|
CREATE TABLE credit_ledger (id bigserial PRIMARY KEY, user_id varchar(32) REFERENCES users(id),
|
|
delta int NOT NULL, reason varchar(80) NOT NULL, balance_after int NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now());
|
|
CREATE TABLE wallet_ledger_entries (id bigserial PRIMARY KEY, account varchar(40) NOT NULL,
|
|
amount numeric(18,6) NOT NULL, meta jsonb NOT NULL DEFAULT '{}', at timestamptz NOT NULL);
|
|
CREATE TABLE render_jobs (id serial PRIMARY KEY, title text, created_at timestamptz DEFAULT now());
|
|
INSERT INTO users SELECT 'u' || g, 'u' || g || '@example.com', g * 3 FROM generate_series(1, 200) g;
|
|
INSERT INTO credit_ledger (user_id, delta, reason, balance_after, created_at)
|
|
SELECT 'u' || (g % 200 + 1), (g % 9) - 4, 'grant', g, now() - (g || ' minutes')::interval FROM generate_series(1, 3000) g;
|
|
INSERT INTO wallet_ledger_entries (account, amount, meta, at)
|
|
SELECT 'acct-' || (g % 17), g / 7.0, jsonb_build_object('n', g, 'note', 'Zoë ' || g), now() - (g || ' s')::interval
|
|
FROM generate_series(1, 800) g;
|
|
INSERT INTO render_jobs (title) SELECT 'Reel ' || g FROM generate_series(1, 400) g;
|
|
"
|
|
|
|
# ---- fake stack -----------------------------------------------------------------
|
|
mkdir -p "$T/stack" "$T/state" "$T/bin" "$T/www"
|
|
: >"$T/stack/docker-compose.neon.yml"
|
|
cat >"$T/bin/fake-compose" <<EOS
|
|
#!/usr/bin/env bash
|
|
printf '%s\n' "\$*" >>"$T/compose.calls"
|
|
case "\$1" in version) echo "2.39.2" ;; esac
|
|
exit 0
|
|
EOS
|
|
chmod +x "$T/bin/fake-compose"
|
|
echo ok >"$T/www/healthz"
|
|
PORT=$((20000 + RANDOM % 20000))
|
|
(cd "$T/www" && exec python3 -m http.server "$PORT" --bind 127.0.0.1 >/dev/null 2>&1) &
|
|
HEALTH_PID=$!
|
|
for _ in $(seq 1 30); do curl -fs -o /dev/null "http://127.0.0.1:$PORT/healthz" && break; sleep 0.2; done
|
|
|
|
cat >"$T/menivor.env" <<EOS
|
|
# test env
|
|
MENIVOR_DB_PASSWORD=boxpw
|
|
REELFORGE_LAUNCH_MODE=internal_pilot
|
|
MENIVOR_NEON_DIRECT_URL=postgresql://neondb_owner:npw@$NEON:5432/neondb?sslmode=require
|
|
MENIVOR_NEON_DATABASE_URL=postgresql+psycopg://neondb_owner:npw@$NEON-pooler.test:5432/neondb?sslmode=require
|
|
MENIVOR_NEON_PROJECT_ID=proj-test-1
|
|
MENIVOR_NEON_REHEARSAL_URL=postgresql://neondb_owner:npw@$NEON:5432/rehearsal?sslmode=prefer
|
|
EOS
|
|
chmod 600 "$T/menivor.env"
|
|
|
|
drill_report() { # drill_report <file> <verdict> <run-stamp> <project>
|
|
jq -n --arg v "$2" --arg run "$3-123" --arg p "$4" \
|
|
'{verdict:$v, mode:"neon-branch", run:$run, notes:["project \($p), parent branch br-main",
|
|
"destructive write on the parent: DELETE (3 late rows first), 300s after T0", "branch counts: before=100/100 late=0/0"]}' >"$1"
|
|
}
|
|
NOW_STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
|
|
OLD_STAMP="$(date -u -d '-10 days' +%Y%m%dT%H%M%SZ 2>/dev/null || date -u -v-10d +%Y%m%dT%H%M%SZ)"
|
|
THREE_DAYS_AGO="$(date -u -d '-3 days' +%FT%TZ 2>/dev/null || date -u -v-3d +%FT%TZ)"
|
|
ONE_HOUR_AGO="$(date -u -d '-1 hour' +%FT%TZ 2>/dev/null || date -u -v-1H +%FT%TZ)"
|
|
drill_report "$T/drill-ok.json" PASS "$NOW_STAMP" proj-test-1
|
|
drill_report "$T/drill-old.json" PASS "$OLD_STAMP" proj-test-1
|
|
drill_report "$T/drill-other.json" PASS "$NOW_STAMP" proj-other
|
|
drill_report "$T/drill-fail.json" FAIL "$NOW_STAMP" proj-test-1
|
|
|
|
export MENIVOR_ENV_FILE="$T/menivor.env" MENIVOR_STACK="$T/stack" MENIVOR_DB_CONTAINER="$BOX" \
|
|
MENIVOR_HEALTH_URL="http://127.0.0.1:$PORT/healthz" NCUT_STATE_DIR="$T/state" \
|
|
NCUT_FLIPS_LOG="$T/origin-flips.log" NCUT_DOCKER_NETWORK="$NET" NCUT_COMPOSE="$T/bin/fake-compose" \
|
|
NCUT_SETTLE_S=0 NCUT_HEALTH_TIMEOUT_S=20 NCUT_FINGERPRINT_MAX_ROWS=300 \
|
|
NCUT_RESTORE_DRILL="$T/bin/no-drill" NCUT_DRILL_ENV="$T/no-drill.env"
|
|
|
|
run() { "$@" >"$T/out" 2>&1; }
|
|
env_line() { grep -E "^$1=" "$T/menivor.env" | tail -n 1 | cut -d= -f2-; }
|
|
latest_report() { find "$T/state" -maxdepth 1 -name "$1-*.json" -print | sort | tail -n 1; }
|
|
|
|
# ---- lib: env edits -------------------------------------------------------------
|
|
cp "$T/menivor.env" "$T/env-copy"
|
|
(
|
|
export MENIVOR_ENV_FILE="$T/env-copy"
|
|
# shellcheck source=../lib.sh
|
|
. "$KIT/lib.sh"
|
|
menivor_env_set NEW_KEY one test
|
|
menivor_env_set NEW_KEY two test
|
|
menivor_env_set REELFORGE_LAUNCH_MODE public test
|
|
)
|
|
check "env set appends a new key once and replaces it after" '[[ "$(grep -c "^NEW_KEY=" "$T/env-copy")" == 1 && "$(grep "^NEW_KEY=" "$T/env-copy")" == "NEW_KEY=two" ]]'
|
|
check "env set replaces in place and keeps the other lines" 'grep -q "^REELFORGE_LAUNCH_MODE=public" "$T/env-copy" && grep -q "^MENIVOR_DB_PASSWORD=boxpw" "$T/env-copy" && grep -q "^# test env" "$T/env-copy"'
|
|
check "env set keeps a backup of the previous file" 'ls "$T"/env-copy.bak-* >/dev/null 2>&1'
|
|
check "env set keeps the file mode" '[[ "$(stat -c %a "$T/env-copy" 2>/dev/null || stat -f %Lp "$T/env-copy")" == 600 ]]'
|
|
|
|
# ---- verify ---------------------------------------------------------------------
|
|
neon_sql "SELECT 1" rehearsal >/dev/null
|
|
run "$KIT/copy.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --work "$T/w-verify"
|
|
check "copy.sh fails without the URL in the environment" 'grep -q "is empty" "$T/out"'
|
|
export MENIVOR_NEON_REHEARSAL_URL="postgresql://neondb_owner:npw@$NEON:5432/rehearsal?sslmode=prefer"
|
|
run "$KIT/copy.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --work "$T/w-verify"
|
|
check "copy.sh restores into an empty database as a non-superuser owner" 'grep -q "^COPY OK" "$T/out"'
|
|
check "copy.sh leaves a checksum next to the dump" '[[ -s "$T/w-verify/menivor.dump.sha256" ]]'
|
|
check "copy.sh dump is private (0600)" '[[ "$(stat -c %a "$T/w-verify/menivor.dump" 2>/dev/null || stat -f %Lp "$T/w-verify/menivor.dump")" == 600 ]]'
|
|
run "$KIT/copy.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --work "$T/w-verify2"
|
|
check "copy.sh refuses a target that already has tables" 'grep -q "restore only into an empty database" "$T/out"'
|
|
run "$KIT/verify.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --report "$T/v1.json"
|
|
check "verify.sh passes on an identical copy (musl vs glibc collation)" '[[ "$(jq -r .verdict "$T/v1.json")" == PASS ]] && [[ "$(jq -r .tables "$T/v1.json")" == 5 ]]'
|
|
check "verify.sh fingerprints money tables above the size cap" '[[ "$(jq -r .fingerprinted_tables "$T/v1.json")" == 4 ]]'
|
|
check "verify.sh records per-table counts" '[[ "$(jq -r ".counts[\"public.credit_ledger\"].target" "$T/v1.json")" == 3000 ]]'
|
|
neon_sql "UPDATE wallet_ledger_entries SET amount = amount + 0.000001 WHERE id = 7" rehearsal
|
|
run "$KIT/verify.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --report "$T/v2.json"
|
|
rc=$?
|
|
check "verify.sh catches a one-row money change (exit 3)" '[[ $rc == 3 ]] && jq -r ".mismatches[]" "$T/v2.json" | grep -q "public.wallet_ledger_entries: row contents differ"'
|
|
neon_sql "DELETE FROM credit_ledger WHERE id = 5; SELECT setval('credit_ledger_id_seq', 10)" rehearsal >/dev/null
|
|
run "$KIT/verify.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --report "$T/v3.json"
|
|
check "verify.sh catches a missing row and a sequence behind the source" 'jq -r ".mismatches[]" "$T/v3.json" | grep -q "credit_ledger: source 3000 rows, target 2999" && jq -r ".mismatches[]" "$T/v3.json" | grep -q "sequence public.credit_ledger_id_seq: target 10 is behind"'
|
|
run "$KIT/verify.sh" --source "container:$BOX/reelforge" --target env:MENIVOR_NEON_REHEARSAL_URL --allow-drift --report "$T/v4.json"
|
|
check "verify.sh --allow-drift tolerates a smaller target but still checks sequences" '[[ "$(jq -r ".drift | length" "$T/v4.json")" == 1 ]] && jq -r ".mismatches[]" "$T/v4.json" | grep -q "sequence"'
|
|
neon_reset
|
|
|
|
# ---- preflight ------------------------------------------------------------------
|
|
unset MENIVOR_NEON_REHEARSAL_URL
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-ok.json"
|
|
check "preflight fails when the last origin flip is unknown" 'grep -q "last origin flip unknown" "$T/out"'
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-ok.json" --last-flip "$ONE_HOUR_AGO"
|
|
check "preflight fails when the last flip is under 48 h old" 'grep -q "FAIL: last origin flip 1 h ago" "$T/out"'
|
|
printf '%s\ts1.1-scribase-box2\n' "$ONE_HOUR_AGO" >"$T/origin-flips.log"
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-ok.json"
|
|
check "preflight reads the flip log" 'grep -q "FAIL: last origin flip 1 h ago" "$T/out"'
|
|
printf '%s\ts1.1-scribase-box2\n' "$THREE_DAYS_AGO" >"$T/origin-flips.log"
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-old.json"
|
|
check "preflight fails on a stale drill report" 'grep -q "days old" "$T/out"'
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-other.json"
|
|
check "preflight fails on a drill for another Neon project" 'grep -q "another Neon project" "$T/out"'
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-fail.json"
|
|
check "preflight fails on a FAIL drill" 'grep -q "need PASS/neon-branch" "$T/out"'
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-ok.json"
|
|
check "preflight passes with a fresh drill and a flip 3 days ago" 'grep -q "^CUTOVER PASS mode=preflight" "$T/out"'
|
|
check "preflight changes nothing" '[[ -z "$(env_line COMPOSE_FILE)" ]] && ! grep -q "^stop" "$T/compose.calls"'
|
|
check "preflight notes a direct (non-pooler) app URL only when it is one" '! grep -q "not a -pooler host" "$T/out"'
|
|
|
|
# ---- rehearsal ------------------------------------------------------------------
|
|
run "$KIT/cutover.sh" --rehearse
|
|
check "rehearsal copies the live database into the rehearsal branch" 'grep -q "^CUTOVER PASS mode=rehearse" "$T/out" && [[ "$(neon_sql "SELECT count(*) FROM credit_ledger" rehearsal)" == 3000 ]]'
|
|
check "rehearsal stops no writer and leaves the env file alone" '! grep -q "^stop" "$T/compose.calls" && [[ -z "$(env_line COMPOSE_FILE)" ]]'
|
|
check "rehearsal reports an expected downtime" '[[ "$(jq -r .expected_downtime_s "$(latest_report cutover)")" =~ ^[0-9]+$ ]]'
|
|
|
|
# ---- cutover ----------------------------------------------------------------------
|
|
BOX_WALLET_SUM="$(box_sql "SELECT sum(amount) FROM wallet_ledger_entries")"
|
|
: >"$T/compose.calls"
|
|
run "$KIT/cutover.sh" --yes --drill-report "$T/drill-ok.json"
|
|
R1="$(latest_report cutover)"
|
|
check "cutover passes" 'grep -q "^CUTOVER PASS mode=cutover" "$T/out"'
|
|
check "cutover stops every writer first" '[[ "$(head -n 2 "$T/compose.calls" | tail -n 1)" == "stop api worker housekeeping beat" ]]'
|
|
check "cutover runs migrate and starts the stack after the switch" 'grep -q "^run --rm migrate" "$T/compose.calls" && grep -q "^up -d$" "$T/compose.calls"'
|
|
check "cutover switches COMPOSE_FILE to the Neon override" '[[ "$(env_line COMPOSE_FILE)" == "docker-compose.yml:docker-compose.neon.yml" && "$(env_line MENIVOR_DB_TARGET)" == neon ]]'
|
|
check "cutover keeps the previous env file" 'ls "$T"/menivor.env.bak-*-to-neon >/dev/null 2>&1'
|
|
check "Neon holds every ledger row after cutover" '[[ "$(neon_sql "SELECT count(*) FROM credit_ledger")" == 3000 && "$(neon_sql "SELECT sum(amount) FROM wallet_ledger_entries")" == "$BOX_WALLET_SUM" ]]'
|
|
check "cutover records downtime, counts and the capacity line" '[[ "$(jq -r .downtime_s "$R1")" =~ ^[0-9]+$ && "$(jq -r .count_credit_ledger "$R1")" == 3000 && "$(jq -r .capacity_line "$R1")" == *"menivor-db-1 stopped"* ]]'
|
|
check "cutover stops the box database but keeps the container" '[[ "$(docker inspect -f "{{.State.Running}}" "$BOX")" == false ]]'
|
|
check "cutover appends to the origin-flip log" 'tail -n 1 "$T/origin-flips.log" | grep -q "menivor-db-neon-cutover"'
|
|
check "cutover reports the skipped post check honestly" '[[ "$(jq -r .post_drill "$R1")" == skipped ]]'
|
|
run "$KIT/cutover.sh" --preflight-only --drill-report "$T/drill-ok.json" --last-flip "$THREE_DAYS_AGO"
|
|
check "a second cutover is refused (already on Neon, Neon not empty)" 'grep -q "FAIL: stack still runs on box 1" "$T/out" && grep -q "FAIL: Neon database is empty" "$T/out"'
|
|
|
|
# ---- rollback, no writes since cutover --------------------------------------------
|
|
: >"$T/compose.calls"
|
|
run "$KIT/rollback.sh" --yes
|
|
R2="$(latest_report rollback)"
|
|
check "rollback with no new writes switches straight back" 'grep -q "^ROLLBACK PASS" "$T/out" && [[ "$(jq -r .path "$R2")" == switch ]]'
|
|
check "rollback restores COMPOSE_FILE to the box stack" '[[ "$(env_line COMPOSE_FILE)" == docker-compose.yml && "$(env_line MENIVOR_DB_TARGET)" == box ]]'
|
|
check "rollback starts the box database again" '[[ "$(docker inspect -f "{{.State.Running}}" "$BOX")" == true ]]'
|
|
check "rollback logs its flip" 'tail -n 1 "$T/origin-flips.log" | grep -q "menivor-db-rollback-to-box"'
|
|
|
|
# ---- rollback after writes on Neon ------------------------------------------------
|
|
( export MENIVOR_ENV_FILE="$T/menivor.env"; . "$KIT/lib.sh"; menivor_env_set COMPOSE_FILE "$NCUT_COMPOSE_FILES_NEON" test )
|
|
neon_sql "INSERT INTO credit_ledger (user_id, delta, reason, balance_after) VALUES ('u1', 50, 'purchase', 53)"
|
|
: >"$T/compose.calls"
|
|
run "$KIT/rollback.sh" --yes
|
|
check "rollback refuses to drop Neon writes without --copy-back" 'grep -q "rerun with --copy-back" "$T/out" && [[ "$(env_line COMPOSE_FILE)" == *neon* ]]'
|
|
check "a refused rollback restarts the writers on Neon" 'grep -q "^start api worker housekeeping beat" "$T/compose.calls"'
|
|
run "$KIT/rollback.sh" --yes --copy-back
|
|
R3="$(latest_report rollback)"
|
|
check "rollback --copy-back passes" 'grep -q "^ROLLBACK PASS" "$T/out" && [[ "$(jq -r .path "$R3")" == copy-back ]]'
|
|
check "copy-back brings the Neon-only purchase to box 1" '[[ "$(box_sql "SELECT count(*) FROM credit_ledger WHERE reason = '"'"'purchase'"'"'")" == 1 ]]'
|
|
KEPT="$(jq -r .kept_box_database "$R3")"
|
|
check "copy-back keeps the old box database under a new name" '[[ "$(box_sql "SELECT count(*) FROM credit_ledger" "$KEPT")" == 3000 ]]'
|
|
check "copy-back leaves Neon untouched" '[[ "$(neon_sql "SELECT count(*) FROM credit_ledger")" == 3001 ]]'
|
|
|
|
# ---- automatic switch back when the API does not come up on Neon ------------------
|
|
neon_reset
|
|
kill "$HEALTH_PID" 2>/dev/null; wait "$HEALTH_PID" 2>/dev/null; HEALTH_PID=""
|
|
: >"$T/compose.calls"
|
|
NCUT_HEALTH_TIMEOUT_S=3 run "$KIT/cutover.sh" --yes --drill-report "$T/drill-ok.json" --last-flip "$THREE_DAYS_AGO"
|
|
check "an unhealthy API on Neon fails the cutover" 'grep -q "^CUTOVER FAIL" "$T/out" && grep -q "not healthy on Neon" "$T/out"'
|
|
check "it switches the env file back to box 1" '[[ "$(env_line COMPOSE_FILE)" == docker-compose.yml && "$(env_line MENIVOR_DB_TARGET)" == box ]]'
|
|
check "it starts the stack on box 1 again" '[[ "$(tail -n 1 "$T/compose.calls")" == "up -d" && "$(docker inspect -f "{{.State.Running}}" "$BOX")" == true ]]'
|
|
|
|
echo "---"
|
|
echo "passed $pass, failed $fail"
|
|
((fail == 0))
|