(
sql: CodeMigrationSql,
migrations: readonly CodeMigration[],
options: RunCodeMigrationsOptions = {},
)
| 82 | }; |
| 83 | |
| 84 | export const runCodeMigrations = async ( |
| 85 | sql: CodeMigrationSql, |
| 86 | migrations: readonly CodeMigration[], |
| 87 | options: RunCodeMigrationsOptions = {}, |
| 88 | ): Promise<readonly string[]> => { |
| 89 | const dryRun = options.dryRun ?? false; |
| 90 | const log = options.log ?? console.log; |
| 91 | assertUniqueNames(migrations); |
| 92 | |
| 93 | return withMigrationLock(sql, dryRun, async () => { |
| 94 | const completed = await readCompletedMigrations(sql, dryRun); |
| 95 | const applied: string[] = []; |
| 96 | |
| 97 | for (const migration of migrations) { |
| 98 | if (completed.has(migration.name)) { |
| 99 | log(`[code-migrate] skip ${migration.name}`); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | log(`[code-migrate] ${dryRun ? "plan" : "run"} ${migration.name}`); |
| 104 | const result = await migration.run({ sql, dryRun, log }); |
| 105 | log(`[code-migrate] ${result.summary}`); |
| 106 | |
| 107 | if (!dryRun) { |
| 108 | await sql.unsafe( |
| 109 | `INSERT INTO "${LEDGER_TABLE}" ("name", "time_completed") VALUES ($1, $2)`, |
| 110 | [migration.name, Date.now()], |
| 111 | ); |
| 112 | } |
| 113 | applied.push(migration.name); |
| 114 | } |
| 115 | |
| 116 | return applied; |
| 117 | }); |
| 118 | }; |
no test coverage detected