Skip to content

Commit

Permalink
PostgreSQL requires all tables with FKs to be truncates in the same
Browse files Browse the repository at this point in the history
command, or have the CASCADE keyword appended.
  • Loading branch information
japgolly committed Feb 20, 2012
1 parent 11d4b72 commit 6134dbc
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/database_cleaner/sequel/truncation.rb
Expand Up @@ -8,8 +8,21 @@ class Truncation
include ::DatabaseCleaner::Generic::Truncation

def clean
each_table do |db, table|
db[table].truncate
case db_type= db.url.sub(/:.+/,'').to_sym
when :postgres
# PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
# appended. Bulk truncation without CASCADE is:
# * Safer. Tables outside of tables_to_truncate won't be affected.
# * Faster. Less roundtrips to the db.
unless (tables= tables_to_truncate(db)).empty?
all_tables= tables.map{|t| %["#{t}"]}.join ','
db.run "TRUNCATE TABLE #{all_tables};"
end
else
# Truncate each table normally
each_table do |db, table|
db[table].truncate
end
end
end

Expand Down

0 comments on commit 6134dbc

Please sign in to comment.