diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index a84e13344b700..e42c8ffc0815d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -105,8 +105,9 @@ def export_name_on_schema_dump? !ActiveRecord::SchemaDumper.fk_ignore_pattern.match?(name) if name end - def defined_for?(to_table: nil, **options) + def defined_for?(to_table: nil, validate: nil, **options) (to_table.nil? || to_table.to_s == self.to_table) && + (validate.nil? || validate == options.fetch(:validate, validate)) && options.all? { |k, v| self.options[k].to_s == v.to_s } end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb index 50cc619181401..824e9372b93a6 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb @@ -61,7 +61,7 @@ def add_foreign_key(from_table, to_table, **options) def remove_foreign_key(from_table, to_table = nil, **options) to_table ||= options[:to_table] - options = options.except(:name, :to_table) + options = options.except(:name, :to_table, :validate) foreign_keys = foreign_keys(from_table) fkey = foreign_keys.detect do |fk| diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb index 50afb9d3da2a6..8b1b54c4ab5a6 100644 --- a/activerecord/test/cases/migration/foreign_key_test.rb +++ b/activerecord/test/cases/migration/foreign_key_test.rb @@ -32,7 +32,7 @@ class Astronaut < ActiveRecord::Base end class CreateRocketsMigration < ActiveRecord::Migration::Current - def up + def change create_table :rockets do |t| t.string :name end @@ -42,11 +42,6 @@ def up t.references :rocket, foreign_key: true end end - - def down - drop_table :astronauts, if_exists: true - drop_table :rockets, if_exists: true - end end def setup @@ -533,18 +528,13 @@ def test_foreign_key_constraint_is_not_cached_incorrectly end class CreateSchoolsAndClassesMigration < ActiveRecord::Migration::Current - def up + def change create_table(:schools) create_table(:classes) do |t| t.references :school end - add_foreign_key :classes, :schools - end - - def down - drop_table :classes, if_exists: true - drop_table :schools, if_exists: true + add_foreign_key :classes, :schools, validate: true end end diff --git a/activerecord/test/cases/migration/references_foreign_key_test.rb b/activerecord/test/cases/migration/references_foreign_key_test.rb index 90a50a5651c51..43bb63389eef9 100644 --- a/activerecord/test/cases/migration/references_foreign_key_test.rb +++ b/activerecord/test/cases/migration/references_foreign_key_test.rb @@ -170,18 +170,13 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase end class CreateDogsMigration < ActiveRecord::Migration::Current - def up + def change create_table :dog_owners create_table :dogs do |t| t.references :dog_owner, foreign_key: true end end - - def down - drop_table :dogs, if_exists: true - drop_table :dog_owners, if_exists: true - end end def test_references_foreign_key_with_prefix