Skip to content

Fixes #62 #119

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

Closed
wants to merge 2 commits into from
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Development (unreleased)

* Fixes issue with CASCADE vs RESTRICT: https://github.com/DatabaseCleaner/database_cleaner-active_record/issues/62

## v2.2.0 2024-07-12

* Fix "ERROR: currval of sequence" in Postgres adapter: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/103
Expand Down
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.

* `:restrict` - Only valid for truncation strategy, when set to `true` it uses RESTRICT, otherwise it uses CASCADE. By default it uses CASCADE.

## Adapter configuration options

`#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:
Expand Down
10 changes: 7 additions & 3 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ 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, :restrict]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, :restrict, and :cache_tables. 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]
@restrict = !!opts[:restrict]
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end

Expand Down Expand Up @@ -194,7 +195,10 @@ def database_tables

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

restrict_or_cascade = @restrict ? "RESTRICT" : "CASCADE"

execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY #{restrict_or_cascade};")
end

def pre_count_truncate_tables(tables)
Expand Down
Loading