Module: AtomicInternalId

Extended by:
ActiveSupport::Concern
Included in:
AlertManagement::Alert, Ci::Pipeline, Deployment, Issue, Iteration, MergeRequest, Operations::FeatureFlag, Operations::FeatureFlags::UserList, Timebox
Defined in:
app/models/concerns/atomic_internal_id.rb

Overview

Include atomic internal id generation scheme for a model

This allows us to atomically generate internal ids that are unique within a given scope.

For example, let's generate internal ids for Issue per Project: “` class Issue < ApplicationRecord

has_internal_id :iid, scope: :project, init: ->(s) { s.project.issues.maximum(:iid) }

end “`

This generates unique internal ids per project for newly created issues. The generated internal id is saved in the `iid` attribute of `Issue`.

This concern uses InternalId records to facilitate atomicity. In the absence of a record for the given scope, one will be created automatically. In this situation, the `init` block is called to calculate the initial value. In the example above, we calculate the maximum `iid` of all issues within the given project.

Note that a model may have more than one internal id associated with possibly different scopes.

Instance Method Summary collapse

Instance Method Details

#internal_id_read_scope(scope) ⇒ Object


118
119
120
# File 'app/models/concerns/atomic_internal_id.rb', line 118

def internal_id_read_scope(scope)
  association(scope).reader
end

#internal_id_scope_attrs(scope) ⇒ Object


108
109
110
111
112
# File 'app/models/concerns/atomic_internal_id.rb', line 108

def internal_id_scope_attrs(scope)
  scope_value = internal_id_read_scope(scope)

  { scope_value.class.table_name.singularize.to_sym => scope_value } if scope_value
end

#internal_id_scope_usageObject


114
115
116
# File 'app/models/concerns/atomic_internal_id.rb', line 114

def internal_id_scope_usage
  self.class.table_name.to_sym
end