|
| 1 | +// Validate migration SQL files for DSQL compatibility. |
| 2 | +// Run after drizzle-kit generate to catch incompatible DDL before commit. |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | +const migrationsDir = path.join(__dirname, '..', 'migrations'); |
| 9 | + |
| 10 | +const rules: { pattern: RegExp; message: string }[] = [ |
| 11 | + { pattern: /CREATE\s+INDEX\s+(?!.*ASYNC)/i, message: 'CREATE INDEX must use ASYNC keyword' }, |
| 12 | + { pattern: /REFERENCES\s+/i, message: 'FOREIGN KEY / REFERENCES not supported' }, |
| 13 | + { pattern: /FOREIGN\s+KEY/i, message: 'FOREIGN KEY not supported' }, |
| 14 | + { pattern: /ALTER\s+.*\s+TYPE\s+/i, message: 'ALTER COLUMN TYPE not supported' }, |
| 15 | + { pattern: /DROP\s+COLUMN/i, message: 'DROP COLUMN not supported' }, |
| 16 | + { pattern: /\bSERIAL\b/i, message: 'SERIAL types not supported (use UUID)' }, |
| 17 | +]; |
| 18 | + |
| 19 | +const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')); |
| 20 | +let errors = 0; |
| 21 | + |
| 22 | +for (const file of files) { |
| 23 | + const content = fs.readFileSync(path.join(migrationsDir, file), 'utf8'); |
| 24 | + for (const { pattern, message } of rules) { |
| 25 | + if (pattern.test(content)) { |
| 26 | + console.error(`ERROR: ${file} — ${message}`); |
| 27 | + errors++; |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +if (errors > 0) { |
| 33 | + console.error(`\n${errors} DSQL compatibility error(s). Fix the SQL before committing.`); |
| 34 | + process.exit(1); |
| 35 | +} |
| 36 | +console.log('All migration files DSQL-compatible.'); |
0 commit comments