diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-04 15:06:55 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-04 15:06:55 +0300 |
commit | 30a8e054751fe9020a9ed526434db83af35b4718 (patch) | |
tree | 705dc26a080a7110a653f72a3200f8c718858892 /app/models | |
parent | f797f00ec769812339968010af178b3d51a4c460 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ci/bridge.rb | 1 | ||||
-rw-r--r-- | app/models/ci/build.rb | 12 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 4 | ||||
-rw-r--r-- | app/models/ci/processable.rb | 6 | ||||
-rw-r--r-- | app/models/concerns/ci/metadatable.rb | 6 | ||||
-rw-r--r-- | app/models/concerns/routable.rb | 22 | ||||
-rw-r--r-- | app/models/deployment.rb | 42 | ||||
-rw-r--r-- | app/models/email.rb | 2 | ||||
-rw-r--r-- | app/models/environment.rb | 14 | ||||
-rw-r--r-- | app/models/group.rb | 1 | ||||
-rw-r--r-- | app/models/identity.rb | 2 | ||||
-rw-r--r-- | app/models/identity/uniqueness_scopes.rb | 2 | ||||
-rw-r--r-- | app/models/namespace.rb | 1 | ||||
-rw-r--r-- | app/models/plan.rb | 2 | ||||
-rw-r--r-- | app/models/redirect_route.rb | 2 | ||||
-rw-r--r-- | app/models/route.rb | 2 | ||||
-rw-r--r-- | app/models/user_detail.rb | 2 | ||||
-rw-r--r-- | app/models/user_preference.rb | 2 | ||||
-rw-r--r-- | app/models/user_status.rb | 2 | ||||
-rw-r--r-- | app/models/user_synced_attributes_metadata.rb | 2 | ||||
-rw-r--r-- | app/models/users/callout.rb | 2 |
21 files changed, 70 insertions, 61 deletions
diff --git a/app/models/ci/bridge.rb b/app/models/ci/bridge.rb index 895ff937abe..74072c362bd 100644 --- a/app/models/ci/bridge.rb +++ b/app/models/ci/bridge.rb @@ -3,7 +3,6 @@ module Ci class Bridge < Ci::Processable include Ci::Contextable - include Ci::Metadatable include Ci::Deployable include Importable include AfterCommitQueue diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 1ce852d4f71..d85c7a5ccef 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -3,7 +3,6 @@ module Ci class Build < Ci::Processable prepend Ci::BulkInsertableTags - include Ci::Metadatable include Ci::Contextable include Ci::Deployable include TokenAuthenticatable @@ -158,16 +157,9 @@ module Ci .includes(:metadata, :job_artifacts_metadata) end - scope :with_project_and_metadata, -> do - if Feature.enabled?(:non_public_artifacts, type: :development) - joins(:metadata).includes(:metadata).preload(:project) - end - end - scope :with_artifacts_not_expired, -> { with_downloadable_artifacts.where('artifacts_expire_at IS NULL OR artifacts_expire_at > ?', Time.current) } scope :with_pipeline_locked_artifacts, -> { joins(:pipeline).where('pipeline.locked': Ci::Pipeline.lockeds[:artifacts_locked]) } scope :last_month, -> { where('created_at > ?', Date.today - 1.month) } - scope :manual_actions, -> { where(when: :manual, status: COMPLETED_STATUSES + %i[manual]) } scope :scheduled_actions, -> { where(when: :delayed, status: COMPLETED_STATUSES + %i[scheduled]) } scope :ref_protected, -> { where(protected: true) } scope :with_live_trace, -> { where('EXISTS (?)', Ci::BuildTraceChunk.where("#{quoted_table_name}.id = #{Ci::BuildTraceChunk.quoted_table_name}.build_id").select(1)) } @@ -387,10 +379,6 @@ module Ci .fabricate! end - def other_manual_actions - pipeline.manual_actions.reject { |action| action.name == name } - end - def other_scheduled_actions pipeline.scheduled_actions.reject { |action| action.name == name } end diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index e2f017b1d14..a0e5eefafa6 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -100,7 +100,7 @@ module Ci has_many :downloadable_artifacts, -> do not_expired.or(where_exists(Ci::Pipeline.artifacts_locked.where("#{Ci::Pipeline.quoted_table_name}.id = #{Ci::Build.quoted_table_name}.commit_id"))).downloadable.with_job end, through: :latest_builds, source: :job_artifacts - has_many :latest_successful_builds, -> { latest.success.with_project_and_metadata }, foreign_key: :commit_id, inverse_of: :pipeline, class_name: 'Ci::Build' + has_many :latest_successful_jobs, -> { latest.success.with_project_and_metadata }, foreign_key: :commit_id, inverse_of: :pipeline, class_name: 'Ci::Processable' has_many :messages, class_name: 'Ci::PipelineMessage', inverse_of: :pipeline @@ -115,7 +115,7 @@ module Ci has_many :retryable_builds, -> { latest.failed_or_canceled.includes(:project) }, foreign_key: :commit_id, class_name: 'Ci::Build', inverse_of: :pipeline has_many :cancelable_statuses, -> { cancelable }, foreign_key: :commit_id, class_name: 'CommitStatus', inverse_of: :pipeline - has_many :manual_actions, -> { latest.manual_actions.includes(:project) }, foreign_key: :commit_id, class_name: 'Ci::Build', inverse_of: :pipeline + has_many :manual_actions, -> { latest.manual_actions.includes(:project) }, foreign_key: :commit_id, class_name: 'Ci::Processable', inverse_of: :pipeline has_many :scheduled_actions, -> { latest.scheduled_actions.includes(:project) }, foreign_key: :commit_id, class_name: 'Ci::Build', inverse_of: :pipeline has_many :auto_canceled_pipelines, class_name: 'Ci::Pipeline', foreign_key: :auto_canceled_by_id, diff --git a/app/models/ci/processable.rb b/app/models/ci/processable.rb index 4c421f066f9..7ad1a727a0e 100644 --- a/app/models/ci/processable.rb +++ b/app/models/ci/processable.rb @@ -6,6 +6,7 @@ module Ci class Processable < ::CommitStatus include Gitlab::Utils::StrongMemoize include FromUnion + include Ci::Metadatable extend ::Gitlab::Utils::Override has_one :resource, class_name: 'Ci::Resource', foreign_key: 'build_id', inverse_of: :processable @@ -16,6 +17,7 @@ module Ci accepts_nested_attributes_for :needs scope :preload_needs, -> { preload(:needs) } + scope :manual_actions, -> { where(when: :manual, status: COMPLETED_STATUSES + %i[manual]) } scope :with_needs, -> (names = nil) do needs = Ci::BuildNeed.scoped_build.select(1) @@ -138,6 +140,10 @@ module Ci raise NotImplementedError end + def other_manual_actions + pipeline.manual_actions.reject { |action| action.name == name } + end + def when read_attribute(:when) || 'on_success' end diff --git a/app/models/concerns/ci/metadatable.rb b/app/models/concerns/ci/metadatable.rb index 1c6b82d6ea7..b785e39523d 100644 --- a/app/models/concerns/ci/metadatable.rb +++ b/app/models/concerns/ci/metadatable.rb @@ -24,6 +24,12 @@ module Ci delegate :id_tokens, to: :metadata, allow_nil: true before_validation :ensure_metadata, on: :create + + scope :with_project_and_metadata, -> do + if Feature.enabled?(:non_public_artifacts, type: :development) + joins(:metadata).includes(:metadata).preload(:project) + end + end end def has_exposed_artifacts? diff --git a/app/models/concerns/routable.rb b/app/models/concerns/routable.rb index d70aad4e9ae..2f77a7947ca 100644 --- a/app/models/concerns/routable.rb +++ b/app/models/concerns/routable.rb @@ -25,17 +25,19 @@ module Routable # # We need to qualify the columns with the table name, to support both direct lookups on # Route/RedirectRoute, and scoped lookups through the Routable classes. - route = - route_scope.find_by(routes: { path: path }) || - route_scope.iwhere(Route.arel_table[:path] => path).take + Gitlab::Database.allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/420046") do + route = + route_scope.find_by(routes: { path: path }) || + route_scope.iwhere(Route.arel_table[:path] => path).take - if follow_redirects - route ||= redirect_route_scope.iwhere(RedirectRoute.arel_table[:path] => path).take - end + if follow_redirects + route ||= redirect_route_scope.iwhere(RedirectRoute.arel_table[:path] => path).take + end - return unless route + next unless route - route.is_a?(Routable) ? route : route.source + route.is_a?(Routable) ? route : route.source + end end included do @@ -94,7 +96,9 @@ module Routable joins(:route) end - route.where(wheres.join(' OR ')) + route + .where(wheres.join(' OR ')) + .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/420046") end end diff --git a/app/models/deployment.rb b/app/models/deployment.rb index 2cef727edf0..38bf84da073 100644 --- a/app/models/deployment.rb +++ b/app/models/deployment.rb @@ -185,23 +185,23 @@ class Deployment < ApplicationRecord # - deploy job B => production environment # In this case, `last_deployment_group` returns both deployments. # - # NOTE: Preload environment.last_deployment and pipeline.latest_successful_builds prior to avoid N+1. + # NOTE: Preload environment.last_deployment and pipeline.latest_successful_jobs prior to avoid N+1. def self.last_deployment_group_for_environment(env) - return self.none unless env.last_deployment_pipeline&.latest_successful_builds&.present? + return self.none unless env.last_deployment_pipeline&.latest_successful_jobs&.present? BatchLoader.for(env).batch(default_value: self.none) do |environments, loader| - latest_successful_build_ids = [] + latest_successful_job_ids = [] environments_hash = {} environments.each do |environment| environments_hash[environment.id] = environment # Refer comment note above, if not preloaded this can lead to N+1. - latest_successful_build_ids << environment.last_deployment_pipeline.latest_successful_builds.map(&:id) + latest_successful_job_ids << environment.last_deployment_pipeline.latest_successful_jobs.map(&:id) end Deployment - .where(deployable_type: 'CommitStatus', deployable_id: latest_successful_build_ids.flatten) + .where(deployable_type: 'CommitStatus', deployable_id: latest_successful_job_ids.flatten) .preload(last_deployment_group_associations) .group_by { |deployment| deployment.environment_id } .each do |env_id, deployment_group| @@ -218,14 +218,14 @@ class Deployment < ApplicationRecord # Fetching any unbounded or large intermediate dataset could lead to loading too many IDs into memory. # See: https://docs.gitlab.com/ee/development/database/multiple_databases.html#use-disable_joins-for-has_one-or-has_many-through-relations # For safety we default limit to fetch not more than 1000 records. - def self.builds(limit = 1000) + def self.jobs(limit = 1000) deployable_ids = where.not(deployable_id: nil).limit(limit).pluck(:deployable_id) - Ci::Build.where(id: deployable_ids) + Ci::Processable.where(id: deployable_ids) end - def build - deployable if deployable.is_a?(::Ci::Build) + def job + deployable if deployable.is_a?(::Ci::Processable) end class << self @@ -290,8 +290,8 @@ class Deployment < ApplicationRecord @scheduled_actions ||= deployable.try(:other_scheduled_actions) end - def playable_build - strong_memoize(:playable_build) do + def playable_job + strong_memoize(:playable_job) do deployable.try(:playable?) ? deployable : nil end end @@ -356,8 +356,8 @@ class Deployment < ApplicationRecord end def deployed_by - # We use deployable's user if available because Ci::PlayBuildService - # does not update the deployment's user, just the one for the deployable. + # We use deployable's user if available because Ci::PlayBuildService and Ci::PlayBridgeService + # do not update the deployment's user, just the one for the deployable. # TODO: use deployment's user once https://gitlab.com/gitlab-org/gitlab-foss/issues/66442 # is completed. deployable&.user || user @@ -403,20 +403,20 @@ class Deployment < ApplicationRecord false end - def sync_status_with(build) - build_status = build.status + def sync_status_with(job) + job_status = job.status - if ::Feature.enabled?(:track_manual_deployments, build.project) - build_status = 'blocked' if build_status == 'manual' # rubocop:disable Style/SoleNestedConditional + if ::Feature.enabled?(:track_manual_deployments, job.project) + job_status = 'blocked' if job_status == 'manual' # rubocop:disable Style/SoleNestedConditional end - return false unless ::Deployment.statuses.include?(build_status) - return false if build_status == self.status + return false unless ::Deployment.statuses.include?(job_status) + return false if job_status == self.status - update_status!(build_status) + update_status!(job_status) rescue StandardError => e Gitlab::ErrorTracking.track_exception( - StatusSyncError.new(e.message), deployment_id: self.id, build_id: build.id) + StatusSyncError.new(e.message), deployment_id: self.id, job_id: job.id) false end diff --git a/app/models/email.rb b/app/models/email.rb index 3896dfd5d22..5fca57520b8 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Email < ApplicationRecord +class Email < MainClusterwide::ApplicationRecord include Sortable include Gitlab::SQL::Pattern diff --git a/app/models/environment.rb b/app/models/environment.rb index 241b454f5ce..da8513a52fd 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -331,9 +331,9 @@ class Environment < ApplicationRecord end def cancel_deployment_jobs! - active_deployments.builds.each do |build| - Gitlab::OptimisticLocking.retry_lock(build, name: 'environment_cancel_deployment_jobs') do |build| - build.cancel! if build&.cancelable? + active_deployments.jobs.each do |job| + Gitlab::OptimisticLocking.retry_lock(job, name: 'environment_cancel_deployment_jobs') do |job| + job.cancel! if job&.cancelable? end rescue StandardError => e Gitlab::ErrorTracking.track_exception(e, environment_id: id, deployment_id: deployment.id) @@ -355,8 +355,12 @@ class Environment < ApplicationRecord Gitlab::OptimisticLocking.retry_lock( stop_action, name: 'environment_stop_with_actions' - ) do |build| - actions << build.play(current_user) + ) do |job| + actions << job.play(current_user) + rescue StateMachines::InvalidTransition + # Ci::PlayBuildService rescues an error of StateMachines::InvalidTransition and fall back to retry. However, + # Ci::PlayBridgeService doesn't rescue it, so we're ignoring the error if it's not playable. + # We should fix this inconsistency in https://gitlab.com/gitlab-org/gitlab/-/issues/420855. end end diff --git a/app/models/group.rb b/app/models/group.rb index 5fb0980ec71..4b879d90089 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -184,6 +184,7 @@ class Group < Namespace ids_by_full_path = Route .for_routable_type(Namespace.name) .where('LOWER(routes.path) IN (?)', paths.map(&:downcase)) + .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/420046") .select(:namespace_id) Group.from_union([by_id(ids), by_id(ids_by_full_path), where('LOWER(path) IN (?)', paths.map(&:downcase))]) diff --git a/app/models/identity.rb b/app/models/identity.rb index df1185f330d..a4c59694050 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Identity < ApplicationRecord +class Identity < MainClusterwide::ApplicationRecord include Sortable include CaseSensitivity diff --git a/app/models/identity/uniqueness_scopes.rb b/app/models/identity/uniqueness_scopes.rb index b41b4572e82..598b7e34738 100644 --- a/app/models/identity/uniqueness_scopes.rb +++ b/app/models/identity/uniqueness_scopes.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Identity < ApplicationRecord +class Identity < MainClusterwide::ApplicationRecord # This module and method are defined in a separate file to allow EE to # redefine the `scopes` method before it is used in the `Identity` model. module UniquenessScopes diff --git a/app/models/namespace.rb b/app/models/namespace.rb index c1f4c7f1b46..abf4ffaead4 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -234,6 +234,7 @@ class Namespace < ApplicationRecord if include_parents without_project_namespaces .where(id: Route.for_routable_type(Namespace.name) + .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/420046") .fuzzy_search(query, [Route.arel_table[:path], Route.arel_table[:name]], use_minimum_char_limit: use_minimum_char_limit) .select(:source_id)) diff --git a/app/models/plan.rb b/app/models/plan.rb index e16ecb4c629..22c1201421c 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Plan < ApplicationRecord +class Plan < MainClusterwide::ApplicationRecord DEFAULT = 'default' has_one :limits, class_name: 'PlanLimits' diff --git a/app/models/redirect_route.rb b/app/models/redirect_route.rb index 749f4a87818..54b4c9d0fe1 100644 --- a/app/models/redirect_route.rb +++ b/app/models/redirect_route.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class RedirectRoute < ApplicationRecord +class RedirectRoute < MainClusterwide::ApplicationRecord include CaseSensitivity belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations diff --git a/app/models/route.rb b/app/models/route.rb index f2fe1664f9e..652c33a673c 100644 --- a/app/models/route.rb +++ b/app/models/route.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Route < ApplicationRecord +class Route < MainClusterwide::ApplicationRecord include CaseSensitivity include Gitlab::SQL::Pattern diff --git a/app/models/user_detail.rb b/app/models/user_detail.rb index 293a20fcc5a..9ac814eebda 100644 --- a/app/models/user_detail.rb +++ b/app/models/user_detail.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class UserDetail < ApplicationRecord +class UserDetail < MainClusterwide::ApplicationRecord include IgnorableColumns extend ::Gitlab::Utils::Override diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index c263d552d40..eac66905d0c 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class UserPreference < ApplicationRecord +class UserPreference < MainClusterwide::ApplicationRecord include IgnorableColumns # We could use enums, but Rails 4 doesn't support multiple diff --git a/app/models/user_status.rb b/app/models/user_status.rb index da24ef47a2a..35aa2427442 100644 --- a/app/models/user_status.rb +++ b/app/models/user_status.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class UserStatus < ApplicationRecord +class UserStatus < MainClusterwide::ApplicationRecord include CacheMarkdownField self.primary_key = :user_id diff --git a/app/models/user_synced_attributes_metadata.rb b/app/models/user_synced_attributes_metadata.rb index 6b23bce6406..0856febf3f6 100644 --- a/app/models/user_synced_attributes_metadata.rb +++ b/app/models/user_synced_attributes_metadata.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class UserSyncedAttributesMetadata < ApplicationRecord +class UserSyncedAttributesMetadata < MainClusterwide::ApplicationRecord belongs_to :user validates :user, presence: true diff --git a/app/models/users/callout.rb b/app/models/users/callout.rb index 0d02a3b99aa..0d3262b2474 100644 --- a/app/models/users/callout.rb +++ b/app/models/users/callout.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Users - class Callout < ApplicationRecord + class Callout < MainClusterwide::ApplicationRecord include Users::Calloutable self.table_name = 'user_callouts' |