require('dotenv').config(); const fs = require('fs'); const path = require('path'); const { Pool } = require('pg'); const pool = new Pool({ connectionString: process.env.DATABASE_URL }); async function migrate() { const migrationsDir = path.join(__dirname, '..', 'migrations'); const files = fs.readdirSync(migrationsDir).filter(f => f.endsWith('.sql')).sort(); // Ensure migrations tracking table exists await pool.query(` CREATE TABLE IF NOT EXISTS schema_migrations ( filename VARCHAR(255) PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ) `); for (const file of files) { const { rows } = await pool.query( 'SELECT filename FROM schema_migrations WHERE filename = $1', [file] ); if (rows.length > 0) { console.log(` ✓ already applied: ${file}`); continue; } const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8'); // Extract and run only the UP section (before the -- DOWN comment) const upSection = sql.split('-- DOWN')[0].trim(); try { await pool.query(upSection); await pool.query('INSERT INTO schema_migrations (filename) VALUES ($1)', [file]); console.log(` ✅ applied: ${file}`); } catch (err) { console.error(` ❌ failed: ${file}`, err.message); process.exit(1); } } console.log('Migrations complete.'); await pool.end(); } migrate().catch(err => { console.error('Migration error:', err); process.exit(1); });