Skip to content

Commit

Permalink
Merge pull request rails#39192 from kamipo/fix_remove_foreign_key_val…
Browse files Browse the repository at this point in the history
…idate

`remove_foreign_key` doesn't care `:validate` option if database has no feature
  • Loading branch information
kamipo authored May 8, 2020
2 parents e61bdbf + 7e96bbc commit d3625cc
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
16 changes: 3 additions & 13 deletions activerecord/test/cases/migration/foreign_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3625cc

Please sign in to comment.