()
| 27 | } |
| 28 | |
| 29 | async function getMissingMigrations(): Promise<string[]> { |
| 30 | const existingMigrations: Set<string> = new Set(); |
| 31 | |
| 32 | for await (const migrationFile of migrationsDirectory) { |
| 33 | // Skip non-files |
| 34 | if (!migrationFile.isFile) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | // Skip files not in the "001-blah.pgsql" format |
| 39 | if (!migrationFile.name.match(/^\d+-.*(\.pgsql)$/)) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | existingMigrations.add(migrationFile.name); |
| 44 | } |
| 45 | |
| 46 | // Sort migrations |
| 47 | const sortedExistingMigrations = [...existingMigrations].sort(); |
| 48 | |
| 49 | // Add everything to run, by default |
| 50 | const migrationsToExecute = new Set([...sortedExistingMigrations]); |
| 51 | |
| 52 | try { |
| 53 | const executedMigrations = await getExecutedMigrations(); |
| 54 | |
| 55 | // Remove any existing migrations that were executed, from the list of migrations to execute |
| 56 | for (const executedMigration of executedMigrations) { |
| 57 | migrationsToExecute.delete(executedMigration); |
| 58 | } |
| 59 | } catch (_error) { |
| 60 | // The table likely doesn't exist, so run everything. |
| 61 | } |
| 62 | |
| 63 | return Array.from(migrationsToExecute).sort(); |
| 64 | } |
| 65 | |
| 66 | async function runMigrations(missingMigrations: string[]): Promise<void> { |
| 67 | for (const missingMigration of missingMigrations) { |
no test coverage detected