java-topology/defects/rails/patch/rails-0015-schema-statements-duplicate-version-hash.patch

26 lines
1.4 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UNDF: UNDF-2026-000000254
Fixes rails-0015: schema_statements#assume_migrated_up_to — inserting.detect { |v| inserting.count(v) > 1 } is O(V²) duplicate detection.
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ DEFECT rails-0015: lines 1455-1460
inserting = (versions - migrated).select { |v| v < version }
if inserting.any?
- if (duplicate = inserting.detect { |v| inserting.count(v) > 1 }) # O(V²) — count() scans all V per element
+ freq = inserting.tally # FIX: O(V) frequency map
+ if (duplicate = freq.find { |v, c| c > 1 }&.first) # O(V) scan once
raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict."
end
execute insert_versions_sql(inserting)
end
# BEFORE: inserting.detect { |v| inserting.count(v) > 1 }
# count(v) is Array#count with argument — O(V) scan per element
# detect iterates up to V elements: total O(V²)
# AFTER: inserting.tally builds Hash<version, count> in O(V); find is O(V)
# total O(V) — linear
# Severity: MEDIUM — runs during db:migrate when migrating large version gaps
# Speedup: V× where V = number of pending migration versions
# At V=500: 250,000 → 500 ops