|
1 | | -// Validate migration SQL files for DSQL compatibility. |
2 | | -// Run after drizzle-kit generate to catch incompatible DDL before commit. |
| 1 | +// Transform drizzle-kit generated SQL for DSQL compatibility, then validate. |
| 2 | +// Automatically fixes what can be fixed, errors on what cannot. |
| 3 | +// |
| 4 | +// Auto-transforms: |
| 5 | +// - "--> statement-breakpoint" → blank line (runner splits on \n\n) |
| 6 | +// - "CREATE INDEX" → "CREATE INDEX ASYNC" |
| 7 | +// - Removes REFERENCES / FOREIGN KEY clauses |
| 8 | +// |
| 9 | +// Errors (cannot auto-fix): |
| 10 | +// - ALTER COLUMN TYPE |
| 11 | +// - DROP COLUMN |
| 12 | +// - SERIAL types |
3 | 13 | import fs from 'node:fs'; |
4 | 14 | import path from 'node:path'; |
5 | 15 | import { fileURLToPath } from 'node:url'; |
6 | 16 |
|
7 | 17 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
8 | 18 | const migrationsDir = path.join(__dirname, '..', 'migrations'); |
9 | 19 |
|
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 | 20 | const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')); |
| 21 | +let transformed = 0; |
20 | 22 | let errors = 0; |
21 | 23 |
|
22 | 24 | 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)) { |
| 25 | + const filePath = path.join(migrationsDir, file); |
| 26 | + const original = fs.readFileSync(filePath, 'utf8'); |
| 27 | + let sql = original; |
| 28 | + |
| 29 | + // Auto-transform: statement-breakpoint → blank line |
| 30 | + sql = sql.replace(/--> statement-breakpoint\n/g, '\n'); |
| 31 | + |
| 32 | + // Auto-transform: CREATE INDEX → CREATE INDEX ASYNC (skip if already ASYNC) |
| 33 | + sql = sql.replace(/CREATE\s+INDEX(?!\s+ASYNC)/gi, 'CREATE INDEX ASYNC'); |
| 34 | + |
| 35 | + // Auto-transform: remove lines with REFERENCES or FOREIGN KEY |
| 36 | + sql = sql |
| 37 | + .split('\n') |
| 38 | + .filter((line) => !/REFERENCES\s+/i.test(line) && !/FOREIGN\s+KEY/i.test(line)) |
| 39 | + .join('\n'); |
| 40 | + |
| 41 | + if (sql !== original) { |
| 42 | + fs.writeFileSync(filePath, sql); |
| 43 | + console.log(`TRANSFORMED: ${file}`); |
| 44 | + transformed++; |
| 45 | + } |
| 46 | + |
| 47 | + // Validate: errors that cannot be auto-fixed |
| 48 | + const unfixable: { pattern: RegExp; message: string }[] = [ |
| 49 | + { pattern: /ALTER\s+.*\s+TYPE\s+/i, message: 'ALTER COLUMN TYPE not supported' }, |
| 50 | + { pattern: /DROP\s+COLUMN/i, message: 'DROP COLUMN not supported' }, |
| 51 | + { pattern: /\bSERIAL\b/i, message: 'SERIAL types not supported (use UUID)' }, |
| 52 | + ]; |
| 53 | + for (const { pattern, message } of unfixable) { |
| 54 | + if (pattern.test(sql)) { |
26 | 55 | console.error(`ERROR: ${file} — ${message}`); |
27 | 56 | errors++; |
28 | 57 | } |
29 | 58 | } |
30 | 59 | } |
31 | 60 |
|
| 61 | +if (transformed > 0) console.log(`\n${transformed} file(s) auto-transformed for DSQL compatibility.`); |
32 | 62 | if (errors > 0) { |
33 | | - console.error(`\n${errors} DSQL compatibility error(s). Fix the SQL before committing.`); |
| 63 | + console.error(`\n${errors} unfixable error(s). These require manual migration scripts.`); |
34 | 64 | process.exit(1); |
35 | 65 | } |
36 | 66 | console.log('All migration files DSQL-compatible.'); |
0 commit comments