Skip to content

Commit

Permalink
Fixed that fixtures were being deleted in the same order as inserts c…
Browse files Browse the repository at this point in the history
…ausing FK errors #890 [andrew.john.peters@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Apr 18, 2005
1 parent b8c07c0 commit 03097d3
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 7 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -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]
Expand Down
21 changes: 14 additions & 7 deletions activerecord/lib/active_record/fixtures.rb
Expand Up @@ -251,6 +251,8 @@ def self.reset_sequences(connection, table_names)
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)
Expand Down Expand Up @@ -470,30 +472,35 @@ def self.method_added(method)
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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/fixtures/db_definitions/db2.sql
Expand Up @@ -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)
);
11 changes: 11 additions & 0 deletions activerecord/test/fixtures/db_definitions/mysql.sql
Expand Up @@ -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`)
);
9 changes: 9 additions & 0 deletions activerecord/test/fixtures/db_definitions/oci.sql
Expand Up @@ -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,
);
9 changes: 9 additions & 0 deletions activerecord/test/fixtures/db_definitions/postgresql.sql
Expand Up @@ -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)
);
11 changes: 11 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlite.sql
Expand Up @@ -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')
);
11 changes: 11 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlserver.sql
Expand Up @@ -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 changes: 3 additions & 0 deletions activerecord/test/fixtures/fk_test_has_fk.yml
@@ -0,0 +1,3 @@
first:
id: 1
fk_id: 1
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/fk_test_has_pk.yml
@@ -0,0 +1,2 @@
first:
id: 1
24 changes: 24 additions & 0 deletions activerecord/test/fixtures_test.rb
Expand Up @@ -179,3 +179,27 @@ def test_fixture_table_names
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






0 comments on commit 03097d3

Please sign in to comment.