#!/usr/bin/env node /** 生产环境脏数据只读审计 */ 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 pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }); const CHECKS = [ ['E2E/test orders', `SELECT order_code, order_status, remark FROM orders WHERE order_code LIKE 'E2E%' OR order_code LIKE 'ORD_MP_%' OR COALESCE(remark, '') ILIKE '%E2E%' OR COALESCE(remark, '') ILIKE '%流程测试%' OR COALESCE(remark, '') ILIKE '%全链路%' ORDER BY order_code`], ['legacy CYC cycles in orders', `SELECT cycle_code, count(*)::int AS c FROM orders WHERE cycle_code ~ '^CYC' GROUP BY 1`], ['test suppliers', `SELECT supplier_code, company_name FROM suppliers WHERE company_name ILIKE '%E2E%' OR supplier_code = 'SUP001'`], ['test purchaser_users', `SELECT purchaser_code, user_id FROM purchaser_users WHERE purchaser_code = 'PUR001'`], ['test delivery DP001', `SELECT person_code, person_name FROM delivery_persons WHERE person_code = 'DP001'`], ['profiles without roles', `SELECT p.username, p.full_name, p.branch_code FROM profiles p LEFT JOIN user_roles ur ON ur.user_id = p.id WHERE ur.user_id IS NULL AND p.id <> 'a0000000-0000-0000-0000-000000000001'`], ['null sku inventory', `SELECT count(*)::int AS c FROM inventory WHERE sku_code IS NULL`], ['null sku picking_items', `SELECT count(*)::int AS c FROM picking_items WHERE sku_code IS NULL`], ['duplicate supplier category bindings', `SELECT category_code, count(*)::int AS c FROM supplier_categories GROUP BY 1 HAVING count(*) > 1`], ['recent orders', `SELECT order_code, order_status, delivery_date, left(COALESCE(remark,''), 80) AS remark FROM orders ORDER BY created_at DESC LIMIT 15`], ['key table counts', `SELECT 'orders' AS t, count(*)::int AS c FROM orders UNION ALL SELECT 'purchase_orders', count(*)::int FROM purchase_orders UNION ALL SELECT 'supplier_preparations', count(*)::int FROM supplier_preparations UNION ALL SELECT 'inspections', count(*)::int FROM inspections UNION ALL SELECT 'inventory', count(*)::int FROM inventory UNION ALL SELECT 'picking_tasks', count(*)::int FROM picking_tasks UNION ALL SELECT 'delivery_tasks', count(*)::int FROM delivery_tasks UNION ALL SELECT 'processing_tasks', count(*)::int FROM processing_tasks UNION ALL SELECT 'exception_tickets', count(*)::int FROM exception_tickets`], ['optional photo permissions', `SELECT rp.role::text, p.permission_code FROM role_permissions rp JOIN permissions p ON p.id = rp.permission_id WHERE p.permission_code IN ('picking_manage', 'picking_barcode', 'inspection_photo', 'delivery_sign_photo') ORDER BY 1, 2`], ['purchase orders missing branch_code', `SELECT count(*)::int AS c FROM purchase_orders WHERE branch_code IS NULL`], ['stale open orders', `SELECT order_code, order_status, delivery_date FROM orders WHERE order_status NOT IN (90, 99) AND delivery_date < CURRENT_DATE - INTERVAL '7 days' ORDER BY delivery_date LIMIT 20`], ]; async function main() { console.log('=== PRODUCTION DIRTY DATA AUDIT ==='); console.log('DATABASE_URL set:', Boolean(process.env.DATABASE_URL)); for (const [label, sql] of CHECKS) { console.log(`\n--- ${label} ---`); try { const { rows } = await pool.query(sql); console.log(JSON.stringify(rows, null, 2)); } catch (error) { console.log('ERR', error.message); } } await pool.end(); } main().catch((error) => { console.error(error); process.exit(1); });