#!/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 < 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 < 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