Skip to content

Commit

Permalink
Adds initial implementation but still failing
Browse files Browse the repository at this point in the history
  • Loading branch information
gurkanindibay committed Dec 3, 2023
1 parent d91e62c commit 1cb31ae
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/activerecord-multi-tenant/delete_operations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Arel
module ActiveRecordRelationExtension
def delete_all(conditions = nil)
tenant_id = MultiTenant.current_tenant_id
tenant_key = MultiTenant.partition_key(MultiTenant.current_tenant_class)

arel = eager_loading? ? apply_join_dependency.arel : build_arel
arel.source.left = table

group_values_arel_columns = arel_columns(group_values.uniq)
having_clause_ast = having_clause.ast unless having_clause.empty?
stmt = arel.compile_delete(table[primary_key], having_clause_ast, group_values_arel_columns)

if tenant_id
tenant_condition = table[tenant_key.downcase].eq(tenant_id)
account_condition = table["account_id"].eq(tenant_id)
conditions = Arel::Nodes::And.new([tenant_condition, conditions].compact)
puts "conditions: #{conditions.to_sql}"
puts "tenant_id: #{tenant_id}"
end

puts "stmt klass: #{stmt.class}"

if conditions
stmt.where(conditions)
end

puts "stmtt: #{stmt.to_sql}"
klass.connection.delete(stmt, "#{klass} Delete All").tap { reset }
end
end
end

# Patch ActiveRecord::Relation with the extension module
ActiveRecord::Relation.prepend(Arel::ActiveRecordRelationExtension)
1 change: 1 addition & 0 deletions lib/activerecord_multi_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
require_relative 'activerecord-multi-tenant/table_node'
require_relative 'activerecord-multi-tenant/version'
require_relative 'activerecord-multi-tenant/habtm'
require_relative 'activerecord-multi-tenant/delete_operations'
28 changes: 28 additions & 0 deletions spec/activerecord-multi-tenant/query_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,40 @@
let!(:manager1) { Manager.create(name: 'Manager 1', project: project1, account: account) }
let!(:manager2) { Manager.create(name: 'Manager 2', project: project2, account: account) }

before(:each) do
@queries = []
ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _started, _finished, _unique_id, payload|
@queries << payload[:sql]
end
end

after(:each) do
ActiveSupport::Notifications.unsubscribe('sql.active_record')
end

it 'delete_all the records' do

expected_query = <<-SQL.strip
DELETE FROM "projects" WHERE "projects"."id" IN
(SELECT "projects"."id" FROM "projects"
INNER JOIN "managers" ON "managers"."project_id" = "projects"."id"
and "managers"."account_id" = :account_id
WHERE "projects"."account_id" = :account_id
)
SQL

expect do
MultiTenant.with(account) do
Project.joins(:manager).delete_all
end
end.to change { Project.count }.from(3).to(1)
query_index = 0
@queries.each_with_index do |actual_query, index|
next unless actual_query.include?('DELETE FROM ')

expect(format_sql(actual_query)).to eq(format_sql(expected_query.gsub(':account_id', account.id.to_s)))
query_index += 1
end
end

it 'delete_all the records without a current tenant' do
Expand Down

0 comments on commit 1cb31ae

Please sign in to comment.