#!/bin/bash set -euo pipefail cd /opt/ai-fresh-delivery/server export MIGRATIONS_DIR="${MIGRATIONS_DIR:-/opt/ai-fresh-delivery/migrations}" node <<'NODE' const fs = require('fs'); const path = require('path'); const { pool } = require('./db'); const migDir = process.env.MIGRATIONS_DIR; async function tableExists(name) { const r = await pool.query('SELECT to_regclass($1) AS reg', [`public.${name}`]); return Boolean(r.rows[0]?.reg); } async function columnExists(table, column) { const r = await pool.query( `SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name=$1 AND column_name=$2`, [table, column] ); return r.rowCount > 0; } (async () => { const files = fs.readdirSync(migDir).filter((f) => f.endsWith('.sql')).sort(); const applied = await pool.query('SELECT filename FROM schema_migrations'); const appliedSet = new Set(applied.rows.map((r) => r.filename)); const pending = files.filter((f) => !appliedSet.has(f)); console.log('=== PRODUCTION MIGRATION AUDIT ==='); console.log('applied:', appliedSet.size, 'pending:', pending.length); const checks = [ 'customer_sku_aliases', 'customer_image_aliases', 'supplier_users', 'delivery_person_users', 'purchaser_users', 'customer_skus', 'allocation_tasks', 'sku_suppliers', 'branches', 'customer_delivery_assignments', 'delivery_person_locations', ]; console.log('\n--- table existence ---'); for (const t of checks) { console.log(`${t}:`, (await tableExists(t)) ? 'OK' : 'MISSING'); } console.log('\n--- profiles columns ---'); for (const c of ['username', 'full_name', 'display_name', 'branch_code', 'status']) { console.log(`profiles.${c}:`, (await columnExists('profiles', c)) ? 'OK' : 'MISSING'); } const roleCol = await pool.query(` SELECT udt_name, data_type FROM information_schema.columns WHERE table_schema='public' AND table_name='user_roles' AND column_name='role' `); console.log('\nuser_roles.role:', roleCol.rows[0] || 'missing'); const profilesRls = await pool.query(` SELECT relrowsecurity FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relname='profiles' `); console.log('profiles RLS enabled:', profilesRls.rows[0]?.relrowsecurity ?? 'n/a'); console.log('\n--- pending list ---'); pending.forEach((f) => console.log(' ', f)); process.exit(0); })().catch((e) => { console.error(e); process.exit(1); }); NODE