* Apply a single migration
(migration: Migration)
| 122 | * Apply a single migration |
| 123 | */ |
| 124 | async function applyMigration(migration: Migration): Promise<void> { |
| 125 | const pool = getPool(); |
| 126 | const client = await pool.connect(); |
| 127 | |
| 128 | try { |
| 129 | await client.query("BEGIN"); |
| 130 | await client.query("SET LOCAL statement_timeout = 0"); |
| 131 | |
| 132 | // Execute migration SQL |
| 133 | await client.query(migration.sql); |
| 134 | |
| 135 | // Record migration |
| 136 | await client.query( |
| 137 | "INSERT INTO schema_migrations (version, filename) VALUES ($1, $2)", |
| 138 | [migration.version, migration.filename] |
| 139 | ); |
| 140 | |
| 141 | await client.query("COMMIT"); |
| 142 | |
| 143 | console.log(`✓ Applied migration: ${migration.filename}`); |
| 144 | } catch (error) { |
| 145 | await client.query("ROLLBACK"); |
| 146 | console.error(`✗ Failed to apply migration: ${migration.filename}`); |
| 147 | throw error; |
| 148 | } finally { |
| 149 | client.release(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Run all pending migrations |
no test coverage detected