@skilldeck/migrate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +18 -0
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/bin/migrate.mjs +24 -0
- package/index.mjs +21 -0
- package/package.json +42 -0
- package/skills/api-contract-diff/SKILL.md +92 -0
- package/skills/api-contract-diff/references/versioning.md +50 -0
- package/skills/api-contract-diff/scripts/contract-diff.mjs +464 -0
- package/skills/dependency-upgrade/SKILL.md +102 -0
- package/skills/dependency-upgrade/references/ecosystems.md +45 -0
- package/skills/dependency-upgrade/scripts/upgrade-report.mjs +335 -0
- package/skills/schema-migration/SKILL.md +121 -0
- package/skills/schema-migration/references/frameworks.md +54 -0
- package/skills/schema-migration/references/postgres-locks.md +73 -0
- package/skills/schema-migration/scripts/lint-ddl.mjs +356 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// migrate / lint-ddl.mjs
|
|
3
|
+
//
|
|
4
|
+
// Flag SQL that takes heavy locks, rewrites tables, or breaks code that is still running.
|
|
5
|
+
//
|
|
6
|
+
// node lint-ddl.mjs <file|dir|->... [--dialect postgres|mysql] [--json]
|
|
7
|
+
//
|
|
8
|
+
// Exit 0 when clean or warnings only, 1 when any error-level finding, 2 on bad usage.
|
|
9
|
+
// Zero dependencies. Node 20+.
|
|
10
|
+
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { pathToFileURL } from 'node:url'
|
|
14
|
+
|
|
15
|
+
const HELP = `lint-ddl: zero-downtime checks for SQL migrations
|
|
16
|
+
|
|
17
|
+
node lint-ddl.mjs <file|dir|->... [--dialect postgres|mysql] [--json]
|
|
18
|
+
|
|
19
|
+
file|dir .sql files; directories are searched recursively. "-" reads stdin.
|
|
20
|
+
--dialect postgres (default) or mysql
|
|
21
|
+
--json machine-readable output
|
|
22
|
+
|
|
23
|
+
Generate SQL first for ORMs that don't write it (see references/frameworks.md):
|
|
24
|
+
alembic upgrade <rev> --sql | node lint-ddl.mjs -
|
|
25
|
+
python manage.py sqlmigrate <app> <migration> | node lint-ddl.mjs -
|
|
26
|
+
`
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Parsing: blank out comments and literals, split on top-level semicolons
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns [{ sql, code, line }] per statement. `code` has comments and string
|
|
34
|
+
* literals replaced by spaces (same length, newlines kept) so rules never match
|
|
35
|
+
* inside them; `sql` is the original text for display.
|
|
36
|
+
*/
|
|
37
|
+
export function splitStatements(src) {
|
|
38
|
+
let code = ''
|
|
39
|
+
let i = 0
|
|
40
|
+
while (i < src.length) {
|
|
41
|
+
const c = src[i]
|
|
42
|
+
const next = src[i + 1]
|
|
43
|
+
if (c === '-' && next === '-') {
|
|
44
|
+
const end = src.indexOf('\n', i)
|
|
45
|
+
const stop = end === -1 ? src.length : end
|
|
46
|
+
code += ' '.repeat(stop - i)
|
|
47
|
+
i = stop
|
|
48
|
+
} else if (c === '/' && next === '*') {
|
|
49
|
+
const end = src.indexOf('*/', i + 2)
|
|
50
|
+
const stop = end === -1 ? src.length : end + 2
|
|
51
|
+
code += blank(src.slice(i, stop))
|
|
52
|
+
i = stop
|
|
53
|
+
} else if (c === "'") {
|
|
54
|
+
let j = i + 1
|
|
55
|
+
while (j < src.length && !(src[j] === "'" && src[j + 1] !== "'")) j += src[j] === "'" ? 2 : 1
|
|
56
|
+
code += "'" + blank(src.slice(i + 1, j)) + "'"
|
|
57
|
+
i = j + 1
|
|
58
|
+
} else if (c === '$') {
|
|
59
|
+
const tag = src.slice(i).match(/^\$[A-Za-z_]*\$/)
|
|
60
|
+
if (!tag) { code += c; i++; continue }
|
|
61
|
+
const end = src.indexOf(tag[0], i + tag[0].length)
|
|
62
|
+
const stop = end === -1 ? src.length : end + tag[0].length
|
|
63
|
+
code += tag[0] + blank(src.slice(i + tag[0].length, stop - tag[0].length)) + (end === -1 ? '' : tag[0])
|
|
64
|
+
i = stop
|
|
65
|
+
} else {
|
|
66
|
+
code += c
|
|
67
|
+
i++
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const out = []
|
|
72
|
+
let start = 0
|
|
73
|
+
for (let k = 0; k <= code.length; k++) {
|
|
74
|
+
if (k === code.length || code[k] === ';') {
|
|
75
|
+
const piece = code.slice(start, k)
|
|
76
|
+
if (piece.trim()) {
|
|
77
|
+
const lead = piece.length - piece.trimStart().length
|
|
78
|
+
const line = code.slice(0, start + lead).split('\n').length
|
|
79
|
+
out.push({ sql: src.slice(start + lead, k).trim(), code: piece.trim(), line })
|
|
80
|
+
}
|
|
81
|
+
start = k + 1
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const blank = (s) => s.replace(/[^\n]/g, ' ')
|
|
88
|
+
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
// Rules
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
const ident = String.raw`(?:"[^"]+"|\x60[^\x60]+\x60|[\w.]+)`
|
|
94
|
+
const unq = (name) => name.replace(/["\x60]/g, '').toLowerCase().split('.').pop()
|
|
95
|
+
const tableOf = (code) => {
|
|
96
|
+
const m = code.match(new RegExp(String.raw`^\s*alter\s+table\s+(?:only\s+)?(?:if\s+exists\s+)?(${ident})`, 'i'))
|
|
97
|
+
|| code.match(new RegExp(String.raw`\bon\s+(?:only\s+)?(${ident})`, 'i'))
|
|
98
|
+
return m ? unq(m[1]) : null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Volatile defaults force a table rewrite even on Postgres 11+.
|
|
102
|
+
const VOLATILE = /\b(random|gen_random_uuid|uuid_generate_v[14]|clock_timestamp|timeofday|nextval)\s*\(/i
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Each rule: { id, level, dialects, test(stmt, ctx) -> bool, message, fix }.
|
|
106
|
+
* `ctx.newTables` holds tables created earlier in the same input: locking an
|
|
107
|
+
* empty table is harmless, so most rules skip them.
|
|
108
|
+
*/
|
|
109
|
+
const RULES = [
|
|
110
|
+
{
|
|
111
|
+
id: 'add-column-not-null-no-default',
|
|
112
|
+
level: 'error',
|
|
113
|
+
dialects: ['postgres'],
|
|
114
|
+
test: (s) => /\badd\s+(column\s+)?(?!constraint\b)[\s\S]*\bnot\s+null\b/i.test(s) && !/\bdefault\b/i.test(s) && /^\s*alter\s+table/i.test(s),
|
|
115
|
+
message: 'ADD COLUMN ... NOT NULL without a DEFAULT fails on any table that already has rows.',
|
|
116
|
+
fix: 'Add the column nullable, backfill in batches, then add the NOT NULL constraint (see set-not-null). Or give it a constant DEFAULT.',
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
id: 'add-column-volatile-default',
|
|
120
|
+
level: 'error',
|
|
121
|
+
dialects: ['postgres'],
|
|
122
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\badd\s+(column\s+)?[\s\S]*\bdefault\b/i.test(s) && VOLATILE.test(s),
|
|
123
|
+
message: 'A volatile DEFAULT (random(), gen_random_uuid(), clock_timestamp()...) rewrites the whole table under an ACCESS EXCLUSIVE lock.',
|
|
124
|
+
fix: 'Add the column with no default, set the default in a separate statement (applies to new rows only), and backfill existing rows in batches.',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
id: 'add-column-stored-generated',
|
|
128
|
+
level: 'error',
|
|
129
|
+
dialects: ['postgres'],
|
|
130
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\badd\s+(column\s+)?[\s\S]*(\bgenerated\s+always\s+as\s*\([\s\S]*\bstored\b|\b(big|small)?serial\b)/i.test(s),
|
|
131
|
+
message: 'Adding a STORED generated or SERIAL column rewrites the table under an ACCESS EXCLUSIVE lock.',
|
|
132
|
+
fix: 'Add a plain nullable column, backfill in batches, and maintain it with a trigger or application code; or schedule a maintenance window.',
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: 'create-index-not-concurrently',
|
|
136
|
+
level: 'error',
|
|
137
|
+
dialects: ['postgres'],
|
|
138
|
+
test: (s) => /^\s*create\s+(unique\s+)?index\b(?!\s+concurrently)/i.test(s),
|
|
139
|
+
message: 'CREATE INDEX without CONCURRENTLY blocks every write to the table until the build finishes.',
|
|
140
|
+
fix: 'Use CREATE INDEX CONCURRENTLY, in its own migration with the transaction wrapper disabled (see references/frameworks.md).',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
id: 'drop-index-not-concurrently',
|
|
144
|
+
level: 'warning',
|
|
145
|
+
dialects: ['postgres'],
|
|
146
|
+
test: (s) => /^\s*drop\s+index\b(?!\s+concurrently)/i.test(s),
|
|
147
|
+
message: 'DROP INDEX without CONCURRENTLY takes an ACCESS EXCLUSIVE lock on the table.',
|
|
148
|
+
fix: 'Use DROP INDEX CONCURRENTLY outside a transaction.',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: 'alter-column-type',
|
|
152
|
+
level: 'error',
|
|
153
|
+
dialects: ['postgres'],
|
|
154
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\balter\s+(column\s+)?\S+\s+(set\s+data\s+)?type\b/i.test(s),
|
|
155
|
+
message: 'Changing a column type usually rewrites the table and its indexes under an ACCESS EXCLUSIVE lock.',
|
|
156
|
+
fix: 'Expand/contract: add a new column with the new type, dual-write, backfill in batches, switch reads, then drop the old column. (Widening varchar(n) or varchar -> text is the safe exception.)',
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
id: 'modify-column-mysql',
|
|
160
|
+
level: 'error',
|
|
161
|
+
dialects: ['mysql'],
|
|
162
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\b(modify|change)\s+(column\s+)?/i.test(s) && !/\balgorithm\s*=\s*(instant|inplace)\b/i.test(s),
|
|
163
|
+
message: 'MODIFY/CHANGE COLUMN often falls back to ALGORITHM=COPY, which rebuilds the table and blocks writes.',
|
|
164
|
+
fix: 'State ALGORITHM=INSTANT or ALGORITHM=INPLACE, LOCK=NONE so MySQL refuses instead of silently copying; for large tables use gh-ost or pt-online-schema-change.',
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
id: 'set-not-null',
|
|
168
|
+
level: 'error',
|
|
169
|
+
dialects: ['postgres'],
|
|
170
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\balter\s+(column\s+)?\S+\s+set\s+not\s+null\b/i.test(s),
|
|
171
|
+
message: 'SET NOT NULL scans the whole table while holding an ACCESS EXCLUSIVE lock.',
|
|
172
|
+
fix: 'Postgres 12+: ADD CONSTRAINT c CHECK (col IS NOT NULL) NOT VALID; then VALIDATE CONSTRAINT c; then SET NOT NULL (skips the scan); then drop the check.',
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: 'add-constraint-without-not-valid',
|
|
176
|
+
level: 'error',
|
|
177
|
+
dialects: ['postgres'],
|
|
178
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\badd\s+(constraint\s+\S+\s+)?(foreign\s+key|check)\b/i.test(s) && !/\bnot\s+valid\b/i.test(s),
|
|
179
|
+
message: 'Adding a FOREIGN KEY or CHECK constraint validates every row while blocking writes (and, for FKs, the referenced table too).',
|
|
180
|
+
fix: 'Add it with NOT VALID (instant), then run ALTER TABLE ... VALIDATE CONSTRAINT in a separate statement; validation only takes a SHARE UPDATE EXCLUSIVE lock.',
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
id: 'add-unique-or-pk-without-index',
|
|
184
|
+
level: 'error',
|
|
185
|
+
dialects: ['postgres'],
|
|
186
|
+
test: (s) => /^\s*alter\s+table[\s\S]*\badd\s+(constraint\s+\S+\s+)?(unique|primary\s+key)\b/i.test(s) && !/\busing\s+index\b/i.test(s),
|
|
187
|
+
message: 'ADD UNIQUE / PRIMARY KEY builds its index while blocking writes.',
|
|
188
|
+
fix: 'CREATE UNIQUE INDEX CONCURRENTLY first, then ADD CONSTRAINT c UNIQUE USING INDEX idx (or PRIMARY KEY USING INDEX).',
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: 'rename',
|
|
192
|
+
level: 'error',
|
|
193
|
+
dialects: ['postgres', 'mysql'],
|
|
194
|
+
test: (s) => /^\s*(alter\s+table[\s\S]*\brename\s+(?!constraint\b|index\b)|rename\s+table\b)/i.test(s),
|
|
195
|
+
message: 'Renaming a column or table breaks every running instance of the app that still uses the old name.',
|
|
196
|
+
fix: 'Expand/contract: add the new column (or a view for tables), dual-write, backfill, move reads, deploy, and only then drop the old name.',
|
|
197
|
+
checkNewTables: false,
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
id: 'drop-column-or-table',
|
|
201
|
+
level: 'warning',
|
|
202
|
+
dialects: ['postgres', 'mysql'],
|
|
203
|
+
test: (s) => /^\s*(alter\s+table[\s\S]*\bdrop\s+(column\s+)?(?!constraint\b|default\b|not\b|index\b|identity\b|expression\b)\w|drop\s+table\b)/i.test(s),
|
|
204
|
+
message: 'Dropping is irreversible, and running code that still reads the column/table will error.',
|
|
205
|
+
fix: 'Deploy code that no longer references it first (for ORMs, also remove it from the model so SELECT * lists stop naming it), take a backup, then drop in a later release.',
|
|
206
|
+
checkNewTables: false,
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
id: 'data-change-in-migration',
|
|
210
|
+
level: 'warning',
|
|
211
|
+
dialects: ['postgres', 'mysql'],
|
|
212
|
+
test: (s) => /^\s*(update|delete)\b/i.test(s),
|
|
213
|
+
message: 'UPDATE/DELETE inside a schema migration runs as one long transaction: row locks, replication lag and bloat on large tables.',
|
|
214
|
+
fix: 'Move the backfill to a batched job (e.g. 1-10k rows per transaction, keyed by primary key) that runs after the schema change deploys.',
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
id: 'table-rewrite-command',
|
|
218
|
+
level: 'error',
|
|
219
|
+
dialects: ['postgres'],
|
|
220
|
+
test: (s) => /^\s*(vacuum\s+full|cluster)\b/i.test(s),
|
|
221
|
+
message: 'VACUUM FULL and CLUSTER rewrite the table under an ACCESS EXCLUSIVE lock.',
|
|
222
|
+
fix: 'Use pg_repack, or schedule a maintenance window.',
|
|
223
|
+
checkNewTables: false,
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
id: 'explicit-lock',
|
|
227
|
+
level: 'warning',
|
|
228
|
+
dialects: ['postgres', 'mysql'],
|
|
229
|
+
test: (s) => /^\s*lock\s+tables?\b/i.test(s),
|
|
230
|
+
message: 'An explicit table lock blocks all traffic to the table for the rest of the transaction.',
|
|
231
|
+
fix: 'Remove it unless you have measured that the transaction is short; set lock_timeout either way.',
|
|
232
|
+
checkNewTables: false,
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: 'mysql-add-index-without-algorithm',
|
|
236
|
+
level: 'warning',
|
|
237
|
+
dialects: ['mysql'],
|
|
238
|
+
test: (s) => (/^\s*create\s+(unique\s+)?index\b/i.test(s) || /^\s*alter\s+table[\s\S]*\badd\s+(unique\s+)?(index|key)\b/i.test(s)) && !/\block\s*=\s*none\b/i.test(s),
|
|
239
|
+
message: 'Without LOCK=NONE, MySQL may pick a locking algorithm for the index build.',
|
|
240
|
+
fix: 'Add ALGORITHM=INPLACE, LOCK=NONE so the statement fails fast instead of blocking writes.',
|
|
241
|
+
},
|
|
242
|
+
]
|
|
243
|
+
|
|
244
|
+
// ---------------------------------------------------------------------------
|
|
245
|
+
// Linting
|
|
246
|
+
// ---------------------------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
/** Lint one SQL source. Returns [{ rule, level, line, statement, message, fix }]. */
|
|
249
|
+
export function lintSql(src, { dialect = 'postgres' } = {}) {
|
|
250
|
+
const findings = []
|
|
251
|
+
const newTables = new Set()
|
|
252
|
+
const stmts = splitStatements(src)
|
|
253
|
+
let lockTimeoutSet = false
|
|
254
|
+
let firstAlter = null
|
|
255
|
+
|
|
256
|
+
for (const st of stmts) {
|
|
257
|
+
const code = st.code.replace(/\s+/g, ' ')
|
|
258
|
+
const created = code.match(new RegExp(String.raw`^\s*create\s+(?:unlogged\s+|temp(?:orary)?\s+)?table\s+(?:if\s+not\s+exists\s+)?(${ident})`, 'i'))
|
|
259
|
+
if (created) { newTables.add(unq(created[1])); continue }
|
|
260
|
+
if (/^\s*set\s+(local\s+)?lock_timeout\b/i.test(code) || /^\s*set\s+(session\s+)?lock_wait_timeout\b/i.test(code)) lockTimeoutSet = true
|
|
261
|
+
if (/^\s*alter\s+table\b/i.test(code) && !firstAlter && !newTables.has(tableOf(code))) firstAlter = st
|
|
262
|
+
|
|
263
|
+
const table = tableOf(code)
|
|
264
|
+
for (const r of RULES) {
|
|
265
|
+
if (!r.dialects.includes(dialect)) continue
|
|
266
|
+
if (r.checkNewTables !== false && table && newTables.has(table)) continue
|
|
267
|
+
if (!r.test(code)) continue
|
|
268
|
+
findings.push({ rule: r.id, level: r.level, line: st.line, statement: preview(st.sql), message: r.message, fix: r.fix })
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (firstAlter && !lockTimeoutSet) {
|
|
273
|
+
findings.push({
|
|
274
|
+
rule: 'missing-lock-timeout',
|
|
275
|
+
level: 'warning',
|
|
276
|
+
line: firstAlter.line,
|
|
277
|
+
statement: preview(firstAlter.sql),
|
|
278
|
+
message: 'No lock timeout is set. If ALTER TABLE queues behind a long query, every later query on the table queues behind it: an outage.',
|
|
279
|
+
fix: dialect === 'mysql'
|
|
280
|
+
? 'Start the migration with SET SESSION lock_wait_timeout = 5; and retry on failure.'
|
|
281
|
+
: "Start the migration with SET lock_timeout = '5s'; and retry on failure.",
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
return findings.sort((a, b) => a.line - b.line)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const preview = (sql) => {
|
|
288
|
+
const one = sql.replace(/\s+/g, ' ').trim()
|
|
289
|
+
return one.length > 110 ? one.slice(0, 107) + '...' : one
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const display = (abs) => {
|
|
293
|
+
const rel = path.relative(process.cwd(), abs)
|
|
294
|
+
return rel.startsWith('..') ? abs : rel
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function collect(inputs) {
|
|
298
|
+
const files = []
|
|
299
|
+
for (const input of inputs) {
|
|
300
|
+
if (input === '-') { files.push('-'); continue }
|
|
301
|
+
const abs = path.resolve(input)
|
|
302
|
+
if (!fs.existsSync(abs)) throw new Error(`not found: ${input}`)
|
|
303
|
+
if (fs.statSync(abs).isDirectory()) {
|
|
304
|
+
const walk = (d) => fs.readdirSync(d, { withFileTypes: true }).forEach((e) => {
|
|
305
|
+
if (e.name === 'node_modules' || e.name.startsWith('.')) return
|
|
306
|
+
const p = path.join(d, e.name)
|
|
307
|
+
if (e.isDirectory()) walk(p)
|
|
308
|
+
else if (e.name.endsWith('.sql')) files.push(p)
|
|
309
|
+
})
|
|
310
|
+
walk(abs)
|
|
311
|
+
} else files.push(abs)
|
|
312
|
+
}
|
|
313
|
+
return files.sort()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function main(argv) {
|
|
317
|
+
const opts = { dialect: 'postgres', json: false, inputs: [] }
|
|
318
|
+
for (let i = 0; i < argv.length; i++) {
|
|
319
|
+
const a = argv[i]
|
|
320
|
+
if (a === '-h' || a === '--help') { process.stdout.write(HELP); return 0 }
|
|
321
|
+
if (a === '--json') opts.json = true
|
|
322
|
+
else if (a === '--dialect') opts.dialect = argv[++i]
|
|
323
|
+
else if (a.startsWith('--dialect=')) opts.dialect = a.slice(10)
|
|
324
|
+
else if (a.startsWith('--') ) { process.stderr.write(`lint-ddl: unknown flag ${a}\n\n${HELP}`); return 2 }
|
|
325
|
+
else opts.inputs.push(a)
|
|
326
|
+
}
|
|
327
|
+
if (!['postgres', 'mysql'].includes(opts.dialect)) { process.stderr.write('lint-ddl: --dialect must be postgres or mysql\n'); return 2 }
|
|
328
|
+
if (!opts.inputs.length) { process.stderr.write(HELP); return 2 }
|
|
329
|
+
|
|
330
|
+
let files
|
|
331
|
+
try { files = collect(opts.inputs) } catch (err) { process.stderr.write(`lint-ddl: ${err.message}\n`); return 2 }
|
|
332
|
+
const results = files.map((f) => ({
|
|
333
|
+
file: f === '-' ? '<stdin>' : display(f),
|
|
334
|
+
findings: lintSql(fs.readFileSync(f === '-' ? 0 : f, 'utf8'), opts),
|
|
335
|
+
}))
|
|
336
|
+
const errors = results.reduce((n, r) => n + r.findings.filter((f) => f.level === 'error').length, 0)
|
|
337
|
+
const warnings = results.reduce((n, r) => n + r.findings.filter((f) => f.level === 'warning').length, 0)
|
|
338
|
+
|
|
339
|
+
if (opts.json) {
|
|
340
|
+
process.stdout.write(JSON.stringify({ dialect: opts.dialect, errors, warnings, results }, null, 2) + '\n')
|
|
341
|
+
} else {
|
|
342
|
+
for (const r of results) {
|
|
343
|
+
if (!r.findings.length) continue
|
|
344
|
+
process.stdout.write(`\n${r.file}\n`)
|
|
345
|
+
for (const f of r.findings) {
|
|
346
|
+
process.stdout.write(` ${f.line} ${f.level.toUpperCase()} ${f.rule}\n ${f.statement}\n why: ${f.message}\n fix: ${f.fix}\n`)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
process.stdout.write(`\n${files.length} file(s), ${errors} error(s), ${warnings} warning(s) [${opts.dialect}]\n`)
|
|
350
|
+
}
|
|
351
|
+
return errors ? 1 : 0
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
355
|
+
process.exitCode = main(process.argv.slice(2))
|
|
356
|
+
}
|