#!/usr/bin/env node /** * Broader scan: UPDATE/DELETE where FROM/USING JOIN ON references the target * table name or alias. */ const fs = require('fs'); const path = require('path'); function walk(d, acc = []) { for (const e of fs.readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) { if (['node_modules', 'dist', '.git'].includes(e.name)) continue; walk(p, acc); } else if (/\.(ts|js|sql|cjs|mjs)$/.test(e.name)) acc.push(p); } return acc; } function chunks(file, text) { if (file.endsWith('.sql')) return [{ text, start: 0 }]; const out = []; const re = /`([\s\S]*?)`/g; let m; while ((m = re.exec(text))) out.push({ text: m[1], start: m.index }); return out; } const roots = ['server/src', 'server/scripts', 'scripts', 'migrations']; const files = roots.flatMap((r) => (fs.existsSync(r) ? walk(r) : [])); const findings = []; function checkStmt(kind, targetRaw, alias, body, meta) { const target = targetRaw.includes('.') ? targetRaw.split('.').pop() : targetRaw; const usingOrFrom = body.match( kind === 'DELETE' ? /\bUSING\b([\s\S]*?)(?=\bWHERE\b|\bRETURNING\b|$)/i : /\bFROM\b([\s\S]*?)(?=\bWHERE\b|\bRETURNING\b|$)/i ); if (!usingOrFrom) return; const scope = usingOrFrom[1]; if (!/\bJOIN\b/i.test(scope)) return; const names = [alias, target].filter(Boolean); const onParts = [ ...scope.matchAll( /\bON\b([\s\S]*?)(?=\b(?:INNER|LEFT|RIGHT|FULL|CROSS)?\s*JOIN\b|\bWHERE\b|$)/gi ), ].map((x) => x[1]); for (const on of onParts) { for (const name of names) { // alias.col OR target.col (when no alias, table name in ON is often invalid) const re = new RegExp(`\\b${name}\\.`, 'i'); if (re.test(on)) { findings.push({ ...meta, kind, target, alias: alias || null, hit: name, sample: on.trim().replace(/\s+/g, ' ').slice(0, 160), }); return; } } } } for (const file of files) { const text = fs.readFileSync(file, 'utf8'); const isMigration = file.includes(`${path.sep}migrations${path.sep}`) || file.includes('/migrations/'); const isScript = file.includes(`${path.sep}scripts${path.sep}`) || file.includes('/scripts/'); for (const chunk of chunks(file, text)) { const sql = chunk.text; const updateRe = /UPDATE\s+(?:ONLY\s+)?([a-zA-Z_][\w.]*)(?:\s+([a-zA-Z_]\w*))?\b([\s\S]*?)(?=;|$)/gi; let m; while ((m = updateRe.exec(sql))) { const [, target, aliasMaybe, body] = m; // aliasMaybe might be SET — treat SET as no alias const alias = aliasMaybe && !/^SET$/i.test(aliasMaybe) ? aliasMaybe : null; const rest = alias ? body : `${aliasMaybe || ''} ${body}`; if (!/\bFROM\b/i.test(rest) || !/\bJOIN\b/i.test(rest)) continue; const line = text.slice(0, chunk.start + m.index).split(/\n/).length; checkStmt('UPDATE', target, alias, rest, { file: file.replace(/\\/g, '/'), line, scope: isMigration ? 'migration' : isScript ? 'script' : 'runtime', }); } const deleteRe = /DELETE\s+FROM\s+([a-zA-Z_][\w.]*)(?:\s+([a-zA-Z_]\w*))?\b([\s\S]*?)(?=;|$)/gi; while ((m = deleteRe.exec(sql))) { const [, target, aliasMaybe, body] = m; const alias = aliasMaybe && !/^(USING|WHERE)$/i.test(aliasMaybe) ? aliasMaybe : null; const rest = alias ? body : `${aliasMaybe || ''} ${body}`; if (!/\bUSING\b/i.test(rest) || !/\bJOIN\b/i.test(rest)) continue; const line = text.slice(0, chunk.start + m.index).split(/\n/).length; checkStmt('DELETE', target, alias, rest, { file: file.replace(/\\/g, '/'), line, scope: isMigration ? 'migration' : isScript ? 'script' : 'runtime', }); } } } const runtime = findings.filter((f) => f.scope === 'runtime'); const other = findings.filter((f) => f.scope !== 'runtime'); console.log('=== RUNTIME ==='); console.log(JSON.stringify(runtime, null, 2)); console.log('runtime_count', runtime.length); console.log('\n=== MIGRATION/SCRIPT (historical, not live path) ==='); console.log(JSON.stringify(other, null, 2)); console.log('other_count', other.length);