From 2ec81dcd28ece609bf837b7869696bba0af5507b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 22 Dec 2004 23:09:30 +0000 Subject: Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case. This only works for auto-incrementing primary keys called "id" for now #359 [Scott Baron] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@257 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 3 +++ activerecord/lib/active_record/fixtures.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index de7ccd163d..1de73028d2 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case. + This only works for auto-incrementing primary keys called "id" for now #359 [Scott Baron] + * Added Base#clear_association_cache to empty all the cached associations #347 [Tobias Luetke] * Added more informative exceptions in establish_connection #356 [bitsweat] diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 248cc170ab..21d0615dfa 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -166,12 +166,30 @@ class Fixtures < Hash fixtures.each { |fixture| fixture.insert_fixtures } end + reset_sequences(connection, table_names) if ActiveRecord::ConnectionAdapters::PostgreSQLAdapter === connection + return fixtures.size > 1 ? fixtures : fixtures.first ensure ActiveRecord::Base.logger.level = old_logger_level end end + # Work around for PostgreSQL to have new fixtures created from id 1 and running. + def self.reset_sequences(connection, table_names) + table_names.flatten.each do |table| + table_class = Inflector.classify(table.to_s) + if Object.const_defined?(table_class) + pk = eval("#{table_class}::primary_key") + if pk == 'id' + connection.execute( + "SELECT setval('public.#{table.to_s}_id_seq', (SELECT MAX(id) FROM #{table.to_s}), true)", + 'Setting Sequence' + ) + end + end + end + end + 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) -- cgit v1.2.3