diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2016-12-21 12:32:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-21 12:32:15 +0100 |
commit | cb029207e19f511cb7b32c0819acb10dbaeb1198 (patch) | |
tree | 41197bd9ae640b736cec312b5c0bc78dadbb48f1 /activerecord | |
parent | 0dc9eb46ee47b1196583fd860b25242b6421ab1e (diff) | |
parent | 5e46f4ccb477dcb0ecce3242f20c59e9c890964d (diff) | |
download | rails-cb029207e19f511cb7b32c0819acb10dbaeb1198.tar.gz rails-cb029207e19f511cb7b32c0819acb10dbaeb1198.tar.bz2 rails-cb029207e19f511cb7b32c0819acb10dbaeb1198.zip |
Merge pull request #27355 from yukideluxe/fixtures-deleted-tables
fix fixtures loading for models with same table names in different databases
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/fixtures_test.rb | 15 | ||||
-rw-r--r-- | activerecord/test/fixtures/other_dogs.yml | 2 | ||||
-rw-r--r-- | activerecord/test/models/other_dog.rb | 5 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 2 | ||||
-rw-r--r-- | activerecord/test/support/connection.rb | 1 |
6 files changed, 28 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 21c5e5b5bb..3b4532a3f2 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -536,16 +536,16 @@ module ActiveRecord update_all_loaded_fixtures fixtures_map connection.transaction(requires_new: true) do - deleted_tables = Set.new + deleted_tables = Hash.new { |h, k| h[k] = Set.new } fixture_sets.each do |fs| conn = fs.model_class.respond_to?(:connection) ? fs.model_class.connection : connection table_rows = fs.table_rows table_rows.each_key do |table| - unless deleted_tables.include? table + unless deleted_tables[conn].include? table conn.delete "DELETE FROM #{conn.quote_table_name(table)}", "Fixture Delete" end - deleted_tables << table + deleted_tables[conn] << table end table_rows.each do |fixture_set_name, rows| diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index ea3e8d7727..dd48053823 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -12,9 +12,11 @@ require "models/company" require "models/computer" require "models/course" require "models/developer" +require "models/dog" require "models/doubloon" require "models/joke" require "models/matey" +require "models/other_dog" require "models/parrot" require "models/pirate" require "models/post" @@ -1021,3 +1023,16 @@ class FixtureClassNamesTest < ActiveRecord::TestCase assert_nil fixture_class_names["unregistered_identifier"] end end + +class SameNameDifferentDatabaseFixturesTest < ActiveRecord::TestCase + fixtures :dogs, :other_dogs + + test "fixtures are properly loaded" do + # Force loading the fixtures again to reproduce issue + ActiveRecord::FixtureSet.reset_cache + create_fixtures("dogs", "other_dogs") + + assert_kind_of Dog, dogs(:sophie) + assert_kind_of OtherDog, other_dogs(:lassie) + end +end diff --git a/activerecord/test/fixtures/other_dogs.yml b/activerecord/test/fixtures/other_dogs.yml new file mode 100644 index 0000000000..b576861929 --- /dev/null +++ b/activerecord/test/fixtures/other_dogs.yml @@ -0,0 +1,2 @@ +lassie: + id: 1 diff --git a/activerecord/test/models/other_dog.rb b/activerecord/test/models/other_dog.rb new file mode 100644 index 0000000000..418caf34be --- /dev/null +++ b/activerecord/test/models/other_dog.rb @@ -0,0 +1,5 @@ +require_dependency "models/arunit2_model" + +class OtherDog < ARUnit2Model + self.table_name = "dogs" +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 658591b6ec..9d76d0537e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -1050,3 +1050,5 @@ Professor.connection.create_table :courses_professors, id: false, force: true do t.references :course t.references :professor end + +OtherDog.connection.create_table :dogs, force: true diff --git a/activerecord/test/support/connection.rb b/activerecord/test/support/connection.rb index c9260398e2..bc5af36a28 100644 --- a/activerecord/test/support/connection.rb +++ b/activerecord/test/support/connection.rb @@ -2,6 +2,7 @@ require "active_support/logger" require "models/college" require "models/course" require "models/professor" +require "models/other_dog" module ARTest def self.connection_name |