#!/usr/bin/env bash # P0② 引擎 ALTER 安全通道 · 端到端验收 # # 证明:已建实体经 POST /api/meta/entity/{table}/attributes 加列后, # ① 元数据(SYS_ENTITYATTRIBUTE)有新列;② 物理表真的 ALTER 加了列(psql 直查 information_schema); # ③ 受控守卫拒绝重复列/不存在的表(负控)。 # # 用法(先以 dev profile 起 8094,见 scripts/run-dev.sh / five-ring-acceptance.sh): # BASE=http://localhost:8094 bash scripts/p0-alter-acceptance.sh set -uo pipefail BASE=${BASE:-http://localhost:8094} ACCT=${ACCT:-admin}; PW=${PW:-111111} PGURL=${PGURL:-postgresql://zhengwr@localhost:5433/eip4dev} TS=$(date +%s) TABLE="EIP4PT_ALT${TS}" TBL_LC=$(echo "$TABLE" | tr '[:upper:]' '[:lower:]') FAIL=0 jqpy() { python3 -c "import sys,json;d=json.load(sys.stdin,strict=False);print(eval(\"d$1\"))" 2>/dev/null; } say() { echo "$@"; } ok() { echo " ✓ $*"; } bad() { echo " ✗ $*"; FAIL=1; } TOK=$(curl -s -X POST "$BASE/api/auth/login" -H 'Content-Type: application/json' \ -d "{\"account\":\"$ACCT\",\"password\":\"$PW\"}" | jqpy "['token']") H=(-H "Authorization: Bearer $TOK" -H 'Content-Type: application/json') [ -n "${TOK:-}" ] && [ "$TOK" != "None" ] && ok "login token len=${#TOK}" || { bad "login 失败"; exit 1; } say "[1] 建实体 $TABLE(仅 TITLE)" ST=$(curl -s -X POST "$BASE/api/meta/entity" "${H[@]}" -d "{ \"tablename\":\"$TABLE\",\"name\":\"ALTER验收\", \"attributes\":[{\"column\":\"TITLE\",\"display\":\"标题\",\"type\":\"string\",\"length\":200,\"mandatory\":true}]}" | jqpy "['status']") [ "$ST" = "0" ] && ok "建表 status=0" || bad "建表失败 status=$ST" say "[2] 加列:AMOUNT(number)/QTY(integer)/NOTE(string,len 300)/OWNER(reference→SYS_USER)" R=$(curl -s -X POST "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" -d '{ "attributes":[{"column":"AMOUNT","display":"金额","type":"number"}, {"column":"QTY","display":"数量","type":"integer"}, {"column":"NOTE","display":"备注","type":"string","length":300}, {"column":"OWNER","display":"负责人","type":"reference","referenceTable":"SYS_USER"}]}') ST=$(echo "$R" | jqpy "['status']") ADDED=$(echo "$R" | jqpy "['data']['addedColumns']") [ "$ST" = "0" ] && ok "加列 status=0 added=$ADDED" || bad "加列失败: $R" say "[3] 元数据核对:GET attributes 含新列" COLS=$(curl -s "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" | python3 -c "import sys,json;d=json.load(sys.stdin);print(' '.join(c['column'] for c in d['data']['columns']))" 2>/dev/null) for c in AMOUNT QTY NOTE OWNER; do echo "$COLS" | grep -q "$c" && ok "元数据有列 $c" || bad "元数据缺列 $c (cols=$COLS)" done say "[4] 物理表核对:psql 直查 information_schema(证明 ALTER 真生效)" if command -v psql >/dev/null 2>&1; then PHYS=$(psql "$PGURL" -At -c "SELECT column_name||':'||data_type FROM information_schema.columns WHERE table_schema='eip3' AND table_name='$TBL_LC' AND column_name IN ('amount','qty','note','owner') ORDER BY column_name;" 2>/dev/null) echo "$PHYS" | grep -q "amount:numeric" && ok "物理列 amount numeric" || bad "物理列 amount 缺/类型错: $PHYS" echo "$PHYS" | grep -q "qty:integer" && ok "物理列 qty integer" || bad "物理列 qty 缺/类型错" echo "$PHYS" | grep -qE "note:character varying" && ok "物理列 note varchar" || bad "物理列 note 缺/类型错" echo "$PHYS" | grep -qE "owner:character" && ok "物理列 owner character(36)" || bad "物理列 owner 缺/类型错" else echo " (psql 不可用,跳过物理核对)" fi say "[5] 负控:重复加 AMOUNT → 拒;加到不存在的表 → 拒" ST=$(curl -s -X POST "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" -d '{"attributes":[{"column":"AMOUNT","type":"number"}]}' | jqpy "['status']") [ "$ST" != "0" ] && ok "重复列被拒 status=$ST" || bad "重复列未被拒" ST=$(curl -s -X POST "$BASE/api/meta/entity/EIP4PT_NOPE${TS}/attributes" "${H[@]}" -d '{"attributes":[{"column":"X","type":"number"}]}' | jqpy "['status']") [ "$ST" != "0" ] && ok "不存在的表被拒 status=$ST" || bad "不存在的表未被拒" say "[6] 清理" curl -s -X DELETE "$BASE/api/meta/entity/$TABLE" "${H[@]}" >/dev/null && ok "删除 $TABLE" echo "" [ "$FAIL" = "0" ] && echo "[完成] P0② 引擎 ALTER 安全通道 端到端验收 ✅ 全绿" || { echo "[失败] 存在未通过项 ✗"; exit 1; }