mirror of https://github.com/rails/rails
Merge pull request #51175 from Shopify/fixtures-connection-pool
Refactor FixtureSet to deal with connection pools
This commit is contained in:
commit
cb47c12aa5
|
@ -553,24 +553,24 @@ module ActiveRecord
|
||||||
@@all_cached_fixtures.clear
|
@@all_cached_fixtures.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_for_connection(connection)
|
def cache_for_connection_pool(connection_pool)
|
||||||
@@all_cached_fixtures[connection]
|
@@all_cached_fixtures[connection_pool]
|
||||||
end
|
end
|
||||||
|
|
||||||
def fixture_is_cached?(connection, table_name)
|
def fixture_is_cached?(connection_pool, table_name)
|
||||||
cache_for_connection(connection)[table_name]
|
cache_for_connection_pool(connection_pool)[table_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def cached_fixtures(connection, keys_to_fetch = nil)
|
def cached_fixtures(connection_pool, keys_to_fetch = nil)
|
||||||
if keys_to_fetch
|
if keys_to_fetch
|
||||||
cache_for_connection(connection).values_at(*keys_to_fetch)
|
cache_for_connection_pool(connection_pool).values_at(*keys_to_fetch)
|
||||||
else
|
else
|
||||||
cache_for_connection(connection).values
|
cache_for_connection_pool(connection_pool).values
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_fixtures(connection, fixtures_map)
|
def cache_fixtures(connection_pool, fixtures_map)
|
||||||
cache_for_connection(connection).update(fixtures_map)
|
cache_for_connection_pool(connection_pool).update(fixtures_map)
|
||||||
end
|
end
|
||||||
|
|
||||||
def instantiate_fixtures(object, fixture_set, load_instances = true)
|
def instantiate_fixtures(object, fixture_set, load_instances = true)
|
||||||
|
@ -588,15 +588,13 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_fixtures(fixtures_directories, fixture_set_names, class_names = {}, config = ActiveRecord::Base, &block)
|
def create_fixtures(fixtures_directories, fixture_set_names, class_names = {}, config = ActiveRecord::Base)
|
||||||
fixture_set_names = Array(fixture_set_names).map(&:to_s)
|
fixture_set_names = Array(fixture_set_names).map(&:to_s)
|
||||||
class_names.stringify_keys!
|
class_names.stringify_keys!
|
||||||
|
|
||||||
# FIXME: Apparently JK uses this.
|
connection_pool = config.connection_pool
|
||||||
connection = block_given? ? block : lambda { ActiveRecord::Base.connection }
|
|
||||||
|
|
||||||
fixture_files_to_read = fixture_set_names.reject do |fs_name|
|
fixture_files_to_read = fixture_set_names.reject do |fs_name|
|
||||||
fixture_is_cached?(connection.call, fs_name)
|
fixture_is_cached?(connection_pool, fs_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
if fixture_files_to_read.any?
|
if fixture_files_to_read.any?
|
||||||
|
@ -604,11 +602,11 @@ module ActiveRecord
|
||||||
Array(fixtures_directories),
|
Array(fixtures_directories),
|
||||||
fixture_files_to_read,
|
fixture_files_to_read,
|
||||||
class_names,
|
class_names,
|
||||||
connection,
|
connection_pool,
|
||||||
)
|
)
|
||||||
cache_fixtures(connection.call, fixtures_map)
|
cache_fixtures(connection_pool, fixtures_map)
|
||||||
end
|
end
|
||||||
cached_fixtures(connection.call, fixture_set_names)
|
cached_fixtures(connection_pool, fixture_set_names)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a consistent, platform-independent identifier for +label+.
|
# Returns a consistent, platform-independent identifier for +label+.
|
||||||
|
@ -641,7 +639,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def read_and_insert(fixtures_directories, fixture_files, class_names, connection) # :nodoc:
|
def read_and_insert(fixtures_directories, fixture_files, class_names, connection_pool) # :nodoc:
|
||||||
fixtures_map = {}
|
fixtures_map = {}
|
||||||
directory_glob = "{#{fixtures_directories.join(",")}}"
|
directory_glob = "{#{fixtures_directories.join(",")}}"
|
||||||
fixture_sets = fixture_files.map do |fixture_set_name|
|
fixture_sets = fixture_files.map do |fixture_set_name|
|
||||||
|
@ -655,21 +653,21 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
update_all_loaded_fixtures(fixtures_map)
|
update_all_loaded_fixtures(fixtures_map)
|
||||||
|
|
||||||
insert(fixture_sets, connection)
|
insert(fixture_sets, connection_pool)
|
||||||
|
|
||||||
fixtures_map
|
fixtures_map
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert(fixture_sets, connection) # :nodoc:
|
def insert(fixture_sets, connection_pool) # :nodoc:
|
||||||
fixture_sets_by_connection = fixture_sets.group_by do |fixture_set|
|
fixture_sets_by_pool = fixture_sets.group_by do |fixture_set|
|
||||||
if fixture_set.model_class
|
if fixture_set.model_class
|
||||||
fixture_set.model_class.connection
|
fixture_set.model_class.connection_pool
|
||||||
else
|
else
|
||||||
connection.call
|
connection_pool
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fixture_sets_by_connection.each do |conn, set|
|
fixture_sets_by_pool.each do |pool, set|
|
||||||
table_rows_for_connection = Hash.new { |h, k| h[k] = [] }
|
table_rows_for_connection = Hash.new { |h, k| h[k] = [] }
|
||||||
|
|
||||||
set.each do |fixture_set|
|
set.each do |fixture_set|
|
||||||
|
@ -678,6 +676,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pool.with_connection do |conn|
|
||||||
conn.insert_fixtures_set(table_rows_for_connection, table_rows_for_connection.keys)
|
conn.insert_fixtures_set(table_rows_for_connection, table_rows_for_connection.keys)
|
||||||
|
|
||||||
check_all_foreign_keys_valid!(conn)
|
check_all_foreign_keys_valid!(conn)
|
||||||
|
@ -688,6 +687,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def check_all_foreign_keys_valid!(conn)
|
def check_all_foreign_keys_valid!(conn)
|
||||||
return unless ActiveRecord.verify_foreign_keys_for_fixtures
|
return unless ActiveRecord.verify_foreign_keys_for_fixtures
|
||||||
|
|
|
@ -1242,8 +1242,8 @@ class FasterFixturesTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cache
|
def test_cache
|
||||||
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection, "categories")
|
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection_pool, "categories")
|
||||||
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection, "authors")
|
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection_pool, "authors")
|
||||||
|
|
||||||
assert_no_queries do
|
assert_no_queries do
|
||||||
create_fixtures("categories")
|
create_fixtures("categories")
|
||||||
|
@ -1251,7 +1251,7 @@ class FasterFixturesTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
load_extra_fixture("posts")
|
load_extra_fixture("posts")
|
||||||
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection, "posts")
|
assert ActiveRecord::FixtureSet.fixture_is_cached?(ActiveRecord::Base.connection_pool, "posts")
|
||||||
self.class.setup_fixture_accessors :posts
|
self.class.setup_fixture_accessors :posts
|
||||||
assert_equal "Welcome to the weblog", posts(:welcome).title
|
assert_equal "Welcome to the weblog", posts(:welcome).title
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue