Skip to content

Commit 62972a1

Browse files
authored
Merge pull request rails#50680 from skipkayhil/hm-fix-legacy-add-reference
Fix add_reference options validated on < 7.1
2 parents af2bbd5 + 71b4e22 commit 62972a1

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fix Migrations with versions older than 7.1 validating options given to
2+
`add_reference`.
3+
4+
*Hartley McGuire*
5+
16
* Add `<role>_types` class method to `ActiveRecord::DelegatedType` so that the delegated types can be instrospected
27

38
*JP Rosevear*

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ def expression_column_name?(column_name)
6363
column_name.is_a?(String) && /\W/.match?(column_name)
6464
end
6565
end
66+
6667
module TableDefinition
6768
include LegacyIndexName
69+
6870
def column(name, type, **options)
6971
options[:_skip_validate_options] = true
7072
super
@@ -97,6 +99,12 @@ def add_index(table_name, column_name, **options)
9799
super
98100
end
99101

102+
def add_reference(table_name, ref_name, **options)
103+
options[:_skip_validate_options] = true
104+
super
105+
end
106+
alias :add_belongs_to :add_reference
107+
100108
def create_table(table_name, **options)
101109
options[:_uses_legacy_table_name] = true
102110
options[:_skip_validate_options] = true

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -255,33 +255,6 @@ def migrate(x)
255255
end
256256
end
257257

258-
def test_options_are_not_validated
259-
migration = Class.new(ActiveRecord::Migration[4.2]) {
260-
def migrate(x)
261-
create_table :tests, wrong_id: false do |t|
262-
t.references :some_table, wrong_primary_key: true
263-
t.integer :some_id, wrong_unique: true
264-
t.string :some_string_column, wrong_null: false
265-
end
266-
267-
add_column :tests, "last_name", :string, wrong_precision: true
268-
269-
change_column :tests, :some_id, :float, wrong_index: true
270-
271-
change_table :tests do |t|
272-
t.change :some_id, :float, null: false, wrong_index: true
273-
t.integer :another_id, wrong_unique: true
274-
end
275-
end
276-
}.new
277-
278-
ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
279-
280-
assert connection.table_exists?(:tests)
281-
ensure
282-
connection.drop_table :tests, if_exists: true
283-
end
284-
285258
def test_create_table_allows_duplicate_column_names
286259
migration = Class.new(ActiveRecord::Migration[5.2]) {
287260
def migrate(x)
@@ -681,6 +654,37 @@ def precision_implicit_default
681654
end
682655
end
683656

657+
module NoOptionValidationTestCases
658+
def test_options_are_not_validated
659+
migration = Class.new(migration_class) {
660+
def migrate(x)
661+
create_table :tests, wrong_id: false do |t|
662+
t.references :some_table, wrong_primary_key: true
663+
t.integer :some_id, wrong_unique: true
664+
t.string :some_string_column, wrong_null: false
665+
end
666+
667+
add_column :tests, "last_name", :string, wrong_precision: true
668+
669+
change_column :tests, :some_id, :float, wrong_index: true
670+
671+
add_reference :tests, :another_table, invalid_option: :something
672+
673+
change_table :tests do |t|
674+
t.change :some_id, :float, null: false, wrong_index: true
675+
t.integer :another_id, wrong_unique: true
676+
end
677+
end
678+
}.new
679+
680+
ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
681+
682+
assert connection.table_exists?(:tests)
683+
ensure
684+
connection.drop_table :tests, if_exists: true
685+
end
686+
end
687+
684688
module DefaultPrecisionImplicitTestCases
685689
def test_datetime_doesnt_set_precision_on_change_table
686690
create_migration = Class.new(migration_class) {
@@ -838,6 +842,7 @@ def teardown
838842

839843
class CompatibilityTest7_0 < BaseCompatibilityTest
840844
include DefaultPrecisionSixTestCases
845+
include NoOptionValidationTestCases
841846

842847
private
843848
def migration_class
@@ -847,6 +852,7 @@ def migration_class
847852

848853
class CompatibilityTest6_1 < BaseCompatibilityTest
849854
include DefaultPrecisionImplicitTestCases
855+
include NoOptionValidationTestCases
850856

851857
private
852858
def migration_class
@@ -856,6 +862,7 @@ def migration_class
856862

857863
class CompatibilityTest6_0 < BaseCompatibilityTest
858864
include DefaultPrecisionImplicitTestCases
865+
include NoOptionValidationTestCases
859866

860867
private
861868
def migration_class
@@ -865,6 +872,7 @@ def migration_class
865872

866873
class CompatibilityTest5_2 < BaseCompatibilityTest
867874
include DefaultPrecisionImplicitTestCases
875+
include NoOptionValidationTestCases
868876

869877
private
870878
def migration_class
@@ -874,6 +882,7 @@ def migration_class
874882

875883
class CompatibilityTest5_1 < BaseCompatibilityTest
876884
include DefaultPrecisionImplicitTestCases
885+
include NoOptionValidationTestCases
877886

878887
private
879888
def migration_class
@@ -883,6 +892,7 @@ def migration_class
883892

884893
class CompatibilityTest5_0 < BaseCompatibilityTest
885894
include DefaultPrecisionImplicitTestCases
895+
include NoOptionValidationTestCases
886896

887897
private
888898
def migration_class
@@ -892,6 +902,7 @@ def migration_class
892902

893903
class CompatibilityTest4_2 < BaseCompatibilityTest
894904
include DefaultPrecisionImplicitTestCases
905+
include NoOptionValidationTestCases
895906

896907
private
897908
def migration_class

0 commit comments

Comments
 (0)