1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

fix #create_fixtures when equal table names in different databases

This commit is contained in:
Julia Lopez 2016-12-14 12:14:47 +01:00
parent 0dc9eb46ee
commit 5e46f4ccb4
6 changed files with 28 additions and 3 deletions

View file

@ -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|

View file

@ -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

View file

@ -0,0 +1,2 @@
lassie:
id: 1

View file

@ -0,0 +1,5 @@
require_dependency "models/arunit2_model"
class OtherDog < ARUnit2Model
self.table_name = "dogs"
end

View file

@ -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

View file

@ -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