From 79e85edd71a4c886b1ac074988963588d8c45755 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 1 Dec 2004 12:25:04 +0000 Subject: Added reverse order of deleting fixtures, so referential keys can be maintained #247 [Tim Bates] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@38 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/fixtures.rb | 31 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index f17768e1f2..df41a1e370 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -63,10 +63,13 @@ class Fixtures < Hash begin ActiveRecord::Base.logger.level = Logger::ERROR - fixtures = connection.transaction do - table_names.flatten.map do |table_name| + fixtures = [] + connection.transaction do + fixtures = table_names.flatten.map do |table_name| Fixtures.new(connection, table_name.to_s, File.join(fixtures_directory, table_name.to_s)) end + fixtures.reverse.each{ |fixture| fixture.delete_existing_fixtures } + fixtures.each{ |fixture| fixture.insert_fixtures } end return fixtures.size > 1 ? fixtures : fixtures.first ensure @@ -79,8 +82,16 @@ class Fixtures < Hash @class_name = Inflector.classify(@table_name) read_fixture_files - delete_existing_fixtures - insert_fixtures + end + + def delete_existing_fixtures + @connection.delete "DELETE FROM #{@table_name}" + end + + def insert_fixtures + values.each do |fixture| + @connection.execute "INSERT INTO #{@table_name} (#{fixture.key_list}) VALUES(#{fixture.value_list})" + end end private @@ -96,16 +107,6 @@ class Fixtures < Hash end end - def delete_existing_fixtures - @connection.delete "DELETE FROM #{@table_name}" - end - - def insert_fixtures - values.each do |fixture| - @connection.execute "INSERT INTO #{@table_name} (#{fixture.key_list}) VALUES(#{fixture.value_list})" - end - end - def yaml_file_path @fixture_path + ".yml" end @@ -205,4 +206,4 @@ class Test::Unit::TestCase #:nodoc: def fixture_table_names self.class.read_inheritable_attribute("fixture_table_names") end -end \ No newline at end of file +end -- cgit v1.2.3