diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index 78183e8..618d06e 100755 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -172,9 +172,15 @@ module DatabaseCleaner # but then the table is cleaned. In other words, this function tells us if the given table # was ever inserted into. def has_been_used?(table) + return has_rows?(table) unless has_sequence?(table) + cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0 cur_val > 0 end + + def has_sequence?(table) + select_value("SELECT true FROM pg_class WHERE relname = '#{table}_id_seq';") + end def has_rows?(table) select_value("SELECT true FROM #{table} LIMIT 1;") diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 6a479d0..8ad59fe 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -25,6 +25,13 @@ module ActiveRecord User.count.should eq 0 end + it "truncates the table without id sequence" do + 2.times { Agent.create } + + connection.truncate_table('agents') + Agent.count.should eq 0 + end + it "resets AUTO_INCREMENT index of table" do 2.times { User.create } User.delete_all @@ -48,4 +55,3 @@ module ActiveRecord end end end - diff --git a/spec/support/active_record/schema_setup.rb b/spec/support/active_record/schema_setup.rb index 39aacb4..59d62ac 100644 --- a/spec/support/active_record/schema_setup.rb +++ b/spec/support/active_record/schema_setup.rb @@ -3,8 +3,15 @@ def active_record_load_schema create_table :users, :force => true do |t| t.integer :name end + + create_table :agents, :id => false, :force => true do |t| + t.integer :name + end end end class ::User < ActiveRecord::Base end + +class ::Agent < ActiveRecord::Base +end