Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify CASCADE and RESTRICT for Truncation strategy #99

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading