|
| 1 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +// Historical migrations use two numbering schemes and contain deployed |
| 5 | +// duplicate prefixes. They are frozen; new migrations begin at 0022. |
| 6 | +const LEGACY_MAX_PREFIX = 21; |
| 7 | +const LEGACY_UNNUMBERED = new Set([ |
| 8 | + 'add_refresh_token_family.sql', 'add_refresh_tokens.down.sql', 'add_refresh_tokens.sql', |
| 9 | + 'auth_index.down.sql', 'auth_index.sql', 'billing_index.down.sql', 'billing_index.sql', |
| 10 | + 'credits_index.down.sql', 'credits_index.sql', |
| 11 | +]); |
| 12 | + |
| 13 | +function prefix(filename: string): number | null { |
| 14 | + const match = filename.match(/^(\d{4})_/); |
| 15 | + return match ? Number(match[1]) : null; |
| 16 | +} |
| 17 | + |
| 18 | +function isUpMigration(filename: string): boolean { |
| 19 | + return (filename.endsWith('.sql') || filename.endsWith('.up.sql')) && !filename.endsWith('.down.sql'); |
| 20 | +} |
| 21 | + |
| 22 | +/** Return policy violations without mutating the migration directory. */ |
| 23 | +export function validateMigrationLayout(migrationDir: string): string[] { |
| 24 | + const files = readdirSync(migrationDir).filter(isUpMigration); |
| 25 | + const violations: string[] = []; |
| 26 | + const future = files.filter((file) => { |
| 27 | + const number = prefix(file); |
| 28 | + return number !== null ? number > LEGACY_MAX_PREFIX : !LEGACY_UNNUMBERED.has(file); |
| 29 | + }); |
| 30 | + |
| 31 | + for (const file of future) { |
| 32 | + const number = prefix(file); |
| 33 | + if (number === null) { |
| 34 | + violations.push(`Migration file "${file}" must use NNNN_description.sql naming.`); |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (!/^\d{4}_[a-z0-9][a-z0-9_-]*\.sql$/.test(file) && !/^\d{4}_[a-z0-9][a-z0-9_-]*\.up\.sql$/.test(file)) { |
| 38 | + violations.push(`Migration file "${file}" must use four digits and a lowercase description.`); |
| 39 | + } |
| 40 | + const content = readFileSync(path.join(migrationDir, file), 'utf8'); |
| 41 | + if (/\b(?:DROP|TRUNCATE)\b|\bDELETE\s+FROM\b/i.test(content) && !/^\s*--\s*destructive-approved:\s*#[0-9]+\s*$/im.test(content)) { |
| 42 | + violations.push(`Destructive migration "${file}" requires -- destructive-approved: #<issue>.`); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const numbers = future.map(prefix).filter((number): number is number => number !== null).sort((a, b) => a - b); |
| 47 | + for (let index = 0; index < numbers.length; index += 1) { |
| 48 | + const expected = LEGACY_MAX_PREFIX + 1 + index; |
| 49 | + if (numbers[index] !== expected) { |
| 50 | + violations.push(`Migration sequence must continue at ${String(expected).padStart(4, '0')}; found ${String(numbers[index]).padStart(4, '0')}.`); |
| 51 | + break; |
| 52 | + } |
| 53 | + if (index > 0 && numbers[index] === numbers[index - 1]) violations.push(`Duplicate new migration prefix ${numbers[index]}.`); |
| 54 | + } |
| 55 | + return violations; |
| 56 | +} |
0 commit comments