Skip to content

Commit 3072d75

Browse files
authored
Merge pull request rails#43904 from TooManyBees/apply-migration-exception-to-7-1
Apply migration if_exists exception only on migrations >= v7.1
2 parents b376da8 + da472b6 commit 3072d75

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ def self.find(version)
3333
V7_1 = Current
3434

3535
class V7_0 < V7_1
36+
module TableDefinition
37+
private
38+
def raise_on_if_exist_options(options)
39+
end
40+
end
41+
42+
def create_table(table_name, **options)
43+
if block_given?
44+
super { |t| yield compatible_table_definition(t) }
45+
else
46+
super
47+
end
48+
end
49+
50+
def change_table(table_name, **options)
51+
if block_given?
52+
super { |t| yield compatible_table_definition(t) }
53+
else
54+
super
55+
end
56+
end
57+
58+
private
59+
def compatible_table_definition(t)
60+
class << t
61+
prepend TableDefinition
62+
end
63+
t
64+
end
3665
end
3766

3867
class V6_1 < V7_0
@@ -119,6 +148,10 @@ def column(name, type, index: nil, **options)
119148
options[:precision] ||= nil
120149
super
121150
end
151+
152+
private
153+
def raise_on_if_exist_options(options)
154+
end
122155
end
123156

124157
def create_table(table_name, **options)
@@ -171,6 +204,10 @@ def column(name, type, index: nil, **options)
171204
options[:precision] ||= nil
172205
super
173206
end
207+
208+
private
209+
def raise_on_if_exist_options(options)
210+
end
174211
end
175212

176213
module CommandRecorder
@@ -265,6 +302,10 @@ def references(*args, **options)
265302
super(*args, type: :integer, **options)
266303
end
267304
alias :belongs_to :references
305+
306+
private
307+
def raise_on_if_exist_options(options)
308+
end
268309
end
269310

270311
def create_table(table_name, **options)
@@ -331,6 +372,10 @@ def timestamps(**options)
331372
options[:null] = true if options[:null].nil?
332373
super
333374
end
375+
376+
private
377+
def raise_on_if_exist_options(options)
378+
end
334379
end
335380

336381
def add_reference(table_name, ref_name, **options)

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def migrate(x)
520520
end
521521

522522
def test_change_table_allows_if_exists_option_on_6_1
523-
migration = Class.new(ActiveRecord::Migration[6.1]) {
523+
migration = Class.new(ActiveRecord::Migration[7.0]) {
524524
def migrate(x)
525525
change_table(:testings) do |t|
526526
t.remove :foo, if_exists: true

0 commit comments

Comments
 (0)