#!/usr/bin/env node

const path = require('path');

if (!process.env.DATABASE_URL) {
  require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
}

if (!process.env.DATABASE_URL) {
  console.error('ERROR: DATABASE_URL is not set');
  process.exit(1);
}

const { spawn } = require('child_process');
const args = process.argv.slice(2);

const migrateArgs = [
  'node-pg-migrate',
  '-f', path.resolve(__dirname, 'migrate-config.cjs'),
  '--no-check-order',
  ...args
];

const migrate = spawn('npx', migrateArgs, {
  stdio: 'inherit',
  env: process.env
});

migrate.on('close', (code) => {
  process.exit(code);
});
