(query: string)
| 84 | }; |
| 85 | |
| 86 | export function analyzeQuery(query: string): DatabaseResourceUpdate[] { |
| 87 | try { |
| 88 | const { stmts } = parseSync(query); |
| 89 | const changes = stmts |
| 90 | .map((s: { stmt: Record<string, unknown> }) => extractChange(s.stmt)) |
| 91 | .filter((c: DatabaseResourceUpdate | null): c is DatabaseResourceUpdate => c !== null); |
| 92 | |
| 93 | const seen = new Set<string>(); |
| 94 | return changes.filter((c: DatabaseResourceUpdate) => { |
| 95 | const key = `${c.type}:${c.name ?? ''}`; |
| 96 | if (seen.has(key)) { |
| 97 | return false; |
| 98 | } |
| 99 | seen.add(key); |
| 100 | return true; |
| 101 | }); |
| 102 | } catch (e) { |
| 103 | logger.warn('SQL parse error:', e); |
| 104 | return []; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function extractChange(stmt: Record<string, unknown>): DatabaseResourceUpdate | null { |
| 109 | const [stmtType, data] = Object.entries(stmt)[0] as [string, Record<string, unknown>]; |
no test coverage detected