#!/usr/bin/env node /** * 清理 migrate 误灌的 E2E 测试分公司/账号,保留 superadmin + BR001 基础平台 * * 用法: * node prod-cleanup-e2e-seed.mjs (默认 dry-run,只统计不删除) * node prod-cleanup-e2e-seed.mjs --execute */ import pg from 'pg'; import dotenv from 'dotenv'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); dotenv.config({ path: path.join(__dirname, '..', '.env') }); const execute = process.argv.includes('--execute'); const dryRun = !execute; const SUPER_ADMIN = 'a0000000-0000-0000-0000-000000000001'; const KEEP_BRANCH = 'BR001'; const E2E_BRANCH = process.env.E2E_TEST_BRANCH || 'BR002'; const TEST_USER_IDS = [ 'a0000000-0000-0000-0000-000000000002', 'a0000000-0000-0000-0000-000000000003', 'a0000000-0000-0000-0000-000000000004', 'a0000000-0000-0000-0000-000000000005', 'a0000000-0000-0000-0000-000000000006', 'b0000000-0000-0000-0000-000000000001', 'b0000000-0000-0000-0000-000000000002', 'b0000000-0000-0000-0000-000000000003', ]; const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }); async function countRows(client, sql, params = []) { const { rows } = await client.query(sql, params); return Number(rows[0]?.c ?? 0); } async function main() { const client = await pool.connect(); try { const counts = { purchaser_users: await countRows( client, `SELECT COUNT(*)::int AS c FROM purchaser_users WHERE purchaser_code = 'PUR001' OR user_id = ANY($1::uuid[])`, [TEST_USER_IDS] ), supplier_users: await countRows( client, `SELECT COUNT(*)::int AS c FROM supplier_users WHERE supplier_code = 'SUP001' OR user_id = ANY($1::uuid[])`, [TEST_USER_IDS] ), delivery_person_users: await countRows( client, `SELECT COUNT(*)::int AS c FROM delivery_person_users WHERE person_code = 'DP001' OR user_id = ANY($1::uuid[])`, [TEST_USER_IDS] ), suppliers: await countRows( client, `SELECT COUNT(*)::int AS c FROM suppliers WHERE supplier_code = 'SUP001' AND company_name LIKE 'E2E%'` ), delivery_persons: await countRows( client, `SELECT COUNT(*)::int AS c FROM delivery_persons WHERE person_code = 'DP001'` ), user_roles: await countRows( client, `SELECT COUNT(*)::int AS c FROM user_roles WHERE user_id = ANY($1::uuid[])`, [TEST_USER_IDS] ), profiles: await countRows( client, `SELECT COUNT(*)::int AS c FROM profiles WHERE id = ANY($1::uuid[]) AND id <> $2`, [TEST_USER_IDS, SUPER_ADMIN] ), auth_users: await countRows( client, `SELECT COUNT(*)::int AS c FROM auth.users WHERE id = ANY($1::uuid[]) AND id <> $2`, [TEST_USER_IDS, SUPER_ADMIN] ), e2e_branch_system_configs: await countRows( client, `SELECT COUNT(*)::int AS c FROM system_configs WHERE branch_code = $1`, [E2E_BRANCH] ), e2e_branch_categories: await countRows( client, `SELECT COUNT(*)::int AS c FROM categories WHERE branch_code = $1`, [E2E_BRANCH] ), e2e_branch: await countRows(client, `SELECT COUNT(*)::int AS c FROM branches WHERE branch_code = $1`, [ E2E_BRANCH, ]), keep_branch_categories: await countRows( client, `SELECT COUNT(*)::int AS c FROM categories WHERE branch_code = $1`, [KEEP_BRANCH] ), }; console.log(JSON.stringify({ dryRun, e2eBranch: E2E_BRANCH, keepBranch: KEEP_BRANCH, toDelete: counts }, null, 2)); if (dryRun) { console.log('\n[dry-run] 未执行删除。确认后加 --execute'); return; } await client.query('BEGIN'); await client.query(`DELETE FROM purchaser_users WHERE purchaser_code = 'PUR001' OR user_id = ANY($1::uuid[])`, [ TEST_USER_IDS, ]); await client.query(`DELETE FROM supplier_users WHERE supplier_code = 'SUP001' OR user_id = ANY($1::uuid[])`, [ TEST_USER_IDS, ]); await client.query(`DELETE FROM delivery_person_users WHERE person_code = 'DP001' OR user_id = ANY($1::uuid[])`, [ TEST_USER_IDS, ]); await client.query(`DELETE FROM suppliers WHERE supplier_code = 'SUP001' AND company_name LIKE 'E2E%'`); await client.query(`DELETE FROM delivery_persons WHERE person_code = 'DP001'`); for (const userId of TEST_USER_IDS) { await client.query(`DELETE FROM user_roles WHERE user_id = $1`, [userId]); await client.query(`DELETE FROM profiles WHERE id = $1 AND id <> $2`, [userId, SUPER_ADMIN]); await client.query(`DELETE FROM auth.users WHERE id = $1 AND id <> $2`, [userId, SUPER_ADMIN]); } const e2eBranch = await client.query(`SELECT branch_code, branch_name FROM branches WHERE branch_code = $1`, [ E2E_BRANCH, ]); if (e2eBranch.rowCount) { await client.query(`DELETE FROM system_configs WHERE branch_code = $1`, [E2E_BRANCH]); await client.query(`DELETE FROM categories WHERE branch_code = $1`, [E2E_BRANCH]); await client.query(`DELETE FROM branches WHERE branch_code = $1`, [E2E_BRANCH]); console.log('removed branch', e2eBranch.rows[0]); } await client.query(`DELETE FROM categories WHERE branch_code = $1`, [KEEP_BRANCH]); await client.query( `UPDATE branches SET branch_name = '基础平台', status = 1, updated_at = NOW() WHERE branch_code = $1`, [KEEP_BRANCH] ); await client.query('COMMIT'); const summary = ( await pool.query( `SELECT 'branches' AS item, count(*)::int AS cnt FROM branches UNION ALL SELECT 'profiles', count(*)::int FROM profiles UNION ALL SELECT 'customers', count(*)::int FROM customers UNION ALL SELECT 'suppliers', count(*)::int FROM suppliers` ) ).rows; const users = ( await pool.query( `SELECT p.username, p.full_name, p.branch_code FROM profiles p ORDER BY p.username` ) ).rows; console.log('summary', JSON.stringify(summary, null, 2)); console.log('users', JSON.stringify(users, null, 2)); } catch (e) { await client.query('ROLLBACK').catch(() => {}); throw e; } finally { client.release(); await pool.end(); } } main().catch((e) => { console.error(e); process.exit(1); });