From 533ad4ac834baef990e3ebf613c2b1fe54f13127 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 16 Oct 2023 15:12:07 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- ..._for_ci_sources_pipelines_pipeline_id_bigint.rb | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb (limited to 'db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb') diff --git a/db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb b/db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb new file mode 100644 index 00000000000..6ce4e1a4da5 --- /dev/null +++ b/db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +class SwapColumnsForCiSourcesPipelinesPipelineIdBigint < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + TABLE_NAME = :ci_sources_pipelines + TARGET_TABLE_NAME = :ci_pipelines + TRIGGER_FUNCTION_NAME = :trigger_68d7b6653c7d + COLUMN_NAMES = %i[pipeline_id source_pipeline_id] + BIGINT_COLUMN_NAMES = %i[pipeline_id_convert_to_bigint source_pipeline_id_convert_to_bigint] + FK_NAMES = [ + :fk_e1bad85861, # for pipeline_id + :fk_d4e29af7d7 # for source_pipeline_id + ] + BIGINT_FK_NAMES = [ + :fk_c1b5dc6b6f, # for pipeline_id_convert_to_bigint + :fk_1df371767f # for source_pipeline_id_convert_to_bigint + ] + INDEX_NAMES = %i[ + index_ci_sources_pipelines_on_pipeline_id + index_ci_sources_pipelines_on_source_pipeline_id + ] + BIGINT_INDEX_NAMES = %i[ + index_ci_sources_pipelines_on_pipeline_id_bigint + index_ci_sources_pipelines_on_source_pipeline_id_bigint + ] + + def up + swap + end + + def down + swap + end + + private + + def swap + with_lock_retries(raise_on_exhaustion: true) do + # Lock the tables involved. + execute "LOCK TABLE #{TARGET_TABLE_NAME}, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE" + + # Rename the columns to swap names + COLUMN_NAMES.each_with_index do |column_name, i| + bigint_column_name = BIGINT_COLUMN_NAMES[i] + temp_name = "temp_#{column_name}" + + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{column_name} TO #{temp_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{bigint_column_name} TO #{column_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{temp_name} TO #{bigint_column_name}" + end + + # Reset the trigger function + execute "ALTER FUNCTION #{quote_column_name(TRIGGER_FUNCTION_NAME)} RESET ALL" + + # Swap fkey constraint + FK_NAMES.each_with_index do |fk_name, i| + bigint_fk_name = BIGINT_FK_NAMES[i] + temp_fk_name = "temp_#{fk_name}" + + execute "ALTER TABLE #{TABLE_NAME} RENAME CONSTRAINT #{fk_name} TO #{temp_fk_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME CONSTRAINT #{bigint_fk_name} TO #{fk_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME CONSTRAINT #{temp_fk_name} TO #{bigint_fk_name}" + end + + # Swap index + INDEX_NAMES.each_with_index do |index_name, i| + bigint_index_name = BIGINT_INDEX_NAMES[i] + temp_index_name = "temp_#{index_name}" + + execute "ALTER INDEX #{index_name} RENAME TO #{temp_index_name}" + execute "ALTER INDEX #{bigint_index_name} RENAME TO #{index_name}" + execute "ALTER INDEX #{temp_index_name} RENAME TO #{bigint_index_name}" + end + end + end +end -- cgit v1.2.3