# # force TestFixtures to begin transaction with all shards. # require 'active_record/fixtures' module ActiveRecord class Fixtures def self.create_fixtures(fixtures_directory, table_names, class_names = {}) table_names = [table_names].flatten.map { |n| n.to_s } table_names.each { |n| class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/') } # FIXME: Apparently JK uses this. connection = block_given? ? yield : ActiveRecord::Base.connection files_to_read = table_names.reject { |table_name| fixture_is_cached?(connection, table_name) } unless files_to_read.empty? connection.disable_referential_integrity do fixtures_map = {} fixture_files = files_to_read.map do |path| table_name = path.tr '/', '_' fixtures_map[path] = ActiveRecord::Fixtures.new( connection, table_name, class_names[table_name.to_sym] || table_name.classify, ::File.join(fixtures_directory, path)) end all_loaded_fixtures.update(fixtures_map) ActiveRecord::Turntable::Base.force_transaction_all_shards!(:requires_new => true) do fixture_files.each do |ff| conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection table_rows = ff.table_rows table_rows.keys.each do |table| conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete' end table_rows.each do |table_name,rows| rows.each do |row| conn.insert_fixture(row, table_name) end end end # Cap primary key sequences to max(pk). if connection.respond_to?(:reset_pk_sequence!) table_names.each do |table_name| connection.reset_pk_sequence!(table_name.tr('/', '_')) end end end cache_fixtures(connection, fixtures_map) end end cached_fixtures(connection, table_names) end end module TestFixtures def setup_fixtures return unless !ActiveRecord::Base.configurations.blank? if pre_loaded_fixtures && !use_transactional_fixtures raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' end @fixture_cache = {} @fixture_connections = [] @@already_loaded_fixtures ||= {} # Load fixtures once and begin transaction. if run_in_transaction? if @@already_loaded_fixtures[self.class] @loaded_fixtures = @@already_loaded_fixtures[self.class] else @loaded_fixtures = load_fixtures @@already_loaded_fixtures[self.class] = @loaded_fixtures end ActiveRecord::Base.force_connect_all_shards! @fixture_connections = enlist_fixture_connections @fixture_connections.each do |connection| connection.increment_open_transactions connection.transaction_joinable = false connection.begin_db_transaction end # Load fixtures for every test. else ActiveRecord::Fixtures.reset_cache @@already_loaded_fixtures[self.class] = nil @loaded_fixtures = load_fixtures end # Instantiate fixtures for every test if requested. instantiate_fixtures if use_instantiated_fixtures end def enlist_fixture_connections ActiveRecord::Base.connection_handler.connection_pools.values.map(&:connection) + ActiveRecord::Base.turntable_connections.values.map(&:connection) end def teardown_fixtures return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? unless run_in_transaction? ActiveRecord::Fixtures.reset_cache end # Rollback changes if a transaction is active. if run_in_transaction? @fixture_connections.each do |connection| if connection.open_transactions != 0 connection.rollback_db_transaction connection.decrement_open_transactions end end @fixture_connections.clear end ActiveRecord::Base.clear_active_connections! end end end