Skip to content

Commit 60bfde1

Browse files
Amit909Singhgurkanindibay
authored andcommitted
Users/amit909sin/issue 195 (#219)
* Fix the tenant scoping for delete_all
1 parent 1cb31ae commit 60bfde1

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

lib/activerecord-multi-tenant/delete_operations.rb

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1+
# frozen_string_literal: true
2+
13
module Arel
24
module ActiveRecordRelationExtension
3-
def delete_all(conditions = nil)
4-
tenant_id = MultiTenant.current_tenant_id
5-
tenant_key = MultiTenant.partition_key(MultiTenant.current_tenant_class)
5+
# Overrides the delete_all method to include tenant scoping
6+
def delete_all
7+
# Call the original delete_all method if the current tenant is identified by an ID
8+
return super if MultiTenant.current_tenant_is_id? || MultiTenant.current_tenant.nil?
69

10+
tenant_key = MultiTenant.partition_key(MultiTenant.current_tenant_class)
11+
tenant_id = MultiTenant.current_tenant_id
712
arel = eager_loading? ? apply_join_dependency.arel : build_arel
813
arel.source.left = table
914

10-
group_values_arel_columns = arel_columns(group_values.uniq)
11-
having_clause_ast = having_clause.ast unless having_clause.empty?
12-
stmt = arel.compile_delete(table[primary_key], having_clause_ast, group_values_arel_columns)
13-
14-
if tenant_id
15-
tenant_condition = table[tenant_key.downcase].eq(tenant_id)
16-
account_condition = table["account_id"].eq(tenant_id)
17-
conditions = Arel::Nodes::And.new([tenant_condition, conditions].compact)
18-
puts "conditions: #{conditions.to_sql}"
19-
puts "tenant_id: #{tenant_id}"
15+
if tenant_id && klass.column_names.include?(tenant_key)
16+
# Check if the tenant key is present in the model's column names
17+
tenant_condition = table[tenant_key].eq(tenant_id)
18+
# Add the tenant condition to the arel query if it is not already present
19+
unless arel.constraints.any? { |node| node.to_sql.include?(tenant_condition.to_sql) }
20+
arel = arel.where(tenant_condition)
21+
end
2022
end
2123

22-
puts "stmt klass: #{stmt.class}"
23-
24-
if conditions
25-
stmt.where(conditions)
26-
end
24+
subquery = arel.clone
25+
subquery.projections.clear
26+
subquery = subquery.project(table[primary_key])
27+
in_condition = Arel::Nodes::In.new(table[primary_key], subquery.ast)
28+
stmt = Arel::DeleteManager.new.from(table)
29+
stmt.wheres = [in_condition]
2730

28-
puts "stmtt: #{stmt.to_sql}"
31+
# Execute the delete statement using the connection and return the result
2932
klass.connection.delete(stmt, "#{klass} Delete All").tap { reset }
3033
end
3134
end

lib/activerecord-multi-tenant/multi_tenant.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.tenant_klass_defined?(tenant_name, options = {})
1717
end
1818

1919
def self.partition_key(tenant_name)
20-
"#{tenant_name}_id"
20+
"#{tenant_name.to_s.underscore}_id"
2121
end
2222

2323
# rubocop:disable Style/ClassVars

spec/activerecord-multi-tenant/query_rewriter_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,26 @@
5757
end
5858

5959
it 'delete_all the records' do
60-
6160
expected_query = <<-SQL.strip
6261
DELETE FROM "projects" WHERE "projects"."id" IN
6362
(SELECT "projects"."id" FROM "projects"
6463
INNER JOIN "managers" ON "managers"."project_id" = "projects"."id"
6564
and "managers"."account_id" = :account_id
6665
WHERE "projects"."account_id" = :account_id
6766
)
67+
AND "projects"."account_id" = :account_id
6868
SQL
6969

7070
expect do
7171
MultiTenant.with(account) do
7272
Project.joins(:manager).delete_all
7373
end
7474
end.to change { Project.count }.from(3).to(1)
75-
query_index = 0
76-
@queries.each_with_index do |actual_query, index|
75+
76+
@queries.each do |actual_query|
7777
next unless actual_query.include?('DELETE FROM ')
7878

7979
expect(format_sql(actual_query)).to eq(format_sql(expected_query.gsub(':account_id', account.id.to_s)))
80-
query_index += 1
8180
end
8281
end
8382

0 commit comments

Comments
 (0)