Commit 622a28c3 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'remove-projects_ci_project_monthly_usages_project_id-fk' into 'master'

Swap FK ci_project_monthly_usages.project_id to projects for LFK

See merge request gitlab-org/gitlab!79073
parents c17d4d1f 1f1ec869
# frozen_string_literal: true
class RemoveProjectsCiProjectMonthlyUsagesProjectIdFk < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
def up
return unless foreign_key_exists?(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6")
with_lock_retries do
execute('LOCK projects, ci_project_monthly_usages IN ACCESS EXCLUSIVE MODE') if transaction_open?
remove_foreign_key_if_exists(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6")
end
end
def down
add_concurrent_foreign_key(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6", column: :project_id, target_column: :id, on_delete: :cascade)
end
end
4b1dad4fc34717c9b89a770e4e0682b0cee7d421da68223011bb9fde9460d1f8
\ No newline at end of file
...@@ -30487,9 +30487,6 @@ ALTER TABLE ONLY resource_iteration_events ...@@ -30487,9 +30487,6 @@ ALTER TABLE ONLY resource_iteration_events
ALTER TABLE ONLY status_page_settings ALTER TABLE ONLY status_page_settings
ADD CONSTRAINT fk_rails_506e5ba391 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_506e5ba391 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_project_monthly_usages
ADD CONSTRAINT fk_rails_508bcd4aa6 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY project_repository_storage_moves ALTER TABLE ONLY project_repository_storage_moves
ADD CONSTRAINT fk_rails_5106dbd44a FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_5106dbd44a FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
...@@ -16,7 +16,7 @@ module Types ...@@ -16,7 +16,7 @@ module Types
description: 'Name of the project.' description: 'Name of the project.'
def name def name
object.project.name object.project&.name
end end
end end
end end
......
...@@ -96,8 +96,9 @@ module Ci ...@@ -96,8 +96,9 @@ module Ci
def project_usage def project_usage
strong_memoize(:project_usage) do strong_memoize(:project_usage) do
::Ci::Minutes::ProjectMonthlyUsage.find_or_create_current(project_id: @project_id) if @project.present?
rescue ActiveRecord::InvalidForeignKey ::Ci::Minutes::ProjectMonthlyUsage.find_or_create_current(project_id: @project_id)
end
end end
end end
......
...@@ -111,4 +111,11 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do ...@@ -111,4 +111,11 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do
it_behaves_like 'namespace monthly usage' it_behaves_like 'namespace monthly usage'
end end
end end
context 'loose foreign key on ci_project_monthly_usages.project_id' do
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:project) }
let!(:model) { create(:ci_project_monthly_usage, project: parent) }
end
end
end end
...@@ -129,6 +129,10 @@ packages_package_file_build_infos: ...@@ -129,6 +129,10 @@ packages_package_file_build_infos:
- table: ci_pipelines - table: ci_pipelines
column: pipeline_id column: pipeline_id
on_delete: async_nullify on_delete: async_nullify
ci_project_monthly_usages:
- table: projects
column: project_id
on_delete: async_delete
pages_deployments: pages_deployments:
- table: ci_builds - table: ci_builds
column: ci_build_id column: ci_build_id
......
...@@ -24,7 +24,6 @@ RSpec.describe 'cross-database foreign keys' do ...@@ -24,7 +24,6 @@ RSpec.describe 'cross-database foreign keys' do
ci_pipeline_schedules.owner_id ci_pipeline_schedules.owner_id
ci_pipeline_schedules.project_id ci_pipeline_schedules.project_id
ci_pipelines.project_id ci_pipelines.project_id
ci_project_monthly_usages.project_id
ci_resource_groups.project_id ci_resource_groups.project_id
ci_runner_namespaces.namespace_id ci_runner_namespaces.namespace_id
ci_running_builds.project_id ci_running_builds.project_id
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment