From 03097d3a6047e269641a14f4c93838598011f0b5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 18 Apr 2005 07:52:58 +0000 Subject: 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 --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/fixtures.rb | 21 ++++++++++++------- activerecord/test/fixtures/db_definitions/db2.sql | 11 ++++++++++ .../test/fixtures/db_definitions/mysql.sql | 11 ++++++++++ activerecord/test/fixtures/db_definitions/oci.sql | 9 ++++++++ .../test/fixtures/db_definitions/postgresql.sql | 9 ++++++++ .../test/fixtures/db_definitions/sqlite.sql | 11 ++++++++++ .../test/fixtures/db_definitions/sqlserver.sql | 11 ++++++++++ activerecord/test/fixtures/fk_test_has_fk.yml | 3 +++ activerecord/test/fixtures/fk_test_has_pk.yml | 2 ++ activerecord/test/fixtures_test.rb | 24 ++++++++++++++++++++++ 11 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 activerecord/test/fixtures/fk_test_has_fk.yml create mode 100644 activerecord/test/fixtures/fk_test_has_pk.yml (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index abcd70cfc6..e9a23dc20a 100644 --- a/activerecord/CHANGELOG +++ b/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] diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 100908cafb..50a824987d 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -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 diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 35efa3f6c7..1097568ce3 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/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) +); diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 294f227be3..da4a69252f 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/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`) +); diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index 29e6c76e2a..62f9ae9fb4 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/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, +); diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 2bc18dfb2b..9b57866368 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/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) +); diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 33c85b4d1c..4507e2c4b2 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/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') +); diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index c6cec8312d..776ac23610 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/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) +); diff --git a/activerecord/test/fixtures/fk_test_has_fk.yml b/activerecord/test/fixtures/fk_test_has_fk.yml new file mode 100644 index 0000000000..67d914e130 --- /dev/null +++ b/activerecord/test/fixtures/fk_test_has_fk.yml @@ -0,0 +1,3 @@ +first: + id: 1 + fk_id: 1 diff --git a/activerecord/test/fixtures/fk_test_has_pk.yml b/activerecord/test/fixtures/fk_test_has_pk.yml new file mode 100644 index 0000000000..c93952180b --- /dev/null +++ b/activerecord/test/fixtures/fk_test_has_pk.yml @@ -0,0 +1,2 @@ +first: + id: 1 \ No newline at end of file diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index 687ce817f6..e1f2f6d24c 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -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 + + + + + + -- cgit v1.2.3