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 support for horizontal sharding for Rails 6.1+ #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions lib/database_cleaner/active_record/deletion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module DatabaseCleaner
module ActiveRecord
class Deletion < Truncation
def clean
connection.disable_referential_integrity do
if pre_count? && connection.respond_to?(:pre_count_tables)
delete_tables(connection, connection.pre_count_tables(tables_to_clean(connection)))
else
delete_tables(connection, tables_to_clean(connection))
with_all_databases do |connection|
connection.disable_referential_integrity do
if pre_count? && connection.respond_to?(:pre_count_tables)
delete_tables(connection, connection.pre_count_tables(tables_to_clean(connection)))
else
delete_tables(connection, tables_to_clean(connection))
end
end
end
end
Expand Down
30 changes: 18 additions & 12 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ def initialize(opts={})
end

def clean
connection.disable_referential_integrity do
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))
with_all_databases do |connection|
connection.disable_referential_integrity do
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))
end
end
end
end

private

def connection
@connection ||= ConnectionWrapper.new(
if ::ActiveRecord.version >= Gem::Version.new("7.2")
connection_class.lease_connection
else
connection_class.connection
def with_all_databases
if ::ActiveRecord.version >= Gem::Version.new("6.1")
connection_class.connection_handler.connection_pools.each do |pool|
pool.with_connection do |connection|
connection = ConnectionWrapper.new(connection)
yield connection
end
end
)
else
connection = ConnectionWrapper.new(connection_class.connection)
yield connection
end
end

def tables_to_clean(connection)
Expand Down