Skip to content

Commit 740baee

Browse files
committed
Fixes #39274 - Fix duplicate hosts in job_invocation scoped searches
1 parent 660c0a2 commit 740baee

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

app/models/concerns/foreman_remote_execution/host_extensions.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ def self.prepended(base)
3232

3333
def search_by_job_invocation(key, operator, value)
3434
if key == 'job_invocation.result'
35-
operator = operator == '=' ? 'IN' : 'NOT IN'
36-
value = TemplateInvocation::TaskResultMap.status_to_task_result(value)
37-
end
35+
in_operator = operator == '=' ? 'IN' : 'NOT IN'
36+
task_results = TemplateInvocation::TaskResultMap.status_to_task_result(value)
37+
subquery = TemplateInvocation.joins(:run_host_job_task)
38+
.where.not(host_id: nil)
39+
.where("#{ForemanTasks::Task.table_name}.result IN (?)", task_results)
40+
.select(:host_id)
41+
42+
{ :conditions => "#{Host.table_name}.id #{in_operator} (#{subquery.to_sql})" }
43+
else
44+
subquery = TemplateInvocation.where.not(host_id: nil)
45+
.where("job_invocation_id #{operator} ?", value_to_sql(operator, value))
46+
.select(:host_id)
3847

39-
mapping = {
40-
'job_invocation.id' => %(#{TemplateInvocation.table_name}.job_invocation_id #{operator} ?),
41-
'job_invocation.result' => %(#{ForemanTasks::Task.table_name}.result #{operator} (?)),
42-
}
43-
{
44-
:conditions => sanitize_sql_for_conditions([mapping[key], value_to_sql(operator, value)]),
45-
:joins => { :template_invocations => [:run_host_job_task] },
46-
}
48+
{ :conditions => "#{Host.table_name}.id IN (#{subquery.to_sql})" }
49+
end
4750
end
4851
end
4952
end

0 commit comments

Comments
 (0)