|
| 1 | +import { readdir, readFile, stat } from "node:fs/promises"; |
| 2 | +import { join, extname } from "node:path"; |
| 3 | + |
| 4 | +// Matches: CREATE [UNIQUE] INDEX [IF NOT EXISTS] <name> ON <table> (<col1>, <col2>, ...) |
| 5 | +const SQL_INDEX_RE = |
| 6 | + /CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?\S+\s+ON\s+["'`]?(\w+)["'`]?\s*\(([^)]+)\)/gi; |
| 7 | + |
| 8 | +// Matches Prisma: @@index([col1, col2]) or @@unique([col1]) |
| 9 | +const PRISMA_INDEX_RE = /@@(?:index|unique)\(\[([^\]]+)\]\)/gi; |
| 10 | + |
| 11 | +// Matches the current Prisma model name: model <Name> { |
| 12 | +const PRISMA_MODEL_RE = /^model\s+(\w+)\s*\{/m; |
| 13 | + |
| 14 | +async function collectFiles(dir: string, ext: string, found: string[]): Promise<void> { |
| 15 | + let entries: string[]; |
| 16 | + try { |
| 17 | + const raw = await readdir(dir); |
| 18 | + entries = raw.map(String); |
| 19 | + } catch { |
| 20 | + return; |
| 21 | + } |
| 22 | + for (const entry of entries) { |
| 23 | + const full = join(dir, String(entry)); |
| 24 | + try { |
| 25 | + const s = await stat(full); |
| 26 | + if (s.isDirectory()) { |
| 27 | + await collectFiles(full, ext, found); |
| 28 | + } else if (extname(String(entry)) === ext) { |
| 29 | + found.push(full); |
| 30 | + } |
| 31 | + } catch { |
| 32 | + // skip unreadable entries |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function parseSqlIndexes(sql: string, indexMap: Map<string, Set<string>>): void { |
| 38 | + SQL_INDEX_RE.lastIndex = 0; |
| 39 | + let m: RegExpExecArray | null; |
| 40 | + while ((m = SQL_INDEX_RE.exec(sql)) !== null) { |
| 41 | + const table = m[1]; |
| 42 | + const cols = m[2].split(",").map((c) => c.trim().replace(/["'`]/g, "")); |
| 43 | + let set = indexMap.get(table); |
| 44 | + if (!set) { |
| 45 | + set = new Set(); |
| 46 | + indexMap.set(table, set); |
| 47 | + } |
| 48 | + for (const col of cols) set.add(col); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function parsePrismaIndexes(content: string, indexMap: Map<string, Set<string>>): void { |
| 53 | + // Split into model blocks and extract model name + @@index directives |
| 54 | + const modelBlocks = content.split(/(?=\nmodel\s)/); |
| 55 | + for (const block of modelBlocks) { |
| 56 | + const modelMatch = PRISMA_MODEL_RE.exec(block); |
| 57 | + if (!modelMatch) continue; |
| 58 | + const modelName = modelMatch[1]; |
| 59 | + |
| 60 | + PRISMA_INDEX_RE.lastIndex = 0; |
| 61 | + let m: RegExpExecArray | null; |
| 62 | + while ((m = PRISMA_INDEX_RE.exec(block)) !== null) { |
| 63 | + const cols = m[1].split(",").map((c) => c.trim()); |
| 64 | + let set = indexMap.get(modelName); |
| 65 | + if (!set) { |
| 66 | + set = new Set(); |
| 67 | + indexMap.set(modelName, set); |
| 68 | + } |
| 69 | + for (const col of cols) set.add(col); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Scans migration files (SQL, Prisma schema) and returns a map of |
| 76 | + * table/model names to their indexed column sets. |
| 77 | + */ |
| 78 | +export class MigrationScanner { |
| 79 | + async scan(dir: string): Promise<Map<string, Set<string>>> { |
| 80 | + const indexMap = new Map<string, Set<string>>(); |
| 81 | + |
| 82 | + const sqlFiles: string[] = []; |
| 83 | + const prismaFiles: string[] = []; |
| 84 | + |
| 85 | + await Promise.all([ |
| 86 | + collectFiles(dir, ".sql", sqlFiles), |
| 87 | + collectFiles(dir, ".prisma", prismaFiles), |
| 88 | + ]); |
| 89 | + |
| 90 | + await Promise.all([ |
| 91 | + ...sqlFiles.map(async (f) => { |
| 92 | + try { |
| 93 | + const content = await readFile(f, "utf8"); |
| 94 | + parseSqlIndexes(content, indexMap); |
| 95 | + } catch { |
| 96 | + // skip unreadable |
| 97 | + } |
| 98 | + }), |
| 99 | + ...prismaFiles.map(async (f) => { |
| 100 | + try { |
| 101 | + const content = await readFile(f, "utf8"); |
| 102 | + parsePrismaIndexes(content, indexMap); |
| 103 | + } catch { |
| 104 | + // skip unreadable |
| 105 | + } |
| 106 | + }), |
| 107 | + ]); |
| 108 | + |
| 109 | + return indexMap; |
| 110 | + } |
| 111 | +} |
0 commit comments