Merge pull request #283 from Strech/master
PostgreSQL truncation for tables without sequence
This commit is contained in:
commit
3b109a1ca8
|
@ -172,10 +172,16 @@ 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;")
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue