Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed that fixtures were being deleted in the same order as inserts c…

…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...
commit 03097d3a6047e269641a14f4c93838598011f0b5 1 parent b8c07c0
@dhh dhh authored
View
2  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]
View
21 activerecord/lib/active_record/fixtures.rb
@@ -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)
@@ -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
View
11 activerecord/test/fixtures/db_definitions/db2.sql
@@ -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)
+);
View
11 activerecord/test/fixtures/db_definitions/mysql.sql
@@ -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`)
+);
View
9 activerecord/test/fixtures/db_definitions/oci.sql
@@ -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,
+);
View
9 activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -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)
+);
View
11 activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -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')
+);
View
11 activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -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)
+);
View
3  activerecord/test/fixtures/fk_test_has_fk.yml
@@ -0,0 +1,3 @@
+first:
+ id: 1
+ fk_id: 1
View
2  activerecord/test/fixtures/fk_test_has_pk.yml
@@ -0,0 +1,2 @@
+first:
+ id: 1
View
24 activerecord/test/fixtures_test.rb
@@ -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
+
+
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.