2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
# Include atomic internal id generation scheme for a model
|
|
|
|
#
|
2018-03-16 08:34:08 -04:00
|
|
|
# This allows us to atomically generate internal ids that are
|
2018-03-12 11:51:38 -04:00
|
|
|
# unique within a given scope.
|
|
|
|
#
|
|
|
|
# For example, let's generate internal ids for Issue per Project:
|
|
|
|
# ```
|
2019-03-28 09:17:42 -04:00
|
|
|
# class Issue < ApplicationRecord
|
2018-03-12 11:51:38 -04:00
|
|
|
# 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.
|
2018-03-06 14:09:01 -05:00
|
|
|
module AtomicInternalId
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
MissingValueError = Class.new(StandardError)
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2020-11-10 19:08:58 -05:00
|
|
|
def has_internal_id( # rubocop:disable Naming/PredicateName
|
2021-01-29 10:09:40 -05:00
|
|
|
column, scope:, init: :not_given, ensure_if: nil, track_if: nil, presence: true, hook_names: :create)
|
2020-11-10 19:08:58 -05:00
|
|
|
raise "has_internal_id init must not be nil if given." if init.nil?
|
2019-10-04 08:06:14 -04:00
|
|
|
raise "has_internal_id needs to be defined on association." unless self.reflect_on_association(scope)
|
2018-07-27 08:39:48 -04:00
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
init = infer_init(scope) if init == :not_given
|
2021-01-29 10:09:40 -05:00
|
|
|
callback_names = Array.wrap(hook_names).map { |hook_name| :"before_#{hook_name}" }
|
|
|
|
callback_names.each do |callback_name|
|
|
|
|
# rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
public_send(callback_name, :"track_#{scope}_#{column}!", if: track_if)
|
|
|
|
public_send(callback_name, :"ensure_#{scope}_#{column}!", if: ensure_if)
|
|
|
|
# rubocop:enable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
after_rollback :"clear_#{scope}_#{column}!", on: hook_names, if: ensure_if
|
|
|
|
|
|
|
|
if presence
|
|
|
|
before_create :"validate_#{column}_exists!"
|
|
|
|
before_update :"validate_#{column}_exists!"
|
|
|
|
end
|
2018-05-03 04:48:23 -04:00
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
define_singleton_internal_id_methods(scope, column, init)
|
2021-01-29 10:09:40 -05:00
|
|
|
define_instance_internal_id_methods(scope, column, init)
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def infer_init(scope)
|
|
|
|
case scope
|
|
|
|
when :project
|
|
|
|
AtomicInternalId.project_init(self)
|
|
|
|
when :group
|
|
|
|
AtomicInternalId.group_init(self)
|
|
|
|
else
|
|
|
|
# We require init here to retain the ability to recalculate in the absence of a
|
|
|
|
# InternalId record (we may delete records in `internal_ids` for example).
|
|
|
|
raise "has_internal_id - cannot infer init for scope: #{scope}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Defines instance methods:
|
|
|
|
# - ensure_{scope}_{column}!
|
|
|
|
# - track_{scope}_{column}!
|
|
|
|
# - reset_{scope}_{column}
|
|
|
|
# - {column}=
|
2021-01-29 10:09:40 -05:00
|
|
|
def define_instance_internal_id_methods(scope, column, init)
|
2018-05-10 03:22:43 -04:00
|
|
|
define_method("ensure_#{scope}_#{column}!") do
|
2019-10-04 08:06:14 -04:00
|
|
|
scope_value = internal_id_read_scope(scope)
|
2018-08-01 05:03:14 -04:00
|
|
|
value = read_attribute(column)
|
|
|
|
return value unless scope_value
|
2018-03-14 09:36:07 -04:00
|
|
|
|
2019-10-04 08:06:14 -04:00
|
|
|
if value.nil?
|
2019-09-30 14:06:34 -04:00
|
|
|
# We don't have a value yet and use a InternalId record to generate
|
|
|
|
# the next value.
|
2019-10-04 08:06:14 -04:00
|
|
|
value = InternalId.generate_next(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
init)
|
2018-08-01 05:03:14 -04:00
|
|
|
write_attribute(column, value)
|
2021-01-29 10:09:40 -05:00
|
|
|
|
|
|
|
@internal_id_set_manually = false
|
2018-03-14 08:42:03 -04:00
|
|
|
end
|
2018-05-03 04:48:23 -04:00
|
|
|
|
2018-08-01 05:03:14 -04:00
|
|
|
value
|
2018-03-12 10:38:56 -04:00
|
|
|
end
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2019-10-04 08:06:14 -04:00
|
|
|
define_method("track_#{scope}_#{column}!") do
|
2019-10-15 17:06:25 -04:00
|
|
|
return unless @internal_id_needs_tracking
|
2019-10-04 08:06:14 -04:00
|
|
|
|
|
|
|
scope_value = internal_id_read_scope(scope)
|
|
|
|
return unless scope_value
|
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
value = read_attribute(column)
|
|
|
|
|
2019-10-04 08:06:14 -04:00
|
|
|
if value.present?
|
|
|
|
# The value was set externally, e.g. by the user
|
|
|
|
# We update the InternalId record to keep track of the greatest value.
|
|
|
|
InternalId.track_greatest(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
value,
|
|
|
|
init)
|
2019-10-11 14:06:15 -04:00
|
|
|
|
|
|
|
@internal_id_needs_tracking = false
|
2019-10-04 08:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 14:06:34 -04:00
|
|
|
define_method("#{column}=") do |value|
|
|
|
|
super(value).tap do |v|
|
|
|
|
# Indicate the iid was set from externally
|
2019-10-04 08:06:14 -04:00
|
|
|
@internal_id_needs_tracking = true
|
2021-01-29 10:09:40 -05:00
|
|
|
@internal_id_set_manually = true
|
2019-09-30 14:06:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-22 11:51:15 -04:00
|
|
|
define_method("reset_#{scope}_#{column}") do
|
|
|
|
if value = read_attribute(column)
|
2019-10-04 08:06:14 -04:00
|
|
|
did_reset = InternalId.reset(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
value)
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2019-10-04 08:06:14 -04:00
|
|
|
if did_reset
|
2019-03-22 11:51:15 -04:00
|
|
|
write_attribute(column, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
read_attribute(column)
|
|
|
|
end
|
2021-01-29 10:09:40 -05:00
|
|
|
|
|
|
|
define_method("clear_#{scope}_#{column}!") do
|
|
|
|
return if @internal_id_set_manually
|
|
|
|
|
|
|
|
return unless public_send(:"#{column}_previously_changed?") # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
|
|
|
|
write_attribute(column, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method("validate_#{column}_exists!") do
|
|
|
|
value = read_attribute(column)
|
|
|
|
|
|
|
|
raise MissingValueError, "#{column} was unexpectedly blank!" if value.blank?
|
|
|
|
end
|
2018-03-12 10:38:56 -04:00
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
|
|
|
|
# Defines class methods:
|
|
|
|
#
|
|
|
|
# - with_{scope}_{column}_supply
|
|
|
|
# This method can be used to allocate a block of IID values during
|
|
|
|
# bulk operations (importing/copying, etc). This can be more efficient
|
|
|
|
# than creating instances one-by-one.
|
|
|
|
#
|
|
|
|
# Pass in a block that receives a `Supply` instance. To allocate a new
|
|
|
|
# IID value, call `Supply#next_value`.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# MyClass.with_project_iid_supply(project) do |supply|
|
|
|
|
# attributes = MyClass.where(project: project).find_each do |record|
|
|
|
|
# record.attributes.merge(iid: supply.next_value)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# bulk_insert(attributes)
|
|
|
|
# end
|
|
|
|
def define_singleton_internal_id_methods(scope, column, init)
|
|
|
|
define_singleton_method("with_#{scope}_#{column}_supply") do |scope_value, &block|
|
|
|
|
subject = find_by(scope => scope_value) || self
|
|
|
|
scope_attrs = ::AtomicInternalId.scope_attrs(scope_value)
|
|
|
|
usage = ::AtomicInternalId.scope_usage(self)
|
|
|
|
|
|
|
|
generator = InternalId::InternalIdGenerator.new(subject, scope_attrs, usage, init)
|
|
|
|
|
|
|
|
generator.with_lock do
|
|
|
|
supply = Supply.new(generator.record.last_value)
|
|
|
|
block.call(supply)
|
|
|
|
ensure
|
|
|
|
generator.track_greatest(supply.current_value) if supply
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.scope_attrs(scope_value)
|
|
|
|
{ scope_value.class.table_name.singularize.to_sym => scope_value } if scope_value
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
2019-10-04 08:06:14 -04:00
|
|
|
|
|
|
|
def internal_id_scope_attrs(scope)
|
|
|
|
scope_value = internal_id_read_scope(scope)
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
::AtomicInternalId.scope_attrs(scope_value)
|
2019-10-04 08:06:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal_id_scope_usage
|
2020-11-10 19:08:58 -05:00
|
|
|
::AtomicInternalId.scope_usage(self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.scope_usage(including_class)
|
|
|
|
including_class.table_name.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.project_init(klass, column_name = :iid)
|
|
|
|
->(instance, scope) do
|
|
|
|
if instance
|
|
|
|
klass.where(project_id: instance.project_id).maximum(column_name)
|
|
|
|
elsif scope.present?
|
|
|
|
klass.where(**scope).maximum(column_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.group_init(klass, column_name = :iid)
|
|
|
|
->(instance, scope) do
|
|
|
|
if instance
|
|
|
|
klass.where(group_id: instance.group_id).maximum(column_name)
|
|
|
|
elsif scope.present?
|
|
|
|
klass.where(group: scope[:namespace]).maximum(column_name)
|
|
|
|
end
|
|
|
|
end
|
2019-10-04 08:06:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal_id_read_scope(scope)
|
|
|
|
association(scope).reader
|
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
|
|
|
|
class Supply
|
|
|
|
attr_reader :current_value
|
|
|
|
|
|
|
|
def initialize(start_value)
|
|
|
|
@current_value = start_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_value
|
|
|
|
@current_value += 1
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|