* Load all migration files
()
| 43 | * Load all migration files |
| 44 | */ |
| 45 | async function loadMigrations(): Promise<Migration[]> { |
| 46 | const migrationsDir = path.join(__dirname, "migrations"); |
| 47 | const files = await fs.readdir(migrationsDir); |
| 48 | |
| 49 | const migrations: Migration[] = []; |
| 50 | const errors: string[] = []; |
| 51 | |
| 52 | for (const file of files) { |
| 53 | if (file.endsWith(".sql")) { |
| 54 | const parsed = parseMigrationFilename(file); |
| 55 | |
| 56 | if (!parsed) { |
| 57 | errors.push( |
| 58 | `Invalid migration filename: ${file}. Expected format: NNN_description.sql (e.g., 001_initial.sql)` |
| 59 | ); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | const filePath = path.join(migrationsDir, file); |
| 64 | const sql = await fs.readFile(filePath, "utf-8"); |
| 65 | |
| 66 | migrations.push({ |
| 67 | filename: file, |
| 68 | version: parsed.version, |
| 69 | sql, |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Detect duplicate version numbers (concurrent PRs picking the same number) |
| 75 | const seen = new Map<number, string>(); |
| 76 | for (const m of migrations) { |
| 77 | const existing = seen.get(m.version); |
| 78 | if (existing) { |
| 79 | errors.push( |
| 80 | `Duplicate migration version ${m.version}: ${existing} and ${m.filename}` |
| 81 | ); |
| 82 | } |
| 83 | seen.set(m.version, m.filename); |
| 84 | } |
| 85 | |
| 86 | if (errors.length > 0) { |
| 87 | throw new Error(`Migration filename validation failed:\n${errors.join("\n")}`); |
| 88 | } |
| 89 | |
| 90 | return migrations.sort((a, b) => a.version - b.version); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create migrations tracking table |
no test coverage detected