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

Fixes Model.limit(n).delete_all generates incorrect query #200

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

gurkanindibay
Copy link
Contributor

I implemented a hook to catch and add tenant into for the issue #195, however when adding a new where criteria, if an IN statement exist, the where criteria is being added into the query inside IN query, not in Delete statement.
I'm using Arel::DeleteManager to manipulate the delete statement.


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

if conditions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here stmt is type of Arel::DeleteManager. When I added tenant parameter, the new where criteria appears inside IN statement.

Copy link

codecov bot commented Dec 3, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (d91e62c) 83.31% compared to head (60bfde1) 84.04%.

Files Patch % Lines
lib/activerecord-multi-tenant/delete_operations.rb 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #200      +/-   ##
==========================================
+ Coverage   83.31%   84.04%   +0.73%     
==========================================
  Files          15       16       +1     
  Lines         725      746      +21     
==========================================
+ Hits          604      627      +23     
+ Misses        121      119       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gurkanindibay gurkanindibay marked this pull request as ready for review December 3, 2023 13:47
@gurkanindibay gurkanindibay requested a review from serprex December 3, 2023 13:48
@gurkanindibay
Copy link
Contributor Author

gurkanindibay commented Dec 3, 2023

@serprex can I get approval please?

@gurkanindibay gurkanindibay changed the title Failing multitenant implementation of Model delete with a join Fixes Model.limit(n).delete_all & Model.limit(n).update_all generates incorrect query Dec 3, 2023
# Check if the tenant key is present in the model's column names
tenant_condition = table[tenant_key].eq(tenant_id)
# Add the tenant condition to the arel query if it is not already present
unless arel.constraints.any? { |node| node.to_sql.include?(tenant_condition.to_sql) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

tenant_condition.to_sql can be hoisted out of any? loop

Copy link
Collaborator

@serprex serprex Dec 3, 2023

Choose a reason for hiding this comment

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

Granted, that will cause an unnecessary to_sql call when arel.constraints.empty?

Copy link
Contributor

@Amit909Singh Amit909Singh left a comment

Choose a reason for hiding this comment

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

Current changes may not address Model.limit(n).update_all. Should we address update_all as well or take it as a separate fix?

@gurkanindibay gurkanindibay merged commit 20d09a8 into master Dec 4, 2023
151 checks passed
@gurkanindibay gurkanindibay deleted the issue_195 branch December 4, 2023 05:43
@gurkanindibay
Copy link
Contributor Author

@Amit909Singh if you have solution, please address it in another PR
Thanks for your contributions

@gurkanindibay gurkanindibay changed the title Fixes Model.limit(n).delete_all & Model.limit(n).update_all generates incorrect query Fixes Model.limit(n).delete_all generates incorrect query Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants