database_consistency 3.0.12 → 3.0.13
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.
- checksums.yaml +4 -4
- data/lib/database_consistency/helper.rb +248 -47
- data/lib/database_consistency/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 30c161f165c612d3196799c754ef057f0236c1bdcfd1bbd87d3c2fc0a2f28bc8
|
|
4
|
+
data.tar.gz: 1196509cb852b3a5ded62f8866b19f34ab6d9abc80af89d0806b08a25b779b56
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e4ba6f89c64740f04fa2a091fd5d137d4295b3884a9a24a524902e46ab20e80d5b7a76db77de59d1e8b214ef656724da103154169bbc99dada06eb6e1f150a26
|
|
7
|
+
data.tar.gz: 618299c5d08832c05120596316c919526ee72b6646272b10dfa624d98a47021931a5f3baa439906305ec615cc3ed7e2f31e55c963891355ffe2fbf244b07c622
|
|
@@ -194,29 +194,141 @@ module DatabaseConsistency
|
|
|
194
194
|
# for a column name by the boolean-predicate normalizer.
|
|
195
195
|
LITERAL_PLACEHOLDER = '__DATABASE_CONSISTENCY_LITERAL<%<index>d>__'
|
|
196
196
|
|
|
197
|
+
# Matches one single-quoted string literal, including any `''` it contains:
|
|
198
|
+
# SQL escapes a quote by doubling it, so a `''` pair is part of the value
|
|
199
|
+
# rather than the end of it.
|
|
200
|
+
CONDITION_LITERAL = /'(?:[^']|'')*'/.freeze
|
|
201
|
+
|
|
202
|
+
# Matches a number PostgreSQL had to quote in order to coerce it, together
|
|
203
|
+
# with the cast that says it is a number rather than a string. `::text` is
|
|
204
|
+
# deliberately absent from the list so a genuine string keeps its quotes.
|
|
205
|
+
COERCED_NUMERIC_LITERAL = /
|
|
206
|
+
' (-? \d+ (?:\.\d+)? (?: e[+-]?\d+ )? ) '
|
|
207
|
+
(?= :: (?: integer | bigint | numeric | double\s+precision ) \b )
|
|
208
|
+
/xi.freeze
|
|
209
|
+
|
|
210
|
+
# Matches a PostgreSQL cast, covering the type names written as several
|
|
211
|
+
# words, the length or precision an explicit cast carries and the `[]` of an
|
|
212
|
+
# array type: `::text`, `::text[]`, `::double precision`,
|
|
213
|
+
# `::character varying(3)`, `::numeric(5,2)`, `::time without time zone`.
|
|
214
|
+
# A date or time type carries its precision in the middle of its name, as
|
|
215
|
+
# `::timestamp(0) without time zone`, so that branch spells out its own.
|
|
216
|
+
CONDITION_CAST = /
|
|
217
|
+
::
|
|
218
|
+
(?:
|
|
219
|
+
character\s+varying |
|
|
220
|
+
double\s+precision |
|
|
221
|
+
bit\s+varying |
|
|
222
|
+
(?:timestamp|time) (?:\(\d+\))? \s+ (?:with|without)\s+time\s+zone |
|
|
223
|
+
\w+
|
|
224
|
+
)
|
|
225
|
+
(?:\(\d+(?:\s*,\s*\d+)?\))?
|
|
226
|
+
(?:\[\])?
|
|
227
|
+
/xi.freeze
|
|
228
|
+
|
|
229
|
+
# Matches a number written in exponent notation, capturing the sign, the
|
|
230
|
+
# digits on each side of the decimal point and the exponent separately so
|
|
231
|
+
# the point can be shifted through the digits as text. The lookbehind keeps
|
|
232
|
+
# the digits of an identifier such as `a1e5` out of it.
|
|
233
|
+
EXPONENT_LITERAL = /
|
|
234
|
+
(?<![\w.])
|
|
235
|
+
(-?) (\d+) (?: \.(\d+) )? e ([+-]?\d+)
|
|
236
|
+
/xi.freeze
|
|
237
|
+
|
|
238
|
+
# The parentheses right after `IN` or `NOT IN` are the list itself rather
|
|
239
|
+
# than something wrapped around a value, so the patterns below leave them
|
|
240
|
+
# alone and `qty IN (1)` stays a list of one. This covers only the
|
|
241
|
+
# parenthesis that opens the list; a value with parentheses of its own
|
|
242
|
+
# further along it, such as the `(1)` in `qty IN ((1), 2)`, still loses
|
|
243
|
+
# them.
|
|
244
|
+
IN_LIST_OPENING = /(?<!\bIN\s)/i.freeze
|
|
245
|
+
|
|
246
|
+
# Matches a bare identifier wrapped in parentheses, e.g. `(internal_name)`.
|
|
247
|
+
WRAPPED_IDENTIFIER = /#{IN_LIST_OPENING}\(([a-z_][\w.]*)\)/i.freeze
|
|
248
|
+
|
|
249
|
+
# Matches a parenthesized numeric literal, e.g. `(0)` or `(0.001)`, which is
|
|
250
|
+
# what a cast such as `(0)::numeric` leaves behind once the cast is gone.
|
|
251
|
+
# The lookbehind keeps the argument list of a call such as `abs(1)` intact.
|
|
252
|
+
WRAPPED_NUMBER = /(?<![\w.])#{IN_LIST_OPENING}\((-?\d+(?:\.\d+)?(?:e-?\d+)?)\)/.freeze
|
|
253
|
+
|
|
254
|
+
# Matches parentheses wrapping exactly one function call, such as the
|
|
255
|
+
# `(abs(1))` a removed `::numeric` cast leaves behind. The inner group
|
|
256
|
+
# recurses so the call's own argument list may nest, and the lookbehind
|
|
257
|
+
# keeps a call's own parentheses out of it.
|
|
258
|
+
WRAPPED_FUNCTION_CALL = /
|
|
259
|
+
(?<![\w.]) #{IN_LIST_OPENING}
|
|
260
|
+
\( (?<call>[a-z_][\w.]* (?<arguments>\( (?:[^()] | \g<arguments>)* \)) ) \)
|
|
261
|
+
/xi.freeze
|
|
262
|
+
|
|
263
|
+
# Matches a bare negated boolean predicate such as `NOT archived`, in the
|
|
264
|
+
# three places one can stand: at the start of an expression, after `AND` or
|
|
265
|
+
# `OR`, or after an opening parenthesis.
|
|
266
|
+
NEGATED_BOOLEAN_PREDICATE = /
|
|
267
|
+
(^ | (?: \bAND\b | \bOR\b | \( ))
|
|
268
|
+
\s* NOT \s+ ([a-z_][\w.]*) \s*
|
|
269
|
+
(?= $ | (?: \bAND\b | \bOR\b | \) ))
|
|
270
|
+
/xi.freeze
|
|
271
|
+
|
|
272
|
+
# Matches a bare boolean predicate such as `most_recent` in those same three
|
|
273
|
+
# places. It runs after the negated form so that `NOT archived` is already
|
|
274
|
+
# gone and cannot be read as the predicate `archived`.
|
|
275
|
+
BARE_BOOLEAN_PREDICATE = /
|
|
276
|
+
(^ | (?: \bAND\b | \bOR\b | \( ))
|
|
277
|
+
\s* ([a-z_][\w.]*) \s*
|
|
278
|
+
(?= $ | (?: \bAND\b | \bOR\b | \) ))
|
|
279
|
+
/xi.freeze
|
|
280
|
+
|
|
281
|
+
# Matches `column = ANY (ARRAY[...])` or `column != ALL ((ARRAY[...]))`,
|
|
282
|
+
# capturing the column name, the operator and the array payload. The inner
|
|
283
|
+
# parentheses come from Postgres indexdefs that wrap the array expression
|
|
284
|
+
# before casting; they are optional, but both or neither, so a group
|
|
285
|
+
# enclosing the whole predicate keeps its own.
|
|
286
|
+
ARRAY_MEMBERSHIP_PREDICATE = /
|
|
287
|
+
(?<column>[a-z_][\w.]*)\s*
|
|
288
|
+
(?<operator>=\s*ANY|(?:!=|<>)\s*ALL)\s*
|
|
289
|
+
\( (?: \(ARRAY\[(?<items>.*?)\]\) | ARRAY\[(?<items>.*?)\] ) \)
|
|
290
|
+
/xi.freeze
|
|
291
|
+
|
|
292
|
+
# Matches SQL like `NOT (column = '' OR column IS NULL)`, holding both sides
|
|
293
|
+
# to the same column with the backreference.
|
|
294
|
+
NEGATED_BLANK_OR_NIL_PREDICATE = /
|
|
295
|
+
NOT \s+ \( \s* \(?
|
|
296
|
+
([a-z_][\w.]*) \s* = \s* '' \s+ OR \s+ \1 \s+ IS \s+ NULL
|
|
297
|
+
\)? \s* \)
|
|
298
|
+
/xi.freeze
|
|
299
|
+
|
|
197
300
|
# Normalizes SQL predicates into a canonical form so semantically equivalent
|
|
198
301
|
# Rails validators and database partial indexes can be compared safely.
|
|
199
302
|
def normalize_condition_sql(sql)
|
|
303
|
+
# The two steps that read the inside of a literal run first, while it is
|
|
304
|
+
# still there to read. Everything after masking works on the shape of the
|
|
305
|
+
# predicate alone and so cannot rewrite a value by accident.
|
|
200
306
|
masked_sql, literals = sql.to_s
|
|
201
|
-
.then { |value|
|
|
202
|
-
.then { |value|
|
|
307
|
+
.then { |value| unquote_numeric_literals(value) }
|
|
308
|
+
.then { |value| normalize_quoted_boolean_literals(value) }
|
|
203
309
|
.then { |value| mask_condition_literals(value) }
|
|
204
310
|
|
|
205
|
-
normalize_masked_condition_sql(
|
|
311
|
+
normalize_masked_condition_sql(
|
|
312
|
+
masked_sql.then { |value| strip_outer_parentheses(value) }
|
|
313
|
+
.then { |value| normalize_boolean_and_null_keywords(value) },
|
|
314
|
+
literals
|
|
315
|
+
)
|
|
206
316
|
end
|
|
207
317
|
|
|
208
318
|
# Finishes normalization after string literals have been masked: runs the
|
|
209
|
-
# regex-based transforms that must not see inside literals,
|
|
210
|
-
#
|
|
319
|
+
# regex-based transforms that must not see inside literals, applies the
|
|
320
|
+
# final structural clean-ups, and only then restores the literal values.
|
|
321
|
+
# Restoring last protects literal contents from whitespace collapse and
|
|
322
|
+
# clause sorting.
|
|
211
323
|
def normalize_masked_condition_sql(masked_sql, literals)
|
|
212
324
|
masked_sql
|
|
213
|
-
.then { |value|
|
|
325
|
+
.then { |value| normalize_adapter_syntax(value) }
|
|
214
326
|
.then { |value| normalize_boolean_predicates(value) }
|
|
215
327
|
.then { |value| normalize_array_any_predicates(value) }
|
|
216
|
-
.then { |value| unmask_condition_literals(value, literals) }
|
|
217
328
|
.then { |value| normalize_negated_blank_or_nil_predicates(value) }
|
|
218
|
-
.then { |value| sort_and_clauses(value) }
|
|
329
|
+
.then { |value| sort_and_clauses(value, literals) }
|
|
219
330
|
.then { |value| value.gsub(/\s+/, ' ').strip }
|
|
331
|
+
.then { |value| unmask_condition_literals(value, literals) }
|
|
220
332
|
end
|
|
221
333
|
|
|
222
334
|
# Masks non-empty string literals so later regexes cannot rewrite their
|
|
@@ -224,7 +336,7 @@ module DatabaseConsistency
|
|
|
224
336
|
# normalization relies on them.
|
|
225
337
|
def mask_condition_literals(sql)
|
|
226
338
|
literals = []
|
|
227
|
-
masked_sql = sql.gsub(
|
|
339
|
+
masked_sql = sql.gsub(CONDITION_LITERAL) do |match|
|
|
228
340
|
if match == "''"
|
|
229
341
|
match
|
|
230
342
|
else
|
|
@@ -244,9 +356,52 @@ module DatabaseConsistency
|
|
|
244
356
|
sql
|
|
245
357
|
end
|
|
246
358
|
|
|
247
|
-
#
|
|
248
|
-
|
|
359
|
+
# PostgreSQL writes any literal it had to coerce as a quoted string with a
|
|
360
|
+
# cast: `-1` becomes `'-1'::integer`, `-1.5` becomes `'-1.5'::numeric` and
|
|
361
|
+
# `1e+20` becomes `'1e+20'::double precision`. Unquoting those lets them line
|
|
362
|
+
# up with the bare numbers Active Record generates. A `::text` cast is left
|
|
363
|
+
# alone so a genuine string comparison keeps its quotes.
|
|
364
|
+
def unquote_numeric_literals(sql)
|
|
365
|
+
sql.gsub(COERCED_NUMERIC_LITERAL) { Regexp.last_match(1) }
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# Rewrites a boolean written as the quoted `'t'` / `'f'` PostgreSQL stores.
|
|
369
|
+
# It reads the value inside the quotes, so it has to run before literals are
|
|
370
|
+
# masked, while that value is still there to read.
|
|
371
|
+
def normalize_quoted_boolean_literals(sql)
|
|
372
|
+
# Normalize PostgreSQL boolean literals stored as `'t'` / `'f'` inside
|
|
373
|
+
# comparisons. The operator is allowed to touch or be surrounded by
|
|
374
|
+
# arbitrary whitespace so forms like `flag='t'` and `flag <> 'f'` all
|
|
375
|
+
# collapse to the same canonical shape. Inequality is preserved as `!=`
|
|
376
|
+
# because `flag <> 't'` is not the same as `flag = 'f'` (NULL handling
|
|
377
|
+
# differs), so they must not share a canonical form. The lookbehind holds
|
|
378
|
+
# the equality patterns to a standalone `=`, so the ordering comparison in
|
|
379
|
+
# `note >= 't'` keeps both its operator and its value.
|
|
380
|
+
sql
|
|
381
|
+
.gsub(/(?<![<>!])\s*=\s*'t'/, ' = 1')
|
|
382
|
+
.gsub(/(?<![<>!])\s*=\s*'f'/, ' = 0')
|
|
383
|
+
.gsub(/\s*<>\s*'t'/, ' != 1')
|
|
384
|
+
.gsub(/\s*<>\s*'f'/, ' != 0')
|
|
385
|
+
.gsub(/\s*!=\s*'t'/, ' != 1')
|
|
386
|
+
.gsub(/\s*!=\s*'f'/, ' != 0')
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# Rewrites the `TRUE` / `FALSE` / `NULL` keywords and the `IS` phrasings
|
|
390
|
+
# around them to one spelling. These run once literals are masked, so a
|
|
391
|
+
# value that happens to read `IS TRUE` keeps its own text.
|
|
392
|
+
def normalize_boolean_and_null_keywords(sql)
|
|
249
393
|
normalized_sql = sql.dup
|
|
394
|
+
# `IS NOT TRUE` / `IS NOT FALSE` are matched before the bare `IS TRUE` /
|
|
395
|
+
# `IS FALSE` forms so the longer phrase wins. They normalize to `IS NOT 1`
|
|
396
|
+
# / `IS NOT 0` rather than `= 0` / `= 1` because `IS NOT TRUE` is not the
|
|
397
|
+
# same as `= FALSE` (NULL handling differs).
|
|
398
|
+
normalized_sql = normalized_sql.gsub(/\bIS\s+NOT\s+TRUE\b/i, ' IS NOT 1')
|
|
399
|
+
normalized_sql = normalized_sql.gsub(/\bIS\s+NOT\s+FALSE\b/i, ' IS NOT 0')
|
|
400
|
+
# `/\bIS\s+TRUE\b/i` and `/\bIS\s+FALSE\b/i` normalize predicate forms
|
|
401
|
+
# like `flag IS TRUE` to `flag = 1` so they match `flag = TRUE` and
|
|
402
|
+
# `flag = 't'`.
|
|
403
|
+
normalized_sql = normalized_sql.gsub(/\bIS\s+TRUE\b/i, ' = 1')
|
|
404
|
+
normalized_sql = normalized_sql.gsub(/\bIS\s+FALSE\b/i, ' = 0')
|
|
250
405
|
# `/\bTRUE\b/i` and `/\bFALSE\b/i` normalize boolean literals to `1` / `0`
|
|
251
406
|
# so they match SQL generated by Active Record on some adapters.
|
|
252
407
|
normalized_sql = normalized_sql.gsub(/\bTRUE\b/i, '1').gsub(/\bFALSE\b/i, '0')
|
|
@@ -254,27 +409,74 @@ module DatabaseConsistency
|
|
|
254
409
|
normalized_sql = normalized_sql.gsub(/\bIS\s+NOT\s+NULL\b/i, ' IS NOT NULL')
|
|
255
410
|
# `/\bIS\s+NULL\b/i` normalizes `IS NULL` spacing and casing.
|
|
256
411
|
normalized_sql = normalized_sql.gsub(/\bIS\s+NULL\b/i, ' IS NULL')
|
|
257
|
-
# `/ = 't'/` and `/ = 'f'/` normalize PostgreSQL boolean literals stored
|
|
258
|
-
# as `'t'` / `'f'` inside comparisons.
|
|
259
|
-
normalized_sql = normalized_sql.gsub(/ = 't'/, ' = 1').gsub(/ = 'f'/, ' = 0')
|
|
260
412
|
normalized_sql.gsub(/\s+/, ' ').strip
|
|
261
413
|
end
|
|
262
414
|
|
|
263
|
-
#
|
|
264
|
-
|
|
415
|
+
# Rewrites exponent notation as the plain decimal PostgreSQL itself writes
|
|
416
|
+
# when it expands a literal, so `1e+20` and the `1.0e+20` Active Record
|
|
417
|
+
# generates reach the same string. The digits are shifted as text rather
|
|
418
|
+
# than through a float, so a wide value keeps every one of them.
|
|
419
|
+
def expand_exponent_literals(sql)
|
|
420
|
+
sql.gsub(EXPONENT_LITERAL) do
|
|
421
|
+
match = Regexp.last_match
|
|
422
|
+
shift_decimal_point(match[1], "#{match[2]}#{match[3]}", match[2].length + match[4].to_i)
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# Places the decimal point `position` digits into `digits`, padding with
|
|
427
|
+
# zeros on whichever side falls short and dropping a fraction that ends in
|
|
428
|
+
# them, so `1e-20` and `1.0e-20` land on the same digits. A zero that only
|
|
429
|
+
# holds the decimal point's place goes too, so `0.1e+2` reaches `10`.
|
|
430
|
+
def shift_decimal_point(sign, digits, position)
|
|
431
|
+
expanded =
|
|
432
|
+
if position >= digits.length
|
|
433
|
+
digits + ('0' * (position - digits.length))
|
|
434
|
+
elsif position.positive?
|
|
435
|
+
"#{digits[0...position]}.#{digits[position..]}"
|
|
436
|
+
else
|
|
437
|
+
"0.#{'0' * -position}#{digits}"
|
|
438
|
+
end
|
|
439
|
+
# On Ruby < 3.0, frozen strings forbid `sub!`.
|
|
440
|
+
expanded = expanded.sub(/\A0+(?=\d)/, '')
|
|
441
|
+
|
|
442
|
+
"#{sign}#{expanded}".sub(/(\.\d*?)0+\z/, '\1').chomp('.')
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# Rewrites the spellings that differ between adapters, or between what an
|
|
446
|
+
# adapter stores and what Active Record writes: quoted identifiers, casts,
|
|
447
|
+
# exponent notation, the spacing of an `IN` list, the parentheses PostgreSQL
|
|
448
|
+
# adds around a cast operand and the `<>` it writes for inequality. Literals are masked throughout, so
|
|
449
|
+
# none of it reaches the inside of a value.
|
|
450
|
+
def normalize_adapter_syntax(sql)
|
|
265
451
|
# Strips quoted identifiers (double quotes on PostgreSQL/SQLite,
|
|
266
452
|
# backticks on MySQL) so the same column normalizes across adapters.
|
|
267
453
|
normalized_sql = sql.gsub(/["`]/, '')
|
|
268
|
-
|
|
269
|
-
normalized_sql = normalized_sql
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
|
|
454
|
+
normalized_sql = normalized_sql.gsub(CONDITION_CAST, '')
|
|
455
|
+
normalized_sql = expand_exponent_literals(normalized_sql)
|
|
456
|
+
# Gives `IN` one space before its list, so `qty IN(1)` and `qty IN (1)`
|
|
457
|
+
# reach the same string and the list is recognisable to the unwrappers
|
|
458
|
+
# below. `\b` keeps a call such as `min(1)` out of it.
|
|
459
|
+
normalized_sql = normalized_sql.gsub(/\bIN\s*\(/i, 'IN (')
|
|
460
|
+
normalized_sql = unwrap_redundant_parentheses(normalized_sql)
|
|
273
461
|
# `/\s*<>\s*/` rewrites the SQL inequality operator `<>` to `!=`.
|
|
274
462
|
normalized_sql = normalized_sql.gsub(/\s*<>\s*/, ' != ')
|
|
275
463
|
normalized_sql.gsub(/\s+/, ' ').strip
|
|
276
464
|
end
|
|
277
465
|
|
|
466
|
+
# Removes the parentheses PostgreSQL puts around an operand it had to cast,
|
|
467
|
+
# which are redundant once the cast itself is gone: `(0)::numeric` -> `0`,
|
|
468
|
+
# `((name)::character varying(3))::text` -> `name`, `(abs(1))::numeric` ->
|
|
469
|
+
# `abs(1)`. Each pass repeats because removing one layer can expose another.
|
|
470
|
+
def unwrap_redundant_parentheses(sql)
|
|
471
|
+
normalized_sql = sql.dup
|
|
472
|
+
|
|
473
|
+
true while normalized_sql.gsub!(WRAPPED_IDENTIFIER, '\1')
|
|
474
|
+
true while normalized_sql.gsub!(WRAPPED_NUMBER, '\1')
|
|
475
|
+
true while normalized_sql.gsub!(WRAPPED_FUNCTION_CALL, '\k<call>')
|
|
476
|
+
|
|
477
|
+
normalized_sql
|
|
478
|
+
end
|
|
479
|
+
|
|
278
480
|
# Repeatedly removes one wrapping layer of parentheses when the whole SQL
|
|
279
481
|
# fragment is enclosed, e.g. `((foo))` -> `foo`.
|
|
280
482
|
def strip_outer_parentheses(sql)
|
|
@@ -317,52 +519,51 @@ module DatabaseConsistency
|
|
|
317
519
|
def normalize_boolean_predicates(sql)
|
|
318
520
|
normalized_sql = sql.dup
|
|
319
521
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
normalized_sql.gsub!(
|
|
324
|
-
/(^|(?:\bAND\b|\bOR\b|\())\s*NOT\s+([a-z_][\w.]*)\s*(?=$|(?:\bAND\b|\bOR\b|\)))/i
|
|
325
|
-
) { "#{Regexp.last_match(1)} #{Regexp.last_match(2)} = 0" }
|
|
522
|
+
normalized_sql.gsub!(NEGATED_BOOLEAN_PREDICATE) do
|
|
523
|
+
"#{Regexp.last_match(1)} #{Regexp.last_match(2)} = 0"
|
|
524
|
+
end
|
|
326
525
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
/(^|(?:\bAND\b|\bOR\b|\())\s*([a-z_][\w.]*)\s*(?=$|(?:\bAND\b|\bOR\b|\)))/i
|
|
331
|
-
) { "#{Regexp.last_match(1)} #{Regexp.last_match(2)} = 1" }
|
|
526
|
+
normalized_sql.gsub!(BARE_BOOLEAN_PREDICATE) do
|
|
527
|
+
"#{Regexp.last_match(1)} #{Regexp.last_match(2)} = 1"
|
|
528
|
+
end
|
|
332
529
|
|
|
333
530
|
normalized_sql.gsub(/\s+/, ' ').strip
|
|
334
531
|
end
|
|
335
532
|
|
|
336
|
-
# Rewrites PostgreSQL's `= ANY (ARRAY[...])`
|
|
337
|
-
#
|
|
533
|
+
# Rewrites PostgreSQL's `= ANY (ARRAY[...])` and `<> ALL (ARRAY[...])` forms
|
|
534
|
+
# into the `IN (...)` and `NOT IN (...)` Active Record generates for arrays.
|
|
535
|
+
# `<>` has already become `!=` by this point in the pipeline.
|
|
338
536
|
def normalize_array_any_predicates(sql)
|
|
339
|
-
sql.gsub(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
537
|
+
sql.gsub(ARRAY_MEMBERSHIP_PREDICATE) do
|
|
538
|
+
match = Regexp.last_match
|
|
539
|
+
membership = match[:operator].match?(/ANY/i) ? 'IN' : 'NOT IN'
|
|
540
|
+
|
|
541
|
+
"#{match[:column]} #{membership} (#{match[:items].gsub(/\s+/, ' ').strip})"
|
|
542
|
+
end
|
|
344
543
|
end
|
|
345
544
|
|
|
346
545
|
# Rewrites negated "blank or nil" predicates into the same shape used by
|
|
347
546
|
# `allow_blank`-derived guards: `IS NOT NULL AND != ''`.
|
|
348
547
|
def normalize_negated_blank_or_nil_predicates(sql)
|
|
349
|
-
sql.gsub(
|
|
350
|
-
#
|
|
351
|
-
|
|
352
|
-
/NOT\s+\(\s*\(?([a-z_][\w.]*)\s*=\s*''\s+OR\s+\1\s+IS\s+NULL\)?\s*\)/i
|
|
353
|
-
) { "#{Regexp.last_match(1)} IS NOT NULL AND #{Regexp.last_match(1)} != ''" }
|
|
548
|
+
sql.gsub(NEGATED_BLANK_OR_NIL_PREDICATE) do
|
|
549
|
+
"#{Regexp.last_match(1)} IS NOT NULL AND #{Regexp.last_match(1)} != ''"
|
|
550
|
+
end
|
|
354
551
|
end
|
|
355
552
|
|
|
356
553
|
# Sorts simple `AND` clauses so `a AND b` and `b AND a` normalize to the
|
|
357
|
-
# same string before comparison.
|
|
358
|
-
|
|
554
|
+
# same string before comparison. Two clauses can be identical apart from the
|
|
555
|
+
# string each one compares against, and then those strings decide the order,
|
|
556
|
+
# which is why the literals go back in before the sort. A placeholder is
|
|
557
|
+
# numbered by where its literal appeared, so sorting on the placeholders
|
|
558
|
+
# would leave such a pair in whichever order it arrived in.
|
|
559
|
+
def sort_and_clauses(sql, literals)
|
|
359
560
|
# Matches `AND` with surrounding whitespace and splits the expression into
|
|
360
561
|
# comparable clause fragments.
|
|
361
562
|
clauses = sql.split(/\s+AND\s+/i)
|
|
362
563
|
return sql if clauses.length == 1
|
|
363
564
|
|
|
364
565
|
clauses.map! { |clause| strip_outer_parentheses(clause) }
|
|
365
|
-
clauses.
|
|
566
|
+
clauses.sort_by { |clause| unmask_condition_literals(clause, literals) }.join(' AND ')
|
|
366
567
|
end
|
|
367
568
|
|
|
368
569
|
# Builds the implicit SQL guard introduced by validator options that skip
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: database_consistency
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0.
|
|
4
|
+
version: 3.0.13
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Evgeniy Demin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-09-
|
|
11
|
+
date: 2026-09-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|