Skip to content
Browse files

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
  • Loading branch information...
1 parent d4d981d commit 79e85edd71a4c886b1ac074988963588d8c45755 @dhh dhh committed Dec 1, 2004
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*CVS*
+* Added reverse order of deleting fixtures, so referential keys can be maintained #247 [Tim Bates]
+
* Added relative path search for sqlite dbfiles in database.yml (if RAILS_ROOT is defined) #233 [bitsweat]
* Added option to establish_connection where you'll be able to leave out the parameter to have it use the RAILS_ENV environment variable
View
31 activerecord/lib/active_record/fixtures.rb
@@ -63,10 +63,13 @@ def self.create_fixtures(fixtures_directory, *table_names)
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 @@ def initialize(connection, table_name, fixture_path, file_filter = /^\.|CVS|\.ym
@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 @@ def read_fixture_files
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 @@ def instantiate_fixtures(*table_names)
def fixture_table_names
self.class.read_inheritable_attribute("fixture_table_names")
end
-end
+end
View
8 activerecord/test/associations_test.rb
@@ -18,7 +18,7 @@ class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
class AssociationsTest < Test::Unit::TestCase
def setup
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
+ create_fixtures "accounts", "companies", "developers", "projects", "developers_projects"
@signals37 = Firm.find(1)
end
@@ -64,7 +64,7 @@ def test_storing_in_pstore
class HasOneAssociationsTest < Test::Unit::TestCase
def setup
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
+ create_fixtures "accounts", "companies", "developers", "projects", "developers_projects"
@signals37 = Firm.find(1)
end
@@ -141,7 +141,7 @@ def test_dependence_with_missing_association
class HasManyAssociationsTest < Test::Unit::TestCase
def setup
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
+ create_fixtures "accounts", "companies", "developers", "projects", "developers_projects", "topics"
@signals37 = Firm.find(1)
end
@@ -319,7 +319,7 @@ def test_adding_array_and_collection
class BelongsToAssociationsTest < Test::Unit::TestCase
def setup
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
+ create_fixtures "accounts", "companies", "developers", "projects", "developers_projects", "topics"
@signals37 = Firm.find(1)
end
View
4 activerecord/test/deprecated_associations_test.rb
@@ -18,7 +18,7 @@ class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
class DeprecatedAssociationsTest < Test::Unit::TestCase
def setup
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
+ create_fixtures "accounts", "companies", "developers", "projects", "developers_projects", "topics"
@signals37 = Firm.find(1)
end
@@ -332,4 +332,4 @@ def test_has_many_find_all
assert_equal 2, Firm.find_first.find_all_in_clients("type = 'Client'").length
assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
end
-end
+end
View
4 activerecord/test/fixtures_test.rb
@@ -4,7 +4,7 @@
require 'fixtures/company'
class FixturesTest < Test::Unit::TestCase
- fixtures :topics, :developers, :accounts, :developers
+ fixtures :topics, :developers, :accounts
FIXTURES = %w( accounts companies customers
developers developers_projects entrants
@@ -81,4 +81,4 @@ def test_erb_in_fixtures
assert_equal 10, @developers.size
assert_equal "fixture_5", @dev_5.name
end
-end
+end

0 comments on commit 79e85ed

Please sign in to comment.
Something went wrong with that request. Please try again.