www_site/services/api/tests/provision_isolated_env.sh
flym 8657f447a1 fix(api): make quota adjustment consistent with concurrent freeze/settlement
Admin quota adjustment now takes the per-user advisory lock and a FOR UPDATE
row lock on the quota account before projecting the balance, so the check
covers both used and reserved amounts and stays transactionally consistent
with task freeze and settlement. Settlement and lease-recovery paths also
lock the quota row explicitly.

Add the isolated Phase 5 exception-matrix harness (fault injection against a
dedicated DB + fake upstream) covering last-quota contention, concurrent
admin reduce, freeze/reduce stress, bad/oversized/timeout upstream, storage
failure, cross-period settlement and queued/running restart recovery.
Add test-only deps, provisioning helper (secrets via env) and test README.
2026-09-10 04:38:47 +08:00

36 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# Provision an ISOLATED Postgres database for the Phase 5 exception matrix.
#
# Secrets are injected through the environment only (never stored in the repo):
# SUDO_PASSWORD password for `sudo -S` (required to create role/DB)
# P5_DB_PASSWORD password for the isolated role (required)
# P5_DB_ROLE role name (default: kaotings_p5)
# P5_DB database name (default: kaotings_p5)
# P5_PGHOST host:port (default: 127.0.0.1:5432)
#
# Idempotent: drops and recreates the isolated role/database, applies the API
# migrations and seeds the plan policies + default voice, matching a fresh
# production schema. The production `kaotings` database is never touched.
set -euo pipefail
: "${SUDO_PASSWORD:?SUDO_PASSWORD must be set in the environment}"
: "${P5_DB_PASSWORD:?P5_DB_PASSWORD must be set in the environment}"
ROLE="${P5_DB_ROLE:-kaotings_p5}"
DB="${P5_DB:-kaotings_p5}"
PGHOST="${P5_PGHOST:-127.0.0.1:5432}"
API_DIR="$(cd "$(dirname "$0")/.." && pwd)"
PY="${P5_PYTHON:-python3}"
pg() { echo "$SUDO_PASSWORD" | sudo -S -p '' -u postgres psql "$@"; }
pg -c "DROP DATABASE IF EXISTS $DB;" -c "DROP ROLE IF EXISTS $ROLE;"
pg -c "CREATE ROLE $ROLE LOGIN PASSWORD '$P5_DB_PASSWORD';" -c "CREATE DATABASE $DB OWNER $ROLE;"
pg -d "$DB" -c "CREATE EXTENSION IF NOT EXISTS pgcrypto;"
export DATABASE_URL="postgresql://$ROLE:$P5_DB_PASSWORD@$PGHOST/$DB"
(cd "$API_DIR" && "$PY" -m app.migrate)
echo "isolated database ready: $DB (role $ROLE)"