|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +export const MIGRATIONS_DIR = path.join(__dirname, '..', 'prisma', 'migrations'); |
| 5 | + |
| 6 | +/** Files permitted to sit directly under prisma/migrations/ (not inside a migration folder). */ |
| 7 | +export const ALLOWED_TOP_LEVEL_FILES = new Set(['migration_lock.toml']); |
| 8 | + |
| 9 | +/** |
| 10 | + * Migration folders created before this naming convention was enforced. |
| 11 | + * They are already applied to real databases, so they can't be renamed |
| 12 | + * without breaking Prisma's `_prisma_migrations` tracking table. New |
| 13 | + * migrations must not be added to this list. |
| 14 | + */ |
| 15 | +export const LEGACY_EXCEPTIONS = new Set([ |
| 16 | + '0_init', |
| 17 | + '1_add_wallet_limit', |
| 18 | + '20260601000000_add_spending_limits', |
| 19 | + '20260601000000_add_transaction_idempotency_key', |
| 20 | + '20260601000000_add_wallet_successor_id', |
| 21 | + '20260602_add_wallet_key_version', |
| 22 | + '20260723_add_asset_code_to_payment', |
| 23 | + '20260724000000_add_user_default_network', |
| 24 | + '20260724000000_add_user_last_login_metadata', |
| 25 | + '20260724_add_soft_delete_to_wallet_limit', |
| 26 | + '20260729000000_add_maintenance_state', |
| 27 | + '20260729000000_add_wallet_nickname', |
| 28 | +]); |
| 29 | + |
| 30 | +const NAME_PATTERN = /^\d{14}_[a-z0-9_]+$/; |
| 31 | + |
| 32 | +export interface MigrationEntry { |
| 33 | + name: string; |
| 34 | + isDirectory: boolean; |
| 35 | + hasMigrationSql: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Pure validation over a directory listing so the rules can be unit tested |
| 40 | + * without touching the filesystem. |
| 41 | + */ |
| 42 | +export function validateMigrationEntries(entries: MigrationEntry[]): string[] { |
| 43 | + const errors: string[] = []; |
| 44 | + const seenTimestamps = new Map<string, string>(); |
| 45 | + |
| 46 | + for (const entry of entries) { |
| 47 | + if (!entry.isDirectory) { |
| 48 | + if (!ALLOWED_TOP_LEVEL_FILES.has(entry.name)) { |
| 49 | + errors.push( |
| 50 | + `"${entry.name}" is a loose file directly under prisma/migrations/. ` + |
| 51 | + `Every migration must live in its own "<timestamp>_<name>/migration.sql" folder.`, |
| 52 | + ); |
| 53 | + } |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (!entry.hasMigrationSql) { |
| 58 | + errors.push( |
| 59 | + `"${entry.name}/" is missing a migration.sql file.`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (LEGACY_EXCEPTIONS.has(entry.name)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if (!NAME_PATTERN.test(entry.name)) { |
| 68 | + errors.push( |
| 69 | + `"${entry.name}" does not match the required "<14-digit-timestamp>_<snake_case_name>" ` + |
| 70 | + `format (e.g. 20260730120000_add_thing). See docs/PRISMA-MIGRATIONS.md.`, |
| 71 | + ); |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + const timestamp = entry.name.slice(0, 14); |
| 76 | + const clash = seenTimestamps.get(timestamp); |
| 77 | + if (clash) { |
| 78 | + errors.push( |
| 79 | + `"${entry.name}" reuses timestamp ${timestamp} already used by "${clash}". ` + |
| 80 | + `Migration timestamps must be unique and monotonically increasing.`, |
| 81 | + ); |
| 82 | + } else { |
| 83 | + seenTimestamps.set(timestamp, entry.name); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return errors; |
| 88 | +} |
| 89 | + |
| 90 | +function readEntries(dir: string): MigrationEntry[] { |
| 91 | + return fs.readdirSync(dir).map((name) => { |
| 92 | + const full = path.join(dir, name); |
| 93 | + const isDirectory = fs.statSync(full).isDirectory(); |
| 94 | + const hasMigrationSql = |
| 95 | + isDirectory && fs.existsSync(path.join(full, 'migration.sql')); |
| 96 | + return { name, isDirectory, hasMigrationSql }; |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function main() { |
| 101 | + const entries = readEntries(MIGRATIONS_DIR); |
| 102 | + const errors = validateMigrationEntries(entries); |
| 103 | + |
| 104 | + if (errors.length > 0) { |
| 105 | + console.error('Prisma migration naming check failed:\n'); |
| 106 | + for (const error of errors) { |
| 107 | + console.error(` - ${error}`); |
| 108 | + } |
| 109 | + console.error( |
| 110 | + '\nSee docs/PRISMA-MIGRATIONS.md for the naming convention and how to fix this.', |
| 111 | + ); |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + console.log(`Prisma migration naming check passed (${entries.length} entries).`); |
| 116 | +} |
| 117 | + |
| 118 | +if (require.main === module) { |
| 119 | + main(); |
| 120 | +} |
0 commit comments