#!/bin/bash set -euo pipefail APP=/opt/ai-fresh-delivery/server SQL_FILE="${1:-/tmp/20260610_customer_delivery_assignments.sql}" MIG_NAME="20260610_customer_delivery_assignments.sql" cd "$APP" export SQL_FILE MIG_NAME node <<'NODE' const fs = require('fs'); const { pool } = require('./db'); const sql = fs.readFileSync(process.env.SQL_FILE, 'utf8'); const migName = process.env.MIG_NAME; (async () => { const exists = await pool.query('SELECT 1 FROM schema_migrations WHERE filename = $1', [migName]); if (exists.rowCount) { console.log('migration already applied:', migName); process.exit(0); } const table = await pool.query("SELECT to_regclass('public.customer_delivery_assignments') AS t"); if (table.rows[0]?.t) { console.log('table already exists, recording migration'); await pool.query('INSERT INTO schema_migrations (filename) VALUES ($1) ON CONFLICT DO NOTHING', [migName]); process.exit(0); } await pool.query(sql); await pool.query('INSERT INTO schema_migrations (filename) VALUES ($1) ON CONFLICT DO NOTHING', [migName]); console.log('migration applied:', migName); process.exit(0); })().catch((e) => { console.error(e.message || e); process.exit(1); }); NODE