Make Atomic Internal ID work for pipelines
This commit is contained in:
parent
35cf604b72
commit
58f229af99
3 changed files with 17 additions and 8 deletions
|
@ -14,7 +14,9 @@ module Ci
|
||||||
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
||||||
belongs_to :pipeline_schedule, class_name: 'Ci::PipelineSchedule'
|
belongs_to :pipeline_schedule, class_name: 'Ci::PipelineSchedule'
|
||||||
|
|
||||||
has_internal_id :iid, scope: :project, init: ->(s) { s&.project&.pipelines.count }
|
has_internal_id :iid, scope: :project, presence: false, to_param: false, init: -> do |s|
|
||||||
|
s&.project&.pipelines&.maximum(:iid) || s&.project&.pipelines.count
|
||||||
|
end
|
||||||
|
|
||||||
has_many :stages
|
has_many :stages
|
||||||
has_many :statuses, class_name: 'CommitStatus', foreign_key: :commit_id, inverse_of: :pipeline
|
has_many :statuses, class_name: 'CommitStatus', foreign_key: :commit_id, inverse_of: :pipeline
|
||||||
|
|
|
@ -25,9 +25,13 @@ module AtomicInternalId
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def has_internal_id(column, scope:, init:) # rubocop:disable Naming/PredicateName
|
def has_internal_id(column, scope:, init:, presence: true, to_param: true) # rubocop:disable Naming/PredicateName
|
||||||
before_validation(on: :create) do
|
before_validation :"ensure_#{column}!", on: :create
|
||||||
|
validates column, presence: presence, numericality: true
|
||||||
|
|
||||||
|
define_method("ensure_#{column}!") do
|
||||||
scope_value = association(scope).reader
|
scope_value = association(scope).reader
|
||||||
|
|
||||||
if read_attribute(column).blank? && scope_value
|
if read_attribute(column).blank? && scope_value
|
||||||
scope_attrs = { scope_value.class.table_name.singularize.to_sym => scope_value }
|
scope_attrs = { scope_value.class.table_name.singularize.to_sym => scope_value }
|
||||||
usage = self.class.table_name.to_sym
|
usage = self.class.table_name.to_sym
|
||||||
|
@ -35,13 +39,13 @@ module AtomicInternalId
|
||||||
new_iid = InternalId.generate_next(self, scope_attrs, usage, init)
|
new_iid = InternalId.generate_next(self, scope_attrs, usage, init)
|
||||||
write_attribute(column, new_iid)
|
write_attribute(column, new_iid)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
read_attribute(column)
|
||||||
end
|
end
|
||||||
|
|
||||||
validates column, presence: true, numericality: true
|
define_method("to_param") do
|
||||||
|
read_attribute(column)
|
||||||
|
end if to_param
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_param
|
|
||||||
iid.to_s
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,9 @@ module Gitlab
|
||||||
include Chain::Helpers
|
include Chain::Helpers
|
||||||
|
|
||||||
def perform!
|
def perform!
|
||||||
|
# TODO: allocate next IID outside of transaction
|
||||||
|
pipeline.ensure_iid!
|
||||||
|
|
||||||
::Ci::Pipeline.transaction do
|
::Ci::Pipeline.transaction do
|
||||||
pipeline.save!
|
pipeline.save!
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue