Files
SupplierSystem/scripts/prod-recover-db-resume.sh
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

83 lines
3.0 KiB
Bash

#!/bin/bash
set -euo pipefail
: "${SUPERADMIN_PASSWORD:?请设置环境变量 SUPERADMIN_PASSWORD}"
APP=/opt/ai-fresh-delivery
SERVER=$APP/server
MIG=$APP/migrations
cd "$SERVER"
export $(grep -m1 '^DATABASE_URL=' .env | xargs)
echo "=== patch known migration order issues ==="
MARKET="$MIG/20260627_market_invoice_api_integration.sql"
if [ -f "$MARKET" ] && ! grep -q 'CREATE TABLE IF NOT EXISTS market_prices' "$MARKET"; then
cat > /tmp/market_stub.sql <<'SQL'
CREATE TABLE IF NOT EXISTS market_prices (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sku_code VARCHAR(50) NOT NULL,
sku_name VARCHAR(200),
category_code VARCHAR(50),
market_price DECIMAL(10,2) NOT NULL DEFAULT 0,
our_price DECIMAL(10,2) NOT NULL DEFAULT 0,
deviation_rate DECIMAL(8,4) NOT NULL DEFAULT 0,
price_level VARCHAR(10) NOT NULL DEFAULT 'normal',
created_at TIMESTAMPTZ DEFAULT NOW()
);
SQL
cat /tmp/market_stub.sql "$MARKET" > /tmp/market_patched.sql
mv /tmp/market_patched.sql "$MARKET"
fi
echo "=== migrate (retry loop) ==="
export MIGRATIONS_DIR="$MIG"
export SKIP_DEMO_SEED=1
export SKIP_E2E_SEED=1
for attempt in 1 2 3 4 5 6 7 8 9 10; do
if node migrate.js 2>&1 | tee /tmp/migrate.log; then
echo "migrate OK"
break
fi
fail=$(grep -oP 'Applied \K[0-9a-z_]+\.sql' /tmp/migrate.log | tail -1 || true)
err=$(grep -E '^\[MIGRATION FAILED\]|^error:' /tmp/migrate.log | tail -1 || true)
echo "attempt $attempt failed after $fail: $err"
# strip data statements from next unapplied migration if FK/data error
if grep -q '23503\|23505\|does not exist' /tmp/migrate.log; then
next=$(ls "$MIG"/*.sql | while read f; do
base=$(basename "$f")
psql "$DATABASE_URL" -tAc "SELECT 1 FROM schema_migrations WHERE filename='$base'" 2>/dev/null | grep -q 1 || { echo "$f"; break; }
done)
if [ -n "${next:-}" ] && [ -f "$next" ]; then
echo "patching $next"
node -e "
const fs=require('fs'); const f=process.argv[1];
let s=fs.readFileSync(f,'utf8');
s=s.replace(/^INSERT INTO[\s\S]*?;\s*$/gim,'-- stripped INSERT\n');
s=s.replace(/^UPDATE[\s\S]*?;\s*$/gim,'-- stripped UPDATE\n');
fs.writeFileSync(f,s);
" "$next"
continue
fi
fi
exit 1
done
echo "=== superadmin ==="
node <<'NODE'
const bcrypt = require('bcryptjs');
const { Pool } = require('pg');
(async () => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const hash = await bcrypt.hash(process.env.SUPERADMIN_PASSWORD || (() => { throw new Error('SUPERADMIN_PASSWORD required'); })(), 10);
await pool.query(`UPDATE auth.users SET encrypted_password=$1 WHERE email='superadmin@meoo.local'`, [hash]);
await pool.end();
console.log('superadmin password set from SUPERADMIN_PASSWORD');
})();
NODE
sudo systemctl restart ai-fresh-api
sleep 3
curl -sS -m 15 -X POST http://127.0.0.1:3001/api/auth/unified-login \
-H 'Content-Type: application/json' \
--data-raw "$(printf '{"username":"%s","password":"%s"}' "superadmin" "$SUPERADMIN_PASSWORD")"
echo