Commit d317042f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move tags matching queries to TaggableQueries concern

parent 9004b29d
......@@ -11,7 +11,6 @@ module Ci
include Importable
include Ci::HasRef
include IgnorableColumns
include TaggableQueries
BuildArchivedError = Class.new(StandardError)
......
......@@ -7,6 +7,7 @@ class CommitStatus < ApplicationRecord
include Presentable
include EnumWithNil
include BulkInsertableAssociations
include TaggableQueries
self.table_name = 'ci_builds'
......@@ -85,29 +86,6 @@ class CommitStatus < ApplicationRecord
merge(or_conditions)
end
##
# The temporary `on:` argument has been introduced to make it possible to
# reuse these scopes as a subquery for a different table (ci_pending_builds).
#
scope :matches_tag_ids, ->(tag_ids, on: 'ci_builds.id') do
matcher = ::ActsAsTaggableOn::Tagging
.where(taggable_type: CommitStatus.name)
.where(context: 'tags')
.where("taggable_id = #{on}")
.where.not(tag_id: tag_ids).select('1')
where("NOT EXISTS (?)", matcher)
end
scope :with_any_tags, ->(on: 'ci_builds.id') do
matcher = ::ActsAsTaggableOn::Tagging
.where(taggable_type: CommitStatus.name)
.where(context: 'tags')
.where("taggable_id = #{on}").select('1')
where("EXISTS (?)", matcher)
end
# We use `Enums::Ci::CommitStatus.failure_reasons` here so that EE can more easily
# extend this `Hash` with new values.
enum_with_nil failure_reason: Enums::Ci::CommitStatus.failure_reasons
......
......@@ -12,5 +12,26 @@ module TaggableQueries
.where(taggings: { context: context, taggable_type: polymorphic_name })
.select('COALESCE(array_agg(tags.name ORDER BY name), ARRAY[]::text[])')
end
def matches_tag_ids(tag_ids, table: quoted_table_name, column: 'id')
matcher = ::ActsAsTaggableOn::Tagging
.where(taggable_type: CommitStatus.name)
.where(context: 'tags')
.where("taggable_id = #{table}.#{column}")
.where.not(tag_id: tag_ids)
.select('1')
where("NOT EXISTS (?)", matcher)
end
def with_any_tags(table: quoted_table_name, column: 'id')
matcher = ::ActsAsTaggableOn::Tagging
.where(taggable_type: CommitStatus.name)
.where(context: 'tags')
.where("taggable_id = #{table}.#{column}")
.select('1')
where("EXISTS (?)", matcher)
end
end
end
......@@ -148,11 +148,11 @@ module Ci
end
def builds_matching_tag_ids(relation, ids)
relation.merge(CommitStatus.matches_tag_ids(ids, on: 'ci_pending_builds.build_id'))
relation.merge(CommitStatus.matches_tag_ids(ids, table: 'ci_pending_builds', column: 'build_id'))
end
def builds_with_any_tags(relation)
relation.merge(CommitStatus.with_any_tags(on: 'ci_pending_builds.build_id'))
relation.merge(CommitStatus.with_any_tags(table: 'ci_pending_builds', column: 'build_id'))
end
def builds_queued_before(relation, time)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment