2018-11-11 03:43:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
2021-11-15 10:10:57 -05:00
|
|
|
include DatabaseReflection
|
|
|
|
include Transactions
|
|
|
|
include LegacyBulkInsert
|
2022-01-20 16:14:18 -05:00
|
|
|
include CrossDatabaseModification
|
2021-11-15 10:10:57 -05:00
|
|
|
|
2018-11-11 03:43:43 -05:00
|
|
|
self.abstract_class = true
|
2018-11-22 04:35:28 -05:00
|
|
|
|
2021-11-19 13:12:50 -05:00
|
|
|
# We should avoid using pluck https://docs.gitlab.com/ee/development/sql.html#plucking-ids
|
|
|
|
# but, if we are going to use it, let's try and limit the number of records
|
|
|
|
MAX_PLUCK = 1_000
|
|
|
|
|
2019-04-08 09:33:36 -04:00
|
|
|
alias_method :reset, :reload
|
|
|
|
|
2020-05-28 11:08:02 -04:00
|
|
|
def self.without_order
|
|
|
|
reorder(nil)
|
|
|
|
end
|
|
|
|
|
2018-11-22 04:35:28 -05:00
|
|
|
def self.id_in(ids)
|
|
|
|
where(id: ids)
|
|
|
|
end
|
2019-02-04 08:39:54 -05:00
|
|
|
|
2020-09-11 17:08:44 -04:00
|
|
|
def self.primary_key_in(values)
|
|
|
|
where(primary_key => values)
|
|
|
|
end
|
|
|
|
|
2020-07-10 02:09:23 -04:00
|
|
|
def self.iid_in(iids)
|
|
|
|
where(iid: iids)
|
|
|
|
end
|
|
|
|
|
2019-03-27 12:18:28 -04:00
|
|
|
def self.id_not_in(ids)
|
|
|
|
where.not(id: ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pluck_primary_key
|
|
|
|
where(nil).pluck(self.primary_key)
|
|
|
|
end
|
|
|
|
|
2019-04-12 00:19:45 -04:00
|
|
|
def self.safe_ensure_unique(retries: 0)
|
2021-08-23 14:11:07 -04:00
|
|
|
transaction(requires_new: true) do # rubocop:disable Performance/ActiveRecordSubtransactions
|
2019-04-12 00:19:45 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
if retries > 0
|
|
|
|
retries -= 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
def self.safe_find_or_create_by!(*args, &block)
|
|
|
|
safe_find_or_create_by(*args, &block).tap do |record|
|
2020-11-04 10:08:41 -05:00
|
|
|
raise ActiveRecord::RecordNotFound unless record.present?
|
|
|
|
|
2019-02-05 12:22:25 -05:00
|
|
|
record.validate! unless record.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-14 14:08:47 -04:00
|
|
|
# Start a new transaction with a shorter-than-usual statement timeout. This is
|
|
|
|
# currently one third of the default 15-second timeout
|
2021-03-23 05:09:17 -04:00
|
|
|
def self.with_fast_read_statement_timeout(timeout_ms = 5000)
|
2021-05-27 02:10:47 -04:00
|
|
|
::Gitlab::Database::LoadBalancing::Session.current.fallback_to_replicas_for_ambiguous_queries do
|
2021-08-23 14:11:07 -04:00
|
|
|
transaction(requires_new: true) do # rubocop:disable Performance/ActiveRecordSubtransactions
|
2021-05-27 02:10:47 -04:00
|
|
|
connection.exec_query("SET LOCAL statement_timeout = #{timeout_ms}")
|
2020-10-14 14:08:47 -04:00
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
yield
|
|
|
|
end
|
2020-10-14 14:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
def self.safe_find_or_create_by(*args, &block)
|
2021-08-18 17:08:44 -04:00
|
|
|
record = find_by(*args)
|
|
|
|
return record if record.present?
|
|
|
|
|
|
|
|
# We need to use `all.create` to make this implementation follow `find_or_create_by` which delegates this in
|
|
|
|
# https://github.com/rails/rails/blob/v6.1.3.2/activerecord/lib/active_record/querying.rb#L22
|
|
|
|
#
|
|
|
|
# When calling this method on an association, just calling `self.create` would call `ActiveRecord::Persistence.create`
|
|
|
|
# and that skips some code that adds the newly created record to the association.
|
2021-08-23 14:11:07 -04:00
|
|
|
transaction(requires_new: true) { all.create(*args, &block) } # rubocop:disable Performance/ActiveRecordSubtransactions
|
2021-08-18 17:08:44 -04:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
find_by(*args)
|
|
|
|
end
|
|
|
|
|
2021-04-27 05:09:46 -04:00
|
|
|
def create_or_load_association(association_name)
|
|
|
|
association(association_name).create unless association(association_name).loaded?
|
|
|
|
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
|
|
|
|
association(association_name).reader
|
|
|
|
end
|
|
|
|
|
2019-05-11 08:06:44 -04:00
|
|
|
def self.underscore
|
|
|
|
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
|
|
|
|
end
|
2020-10-14 08:08:58 -04:00
|
|
|
|
|
|
|
def self.where_exists(query)
|
|
|
|
where('EXISTS (?)', query.select(1))
|
|
|
|
end
|
2021-01-14 01:11:16 -05:00
|
|
|
|
|
|
|
def self.declarative_enum(enum_mod)
|
2021-11-17 01:12:07 -05:00
|
|
|
enum(enum_mod.key => enum_mod.values)
|
2021-01-14 01:11:16 -05:00
|
|
|
end
|
2021-07-21 11:08:52 -04:00
|
|
|
|
2021-07-26 20:08:48 -04:00
|
|
|
def self.cached_column_list
|
|
|
|
self.column_names.map { |column_name| self.arel_table[column_name] }
|
|
|
|
end
|
2021-08-03 17:09:39 -04:00
|
|
|
|
2021-09-01 05:10:58 -04:00
|
|
|
def self.default_select_columns
|
|
|
|
if ignored_columns.any?
|
|
|
|
cached_column_list
|
|
|
|
else
|
|
|
|
arel_table[Arel.star]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-03 17:09:39 -04:00
|
|
|
def readable_by?(user)
|
|
|
|
Ability.allowed?(user, "read_#{to_ability_name}".to_sym, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_ability_name
|
|
|
|
model_name.element
|
|
|
|
end
|
2018-11-11 03:43:43 -05:00
|
|
|
end
|