Commit 58f229af authored by Kamil Trzciński's avatar Kamil Trzciński

Make Atomic Internal ID work for pipelines

parent 35cf604b
......@@ -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
......
......@@ -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
......@@ -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!
......
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