aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG3
-rwxr-xr-xactiverecord/lib/active_record/fixtures.rb18
2 files changed, 21 insertions, 0 deletions
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)