Browse files

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
  • Loading branch information...
1 parent d91405a commit 2ec81dcd28ece609bf837b7869696bba0af5507b @dhh dhh committed Dec 22, 2004
Showing with 21 additions and 0 deletions.
  1. +3 −0 activerecord/CHANGELOG
  2. +18 −0 activerecord/lib/active_record/fixtures.rb
View
3 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]
View
18 activerecord/lib/active_record/fixtures.rb
@@ -166,12 +166,30 @@ def self.create_fixtures(fixtures_directory, *table_names)
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)

0 comments on commit 2ec81dc

Please sign in to comment.