diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-22 23:09:30 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-22 23:09:30 +0000 |
commit | 2ec81dcd28ece609bf837b7869696bba0af5507b (patch) | |
tree | e0e48c6db025d91a8a04518297f884cddada8838 /activerecord/lib/active_record | |
parent | d91405a415819a626427373437e0929b19914cf4 (diff) | |
download | rails-2ec81dcd28ece609bf837b7869696bba0af5507b.tar.gz rails-2ec81dcd28ece609bf837b7869696bba0af5507b.tar.bz2 rails-2ec81dcd28ece609bf837b7869696bba0af5507b.zip |
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
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 18 |
1 files changed, 18 insertions, 0 deletions
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) |