Class: WorkerPlugins::RemoveQuery
Instance Method Summary
collapse
#db_now_value, #mysql?, #postgres?, #quote, #quote_column, #quote_table, #sqlite?
Instance Method Details
#links_scope ⇒ Object
8
9
10
11
12
13
|
# File 'app/services/worker_plugins/remove_query.rb', line 8
def links_scope
scope = workplace.workplace_links.where(resource_type: model_class.name)
return scope if unscoped_query?
scope.where(resource_id: query_with_selected_ids)
end
|
#model_class ⇒ Object
37
38
39
|
# File 'app/services/worker_plugins/remove_query.rb', line 37
def model_class
query.klass
end
|
4
5
6
|
# File 'app/services/worker_plugins/remove_query.rb', line 4
def perform
succeed!(affected_count: links_scope.delete_all)
end
|
#query_with_selected_ids ⇒ Object
#unscoped_query? ⇒ Boolean
If the caller’s query has no meaningful scoping applied, the ‘resource_id IN (SELECT … FROM <target_table>)` subquery would simply materialize every row of the target model — for 340k+ users that’s a full-table scan with no semantic effect other than preserving orphaned links. The ‘resource_type = ?` filter alone is enough to pin the DELETE to this workplace’s links of the given type, so we short-circuit the subquery in that case. Orphaned links (whose resource row has since been deleted) are deleted alongside live ones, which matches caller intent (“remove everything matching the query”) and is the correct thing to do with dead references anyway.
25
26
27
28
29
30
31
32
33
34
35
|
# File 'app/services/worker_plugins/remove_query.rb', line 25
def unscoped_query?
@query.where_clause.empty? &&
@query.joins_values.empty? &&
@query.left_outer_joins_values.empty? &&
@query.group_values.empty? &&
@query.having_clause.empty? &&
@query.limit_value.nil? &&
@query.offset_value.nil? &&
@query.from_clause.value.nil? &&
@query.with_values.empty?
end
|