Files
wownerogue/docs/FINANCIAL_CONSTRAINT_VALIDATION.md
jwinterm ed24d43b56 chore: prepare the repository for public release
Removes operator-private material from the shipped tree, corrects documentation
that had drifted from the code, deletes dead code, and applies a consistent
prose style.

Private material moved out of the public tree (docs/private/, git-ignored):
  - the wallet keyfile recovery runbook and its helper scripts, which together
    published the mainnet wallet file path, the service account, backup snapshot
    filenames, and the wallet RPC listener state. The helper duplicated the same
    constants, so relocating the document alone would not have removed them.
  - the stagenet parallel-candidate runbook and its fixtures.
  - the dated production-state record, the internal remediation plan, and the
    TODO list. None are reference documentation for an outside reader.

Redacted or genericised:
  - operator home-directory paths in shipped scripts and units. The asset build
    script now takes KENNEY_ASSETS_DIR instead of a hard-coded path.
  - private LAN addresses in tests, env examples and deploy docs, replaced with
    RFC 5737 documentation addresses.
  - the default alert sender and the notification hub example.
  - the Monero wallet unit now matches the Wownero one: a dedicated service
    account and a config file, instead of the operator's login account with the
    wallet password in argv where any local user can read it from /proc.

Ignore rules now cover every .env variant rather than the bare name, plus local
agent state. The deployment docs instruct operators to create .env.mainnet and
.env.stagenet, which the exact-name rule would have allowed to be committed.

Documentation corrected against the code: per-chain dungeon level counts, the
premium unlock rule, Tavern chat scope, backup retention, the chain-profile
fallback, the environment template location, and several sections describing
shipped work as pending.

Dead code removed: configPersistence, rpccalls, a duplicate vendored ROT.js
bundle, the unused tile-picker overlay, the unreachable legacy game-size path
and its vestigial parameter, spectator stubs that shadowed the real
SpectatorManager, and commented-out debugging. nip98 now imports the shared
nostr loader rather than duplicating it.

Prose style: em-dashes removed throughout in favour of colons, semicolons and
commas; comments restated as present-tense descriptions of the code rather than
narration of past changes.

127 suites / 1015 tests pass.
2026-07-27 06:40:38 -04:00

4.2 KiB

Historical financial constraint release gate

Several migrations add CHECK ... NOT VALID. PostgreSQL enforces those constraints for every new and changed row, but it does not scan rows that already existed when the constraint was added. A complete schema_migrations ledger therefore does not, by itself, prove that historical financial rows obey the current constraints.

Two repository-owned gates close that evidence gap without changing a live database. Both cover the same 18 constraints across payments, payouts, games, matches, match_queue_entries, race_entry_lots, and payment_refunds.

1. Read-only historical audit

scripts/deploy/financial-constraint-audit.sql checks the exact connected database name, verifies the complete 18-constraint catalog inventory, and reports a named row count for every historical predicate. It runs in one repeatable-read, read-only transaction, takes no explicit table lock, and rolls back. Connection and credential handling stay operator-specific; keep database passwords out of command history.

psql -X -v ON_ERROR_STOP=1 \
  --set=expected_database=EXACT_DATABASE_NAME \
  --file=scripts/deploy/financial-constraint-audit.sql

Exit codes:

Code Meaning
0 Every required constraint exists and every historical row satisfies it.
2 expected_database was not supplied, or the connected database does not match it exactly.
3 A required constraint is missing, or at least one predicate has a nonzero violation count.

The violation predicates mirror PostgreSQL CHECK semantics: only FALSE counts as a violation, since CHECK accepts NULL. Reconcile any violation with a separately reviewed, backup-backed repair. The audit has no repair mode by design.

This check is safe against production and should also run against every restored release candidate database. It reports how many of the constraints are still catalog-marked unvalidated; that count is informational, because the second gate exercises PostgreSQL's own validator.

2. Explicit validation on a disposable restore

scripts/deploy/financial-constraint-validate-restore.sql includes the read-only audit, then issues an explicit ALTER TABLE ... VALIDATE CONSTRAINT for all 18 constraints inside a transaction. It confirms pg_constraint.convalidated for each one and rolls the whole transaction back, so no catalog flag changes durably.

VALIDATE CONSTRAINT still scans tables, takes locks, and transiently updates PostgreSQL catalogs. The script therefore refuses to reach BEGIN unless all three conditions hold:

  • the connected database name exactly equals expected_database;
  • that name contains restore or scratch;
  • the exact disposable-restore confirmation string is supplied.

Run it only against a freshly restored, isolated database with no application traffic:

psql -X -v ON_ERROR_STOP=1 \
  --set=expected_database=monerogue_release_restore \
  --set=confirm_disposable=I_CONFIRM_THIS_DATABASE_IS_A_DISPOSABLE_RESTORE \
  --file=scripts/deploy/financial-constraint-validate-restore.sql

Both scripts set statement_timeout to 10 minutes and lock_timeout to 5 seconds for the duration of their transaction.

A pass proves both the explicit historical predicates and PostgreSQL's native validation path on that restored data. It does not mark live constraints validated. Making a live VALIDATE CONSTRAINT durable is a separate operation that needs its own reviewed maintenance plan, backup and restore proof, lock budget, and rollback decision. Do not fold that mutation into application startup.

Static coverage

A focused test inventories every migration occurrence of NOT VALID and fails if either gate omits a constraint or if the inventory size drifts from 18:

npm --prefix src test -- --runTestsByPath ../test/financialConstraintValidation.test.js

The test also asserts that the audit script contains no ALTER TABLE and no write statement, so the read-only gate cannot silently gain a mutation.

Adding another NOT VALID constraint requires three matching updates: its exact historical predicate and inventory entry in the audit script, its VALIDATE CONSTRAINT statement and inventory entry in the restore script, and its table and name in the test's constraint list.