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
This commit is contained in:
David Heinemeier Hansson 2004-12-22 23:09:30 +00:00
parent d91405a415
commit 2ec81dcd28
2 changed files with 21 additions and 0 deletions

View File

@ -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 File

@ -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)