From 2a2b4b02ad6d68a775fe98dba25a4bf0fc1c1d33 Mon Sep 17 00:00:00 2001
From: Marius Bobin <mbobin@gitlab.com>
Date: Tue, 4 Oct 2022 16:06:59 +0300
Subject: [PATCH] Use table_name in SQL queries for Ci::BuildMetadata

It replaces the usage of `ci_builds_metadata` with Ci::BuildMetadata.table_name
or Ci::BuildMetadata.quoted_table_name because we're planning to partition that
table and we'll rename it to `p_ci_builds_metadata`.
---
 app/models/ci/build.rb                           | 5 +++--
 app/models/ci/build_metadata.rb                  | 2 +-
 app/models/ci/pipeline.rb                        | 4 ++--
 spec/services/ci/create_pipeline_service_spec.rb | 2 +-
 4 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index f67d1983356a..282622c792e3 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -108,7 +108,8 @@ def persisted_environment=(environment)
     validates :ref, presence: true
 
     scope :not_interruptible, -> do
-      joins(:metadata).where.not('ci_builds_metadata.id' => Ci::BuildMetadata.scoped_build.with_interruptible.select(:id))
+      joins(:metadata)
+        .where.not(Ci::BuildMetadata.table_name => { id: Ci::BuildMetadata.scoped_build.with_interruptible.select(:id) })
     end
 
     scope :unstarted, -> { where(runner_id: nil) }
@@ -187,7 +188,7 @@ def persisted_environment=(environment)
     scope :license_management_jobs, -> { where(name: %i(license_management license_scanning)) } # handle license rename https://gitlab.com/gitlab-org/gitlab/issues/8911
 
     scope :with_secure_reports_from_config_options, -> (job_types) do
-      joins(:metadata).where("ci_builds_metadata.config_options -> 'artifacts' -> 'reports' ?| array[:job_types]", job_types: job_types)
+      joins(:metadata).where("#{Ci::BuildMetadata.quoted_table_name}.config_options -> 'artifacts' -> 'reports' ?| array[:job_types]", job_types: job_types)
     end
 
     scope :with_coverage, -> { where.not(coverage: nil) }
diff --git a/app/models/ci/build_metadata.rb b/app/models/ci/build_metadata.rb
index 6d412d490837..33092e881f04 100644
--- a/app/models/ci/build_metadata.rb
+++ b/app/models/ci/build_metadata.rb
@@ -30,7 +30,7 @@ class BuildMetadata < Ci::ApplicationRecord
 
     chronic_duration_attr_reader :timeout_human_readable, :timeout
 
-    scope :scoped_build, -> { where('ci_builds_metadata.build_id = ci_builds.id') }
+    scope :scoped_build, -> { where("#{quoted_table_name}.build_id = #{Ci::Build.quoted_table_name}.id") }
     scope :with_interruptible, -> { where(interruptible: true) }
     scope :with_exposed_artifacts, -> { where(has_exposed_artifacts: true) }
 
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 2631ed3f6725..853437b2d27a 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -995,8 +995,8 @@ def environments_in_self_and_project_descendants(deployment_status: nil)
       # See: https://gitlab.com/gitlab-org/gitlab/-/issues/340781#note_699114700
       expanded_environment_names =
         builds_in_self_and_project_descendants.joins(:metadata)
-                                      .where.not('ci_builds_metadata.expanded_environment_name' => nil)
-                                      .distinct('ci_builds_metadata.expanded_environment_name')
+                                      .where.not(Ci::BuildMetadata.table_name => { expanded_environment_name: nil })
+                                      .distinct("#{Ci::BuildMetadata.quoted_table_name}.expanded_environment_name")
                                       .limit(100)
                                       .pluck(:expanded_environment_name)
 
diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb
index f6030c513364..458692ba1c09 100644
--- a/spec/services/ci/create_pipeline_service_spec.rb
+++ b/spec/services/ci/create_pipeline_service_spec.rb
@@ -293,7 +293,7 @@ def execute_service(
               pipeline_on_previous_commit
                 .builds
                 .joins(:metadata)
-                .pluck(:name, 'ci_builds_metadata.interruptible')
+                .pluck(:name, "#{Ci::BuildMetadata.quoted_table_name}.interruptible")
 
             expect(interruptible_status).to contain_exactly(
               ['build_1_1', true],
-- 
GitLab