From 2487aab99a0c1767502c9f635102607fb4ded05d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 14 Feb 2011 11:25:19 -0800 Subject: fixtures will return a list of tables that may be effected, delete existing fixtures will delete those tables --- activerecord/lib/active_record/fixtures.rb | 31 ++++++++++++++++++++++++------ activerecord/test/cases/fixtures_test.rb | 5 +++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index feca1e0035..154c2d022c 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -590,8 +590,31 @@ class Fixtures fixtures.size end - def delete_existing_fixtures(table = table_name) - @connection.delete "DELETE FROM #{@connection.quote_table_name(table)}", 'Fixture Delete' + def delete_existing_fixtures + tables.each do |table| + @connection.delete "DELETE FROM #{@connection.quote_table_name(table)}", 'Fixture Delete' + end + end + + # Return a list of tables this fixture effect. This is typically the +table_name+ + # along with any habtm tables specified via Foxy Fixtures. + def tables + [table_name] + fixtures.values.map { |fixture| + row = fixture.to_hash + + # If STI is used, find the correct subclass for association reflection + associations = [] + if model_class && model_class < ActiveRecord::Base + reflection_class = row[inheritance_column_name].constantize rescue model_class + associations = reflection_class.reflect_on_all_associations + end + + foxy_habtms = associations.find_all { |assoc| + assoc.macro == :has_and_belongs_to_many && row.key?(assoc.name.to_s) + } + + foxy_habtms.map { |assoc| assoc.options[:join_table] } + }.flatten.uniq end def insert_fixtures @@ -667,10 +690,6 @@ class Fixtures @connection.insert_fixture(row, table_name) end - habtm_fixtures.keys.each do |table| - delete_existing_fixtures(table) - end - # insert any HABTM join tables we discovered habtm_fixtures.each do |table, fixtures| fixtures.each do |row| diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index ee51692f93..2a5d6b2beb 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -188,6 +188,11 @@ class FixturesTest < ActiveRecord::TestCase end end + def test_tables + fixtures = Fixtures.new(Parrot.connection, 'parrots', 'Parrot', FIXTURES_ROOT + "/parrots") + assert_equal %w{parrots parrots_treasures}, fixtures.tables + end + def test_yml_file_in_subdirectory assert_equal(categories(:sub_special_1).name, "A special category in a subdir file") assert_equal(categories(:sub_special_1).class, SpecialCategory) -- cgit v1.2.3 From ca0501676500471981806993e7ccd7536f7943f0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 14 Feb 2011 12:01:22 -0800 Subject: extract rows that should be inserted to a method --- activerecord/lib/active_record/fixtures.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 154c2d022c..75c5daaaae 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -617,7 +617,9 @@ class Fixtures }.flatten.uniq end - def insert_fixtures + # Return a hash of rows to be inserted. The key is the table, the value is + # a list of rows to insert to that table. + def rows now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now now = now.to_s(:db) @@ -625,9 +627,9 @@ class Fixtures fixtures.delete('DEFAULTS') # track any join tables we need to insert later - habtm_fixtures = Hash.new { |h,k| h[k] = [] } + rows = Hash.new { |h,table| h[table] = [] } - rows = fixtures.map do |label, fixture| + rows[table_name] = fixtures.map do |label, fixture| row = fixture.to_hash if model_class && model_class < ActiveRecord::Base @@ -674,7 +676,7 @@ class Fixtures if (targets = row.delete(association.name.to_s)) targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) table_name = association.options[:join_table] - habtm_fixtures[table_name].concat targets.map { |target| + rows[table_name].concat targets.map { |target| { association.foreign_key => row[primary_key_name], association.association_foreign_key => Fixtures.identify(target) } } @@ -685,15 +687,13 @@ class Fixtures row end + rows + end - rows.each do |row| - @connection.insert_fixture(row, table_name) - end - - # insert any HABTM join tables we discovered - habtm_fixtures.each do |table, fixtures| - fixtures.each do |row| - @connection.insert_fixture(row, table) + def insert_fixtures + rows.each do |table_name, rows| + rows.each do |row| + @connection.insert_fixture(row, table_name) end end end -- cgit v1.2.3 From fd81c700ec4ff7a8170e5497ecc351d76fa50b84 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 14 Feb 2011 14:53:42 -0800 Subject: extract database activity out of Fixtures instances --- activerecord/lib/active_record/fixtures.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 75c5daaaae..97d814eb71 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -504,8 +504,12 @@ 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?('/') } - connection = block_given? ? yield : ActiveRecord::Base.connection + 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) } @@ -513,7 +517,7 @@ class Fixtures connection.disable_referential_integrity do fixtures_map = {} - fixtures = files_to_read.map do |path| + fixture_files = files_to_read.map do |path| table_name = path.tr '/', '_' fixtures_map[path] = Fixtures.new( @@ -526,8 +530,20 @@ class Fixtures all_loaded_fixtures.update(fixtures_map) connection.transaction(:requires_new => true) do - fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures } - fixtures.each { |fixture| fixture.insert_fixtures } + 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!) @@ -619,7 +635,7 @@ class Fixtures # Return a hash of rows to be inserted. The key is the table, the value is # a list of rows to insert to that table. - def rows + def table_rows now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now now = now.to_s(:db) @@ -691,7 +707,7 @@ class Fixtures end def insert_fixtures - rows.each do |table_name, rows| + table_rows.each do |table_name, rows| rows.each do |row| @connection.insert_fixture(row, table_name) end -- cgit v1.2.3 From f9ea47736e270152c264bb5f8fdbfaa1d04fe82f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 14 Feb 2011 14:55:01 -0800 Subject: remove unused methods --- activerecord/lib/active_record/fixtures.rb | 35 ------------------------------ activerecord/test/cases/fixtures_test.rb | 5 ----- 2 files changed, 40 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 97d814eb71..3876e57210 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -606,33 +606,6 @@ class Fixtures fixtures.size end - def delete_existing_fixtures - tables.each do |table| - @connection.delete "DELETE FROM #{@connection.quote_table_name(table)}", 'Fixture Delete' - end - end - - # Return a list of tables this fixture effect. This is typically the +table_name+ - # along with any habtm tables specified via Foxy Fixtures. - def tables - [table_name] + fixtures.values.map { |fixture| - row = fixture.to_hash - - # If STI is used, find the correct subclass for association reflection - associations = [] - if model_class && model_class < ActiveRecord::Base - reflection_class = row[inheritance_column_name].constantize rescue model_class - associations = reflection_class.reflect_on_all_associations - end - - foxy_habtms = associations.find_all { |assoc| - assoc.macro == :has_and_belongs_to_many && row.key?(assoc.name.to_s) - } - - foxy_habtms.map { |assoc| assoc.options[:join_table] } - }.flatten.uniq - end - # Return a hash of rows to be inserted. The key is the table, the value is # a list of rows to insert to that table. def table_rows @@ -706,14 +679,6 @@ class Fixtures rows end - def insert_fixtures - table_rows.each do |table_name, rows| - rows.each do |row| - @connection.insert_fixture(row, table_name) - end - end - end - private def primary_key_name @primary_key_name ||= model_class && model_class.primary_key diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 2a5d6b2beb..ee51692f93 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -188,11 +188,6 @@ class FixturesTest < ActiveRecord::TestCase end end - def test_tables - fixtures = Fixtures.new(Parrot.connection, 'parrots', 'Parrot', FIXTURES_ROOT + "/parrots") - assert_equal %w{parrots parrots_treasures}, fixtures.tables - end - def test_yml_file_in_subdirectory assert_equal(categories(:sub_special_1).name, "A special category in a subdir file") assert_equal(categories(:sub_special_1).class, SpecialCategory) -- cgit v1.2.3