force-migrate.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dotenv/config';
  2. import { Pool } from 'pg';
  3. import fs from 'fs';
  4. import path from 'path';
  5. async function forceMigrate() {
  6. const pool = new Pool({
  7. connectionString: process.env.DATABASE_URL,
  8. });
  9. console.log('🚀 Starting Force Migration (Manual SQL Execution)...');
  10. const migrationDir = path.join(process.cwd(), 'drizzle');
  11. const files = fs.readdirSync(migrationDir)
  12. .filter(file => file.endsWith('.sql'))
  13. .sort();
  14. console.log('Found migration files:', files);
  15. for (const file of files) {
  16. const filePath = path.join(migrationDir, file);
  17. const sql = fs.readFileSync(filePath, 'utf8');
  18. console.log(`\n--- Executing: ${file} ---`);
  19. try {
  20. // We split by semicolon to execute statements one by one for better error granularity
  21. // Note: This is a simple splitter and might not handle complex multi-line statements perfectly,
  22. // but for standard Drizzle migrations it usually works.
  23. const statements = sql.split(';').map(s => s.trim()).filter(s => s.length > 0);
  24. for (const statement of statements) {
  25. console.log(`Executing: ${statement.substring(0, 50)}${statement.length > 50 ? '...' : ''}`);
  26. await pool.query(statement);
  27. }
  28. console.log(`✅ Successfully applied ${file}`);
  29. } catch (err: any) {
  30. console.error(`❌ Failed executing ${file}`);
  31. console.error('Error Message:', err.message);
  32. console.error('Error Code:', err.code);
  33. if (err.detail) console.error('Detail:', err.detail);
  34. if (err.hint) console.error('Hint:', err.hint);
  35. // We stop on error to prevent cascading failures
  36. process.exit(1);
  37. }
  38. }
  39. console.log('\n✨ All migrations applied successfully!');
  40. await pool.end();
  41. }
  42. forceMigrate();