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
This commit is contained in:
David Heinemeier Hansson 2004-12-01 12:25:04 +00:00
parent d4d981d707
commit 79e85edd71
5 changed files with 26 additions and 23 deletions

View File

@ -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 File

@ -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
end

View File

@ -18,7 +18,7 @@ raise "ActiveRecord should have barked on bad collection keys" unless bad_collec
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 @@ end
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 @@ end
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 @@ end
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 File

@ -18,7 +18,7 @@ raise "ActiveRecord should have barked on bad collection keys" unless bad_collec
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 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
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 File

@ -4,7 +4,7 @@ require 'fixtures/developer'
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 @@ class FixturesTest < Test::Unit::TestCase
assert_equal 10, @developers.size
assert_equal "fixture_5", @dev_5.name
end
end
end