mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed that fixtures were being deleted in the same order as inserts causing FK errors #890 [andrew.john.peters@gmail.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
b8c07c0325
commit
03097d3a60
11 changed files with 107 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Fixed that fixtures were being deleted in the same order as inserts causing FK errors #890 [andrew.john.peters@gmail.com]
|
||||
|
||||
* Fixed loading of fixtures in to be in the right order (or PostgreSQL would bark) #1047 [stephenh@chase3000.com]
|
||||
|
||||
* Fixed page caching for non-vhost applications living underneath the root #1004 [Ben Schumacher]
|
||||
|
|
|
@ -251,6 +251,8 @@ class Fixtures < Hash
|
|||
end
|
||||
end
|
||||
|
||||
attr_reader :table_name
|
||||
|
||||
def initialize(connection, table_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
|
||||
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
|
||||
@class_name = Inflector.classify(@table_name)
|
||||
|
@ -470,30 +472,35 @@ module Test #:nodoc:
|
|||
private
|
||||
def load_fixtures
|
||||
@loaded_fixtures = {}
|
||||
fixture_table_names.each do |table_name|
|
||||
@loaded_fixtures[table_name] = Fixtures.create_fixtures(fixture_path, table_name)
|
||||
fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names)
|
||||
unless fixtures.nil?
|
||||
if fixtures.instance_of?(Fixtures)
|
||||
@loaded_fixtures[fixtures.table_name] = fixtures
|
||||
else
|
||||
fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# for pre_loaded_fixtures, only require the classes once. huge speed improvement
|
||||
@@required_fixture_classes = false
|
||||
|
||||
|
||||
def instantiate_fixtures
|
||||
if pre_loaded_fixtures
|
||||
raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty?
|
||||
unless @@required_fixture_classes
|
||||
self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys
|
||||
self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys
|
||||
@@required_fixture_classes = true
|
||||
end
|
||||
Fixtures.instantiate_all_loaded_fixtures(self, load_instances?)
|
||||
else
|
||||
else
|
||||
raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil?
|
||||
@loaded_fixtures.each do |table_name, fixtures|
|
||||
Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def load_instances?
|
||||
use_instantiated_fixtures != :no_instances
|
||||
end
|
||||
|
|
|
@ -164,3 +164,14 @@ CREATE TABLE categories_posts (
|
|||
category_id int NOT NULL,
|
||||
post_id int NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_pk (
|
||||
id INTEGER NOT NULL PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_fk (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
fk_id INTEGER NOT NULL,
|
||||
|
||||
FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
|
||||
);
|
||||
|
|
|
@ -166,3 +166,14 @@ CREATE TABLE `categories_posts` (
|
|||
`category_id` int(11) NOT NULL,
|
||||
`post_id` int(11) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE `fk_test_has_pk` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE `fk_test_has_fk` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY,
|
||||
`fk_id` INTEGER NOT NULL,
|
||||
|
||||
FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`)
|
||||
);
|
||||
|
|
|
@ -202,3 +202,12 @@ create table categories_posts (
|
|||
category_id integer not null references developers initially deferred disable,
|
||||
post_id int integer not null references developers initially deferred disable
|
||||
);
|
||||
|
||||
create table fk_test_has_pk (
|
||||
id integer not null primary key
|
||||
);
|
||||
|
||||
create table fk_test_has_fk (
|
||||
id integer not null primary key,
|
||||
fk_id integer not null references fk_test_has_fk initially deferred disable,
|
||||
);
|
||||
|
|
|
@ -183,3 +183,12 @@ CREATE TABLE categories_posts (
|
|||
category_id integer NOT NULL,
|
||||
post_id integer NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_pk (
|
||||
id INTEGER NOT NULL PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_fk (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
fk_id INTEGER NOT NULL REFERENCES fk_test_has_fk(id)
|
||||
);
|
||||
|
|
|
@ -151,3 +151,14 @@ CREATE TABLE 'categories_posts' (
|
|||
'category_id' INTEGER NOT NULL,
|
||||
'post_id' INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE 'fk_test_has_pk' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE 'fk_test_has_fk' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY,
|
||||
'fk_id' INTEGER NOT NULL,
|
||||
|
||||
FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id')
|
||||
);
|
||||
|
|
|
@ -151,3 +151,14 @@ CREATE TABLE categories_posts (
|
|||
category_id int NOT NULL,
|
||||
post_id int NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_pk (
|
||||
id INTEGER NOT NULL PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE fk_test_has_fk (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
fk_id INTEGER NOT NULL,
|
||||
|
||||
FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
|
||||
);
|
||||
|
|
3
activerecord/test/fixtures/fk_test_has_fk.yml
vendored
Normal file
3
activerecord/test/fixtures/fk_test_has_fk.yml
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
first:
|
||||
id: 1
|
||||
fk_id: 1
|
2
activerecord/test/fixtures/fk_test_has_pk.yml
vendored
Normal file
2
activerecord/test/fixtures/fk_test_has_pk.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
first:
|
||||
id: 1
|
|
@ -179,3 +179,27 @@ class OverlappingFixturesTest < Test::Unit::TestCase
|
|||
assert_equal([:topics, :developers, :accounts], fixture_table_names)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class ForeignKeyFixturesTest < Test::Unit::TestCase
|
||||
fixtures :fk_test_has_pk, :fk_test_has_fk
|
||||
|
||||
# if foreign keys are implemented and fixtures
|
||||
# are not deleted in reverse order then this test
|
||||
# case will raise StatementInvalid
|
||||
|
||||
def test_number1
|
||||
assert true
|
||||
end
|
||||
|
||||
def test_number2
|
||||
assert true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue