Skip to content

Commit

Permalink
Add option to specify CASCADE and RESTRICT for Truncation strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
thegeorgeous committed Feb 20, 2024
1 parent aafa5b2 commit cbe6044
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deleti

* `:reset_ids` - Only valid for deletion strategy, when set to `true` resets ids to 1 after each table is cleaned.

* `:truncate_option` - Only valid for PostgreSQL. Acceptable values are `:restrict` and `:cascade`. Default is `:restrict`

## Adapter configuration options

`#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:
Expand Down
14 changes: 8 additions & 6 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DatabaseCleaner
module ActiveRecord
class Truncation < Base
def initialize(opts={})
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids]).empty?
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids, :truncate_option]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, and :cache_tables. You specified #{opts.keys.join(',')}."
end

Expand All @@ -14,6 +14,7 @@ def initialize(opts={})

@reset_ids = opts[:reset_ids]
@pre_count = opts[:pre_count]
@truncate_option = opts[:cascade] || :restrict
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end

Expand All @@ -22,7 +23,7 @@ def clean
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
connection.pre_count_truncate_tables(tables_to_truncate(connection))
else
connection.truncate_tables(tables_to_truncate(connection))
connection.truncate_tables(tables_to_truncate(connection), { truncate_option: @truncate_option })
end
end
end
Expand Down Expand Up @@ -96,7 +97,7 @@ def truncate_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)}")
end

def truncate_tables(tables)
def truncate_tables(tables, opts)
tables.each { |t| truncate_table(t) }
end
end
Expand Down Expand Up @@ -146,7 +147,7 @@ def truncate_table(table_name)
end
end

def truncate_tables(tables)
def truncate_tables(tables, opts)
tables.each { |t| truncate_table(t) }
end

Expand Down Expand Up @@ -187,9 +188,10 @@ def database_tables
tables_with_schema
end

def truncate_tables(table_names)
def truncate_tables(table_names, opts)
return if table_names.nil? || table_names.empty?
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY RESTRICT;")

execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY #{opts[:truncate_option]};")
end

def pre_count_truncate_tables(tables)
Expand Down

0 comments on commit cbe6044

Please sign in to comment.