Files
SupplierSystem/scripts/prod-recover-db-continue.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

100 lines
3.9 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)
eval "$(node -e "
const u = new URL(process.env.DATABASE_URL);
console.log('DB_USER=' + JSON.stringify(u.username));
console.log('DB_PASS=' + JSON.stringify(decodeURIComponent(u.password||'')));
console.log('DB_NAME=' + JSON.stringify(u.pathname.replace(/^\//,'')));
")"
echo "=== recreate database $DB_NAME ==="
sudo -u postgres psql -v ON_ERROR_STOP=1 <<SQL
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$DB_NAME' AND pid <> pg_backend_pid();
DROP DATABASE IF EXISTS $DB_NAME;
CREATE DATABASE $DB_NAME OWNER $DB_USER;
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
SQL
export DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@127.0.0.1:5432/${DB_NAME}"
sed -i "s|^DATABASE_URL=.*|DATABASE_URL=$DATABASE_URL|" .env
echo "=== patch migrations: app_role enum compare -> text ==="
find "$MIG" -name '*.sql' -print0 | while IFS= read -r -d '' f; do
sed -i \
-e 's/user_roles\.role = ANY/user_roles.role::text = ANY/g' \
-e 's/ur\.role = /ur.role::text = /g' \
-e "s/ur\.role = '\([^']*\)'/ur.role::text = '\1'/g" \
"$f"
done
echo "=== strip seed INSERTs/UPDATEs (avoid FK failures on empty DB) ==="
node <<NODE
const fs = require('fs');
const path = require('path');
const keepAll = new Set(['20260606_041744_create_default_users.sql']);
const keepInsert = new Set(['20260607_create_branch_tenant.sql']);
const mig = '$MIG';
for (const name of fs.readdirSync(mig).filter((f) => f.endsWith('.sql')).sort()) {
const file = path.join(mig, name);
let out = fs.readFileSync(file, 'utf8');
if (!keepAll.has(name) && !keepInsert.has(name)) {
out = out.replace(/^INSERT INTO[\s\S]*?;\s*$/gim, '-- [recovery] stripped INSERT\n');
}
if (!keepAll.has(name)) {
out = out.replace(/^UPDATE[\s\S]*?;\s*$/gim, '-- [recovery] stripped UPDATE\n');
}
fs.writeFileSync(file, out);
}
NODE
echo "=== migrate ==="
export MIGRATIONS_DIR="$MIG"
export SKIP_DEMO_SEED=1
export SKIP_E2E_SEED=1
node migrate.js
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);
const exists = await pool.query(`SELECT id FROM auth.users WHERE email='superadmin@meoo.local'`);
if (!exists.rowCount) {
const uid = '00000000-0000-4000-8000-000000000001';
await pool.query(
`INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, raw_app_meta_data, raw_user_meta_data, created_at, updated_at)
VALUES ($1, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'superadmin@meoo.local', $2, NOW(), '{"provider":"email","providers":["email"]}', '{"username":"superadmin"}', NOW(), NOW())`,
[uid, hash]
);
await pool.query(
`INSERT INTO profiles (id, username, full_name, status) VALUES ($1, 'superadmin', '超级管理员', 1) ON CONFLICT (id) DO NOTHING`,
[uid]
);
await pool.query(
`INSERT INTO user_roles (user_id, role) VALUES ($1, 'super_admin') ON CONFLICT DO NOTHING`,
[uid]
);
} else {
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
echo "=== login ==="
curl -sS -m 10 -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