diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /app/models/application_record.rb | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'app/models/application_record.rb')
-rw-r--r-- | app/models/application_record.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 527b67712ee..d9375b55e89 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -63,11 +63,27 @@ class ApplicationRecord < ActiveRecord::Base end def self.safe_find_or_create_by(*args, &block) + return optimized_safe_find_or_create_by(*args, &block) if Feature.enabled?(:optimize_safe_find_or_create_by, default_enabled: :yaml) + safe_ensure_unique(retries: 1) do find_or_create_by(*args, &block) end end + def self.optimized_safe_find_or_create_by(*args, &block) + 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. + transaction(requires_new: true) { all.create(*args, &block) } + rescue ActiveRecord::RecordNotUnique + find_by(*args) + end + def create_or_load_association(association_name) association(association_name).create unless association(association_name).loaded? rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation @@ -87,6 +103,23 @@ class ApplicationRecord < ActiveRecord::Base enum(enum_mod.key => values) end + def self.transaction(**options, &block) + if options[:requires_new] && track_subtransactions? + ::Gitlab::Database::Metrics.subtransactions_increment(self.name) + end + + super(**options, &block) + end + + def self.track_subtransactions? + ::Feature.enabled?(:active_record_subtransactions_counter, type: :ops, default_enabled: :yaml) && + connection.transaction_open? + end + + def self.cached_column_list + self.column_names.map { |column_name| self.arel_table[column_name] } + end + def readable_by?(user) Ability.allowed?(user, "read_#{to_ability_name}".to_sym, self) end |