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 #115

Closed
Closed
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
17 changes: 10 additions & 7 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require "delegate"
require 'database_cleaner/active_record/base'

module DatabaseCleaner
module ActiveRecord
class Truncation < Base
def initialize(opts={})
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, and :cache_tables. You specified #{opts.keys.join(',')}."
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, :cache_tables and :truncate_option. You specified #{opts.keys.join(',')}."
end

@only = Array(opts[:only]).dup
@except = Array(opts[:except]).dup

@reset_ids = opts[:reset_ids]
@pre_count = opts[:pre_count]
@truncate_option = opts[:cascade] || :restrict
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this should be opts[:truncate_option] rather than opts[:cascade] (I know this is from the original PR - just want to make sure it gets addressed before merging!)

@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end

Expand All @@ -21,7 +23,7 @@ def clean
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
connection.pre_count_truncate_tables(tables_to_clean(connection))
else
connection.truncate_tables(tables_to_clean(connection))
connection.truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option })
end
end
end
Expand Down Expand Up @@ -101,7 +103,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 @@ -151,7 +153,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 @@ -192,9 +194,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