Make Atomic Internal ID work for pipelines

This commit is contained in:
Kamil Trzciński 2018-05-03 10:48:23 +02:00
parent 35cf604b72
commit 58f229af99
3 changed files with 17 additions and 8 deletions

View File

@ -14,7 +14,9 @@ module Ci
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
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 :statuses, class_name: 'CommitStatus', foreign_key: :commit_id, inverse_of: :pipeline

View File

@ -25,9 +25,13 @@ module AtomicInternalId
extend ActiveSupport::Concern
module ClassMethods
def has_internal_id(column, scope:, init:) # rubocop:disable Naming/PredicateName
before_validation(on: :create) do
def has_internal_id(column, scope:, init:, presence: true, to_param: true) # rubocop:disable Naming/PredicateName
before_validation :"ensure_#{column}!", on: :create
validates column, presence: presence, numericality: true
define_method("ensure_#{column}!") do
scope_value = association(scope).reader
if read_attribute(column).blank? && scope_value
scope_attrs = { scope_value.class.table_name.singularize.to_sym => scope_value }
usage = self.class.table_name.to_sym
@ -35,13 +39,13 @@ module AtomicInternalId
new_iid = InternalId.generate_next(self, scope_attrs, usage, init)
write_attribute(column, new_iid)
end
read_attribute(column)
end
validates column, presence: true, numericality: true
define_method("to_param") do
read_attribute(column)
end if to_param
end
end
def to_param
iid.to_s
end
end

View File

@ -6,6 +6,9 @@ module Gitlab
include Chain::Helpers
def perform!
# TODO: allocate next IID outside of transaction
pipeline.ensure_iid!
::Ci::Pipeline.transaction do
pipeline.save!