diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-10-19 15:57:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-10-19 15:57:54 +0300 |
commit | 419c53ec62de6e97a517abd5fdd4cbde3a942a34 (patch) | |
tree | 1f43a548b46bca8a5fb8fe0c31cef1883d49c5b6 /db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb | |
parent | 1da20d9135b3ad9e75e65b028bffc921aaf8deb7 (diff) |
Add latest changes from gitlab-org/gitlab@16-5-stable-eev16.5.0-rc42
Diffstat (limited to 'db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb')
-rw-r--r-- | db/post_migrate/20231004091113_swap_columns_for_ci_sources_pipelines_pipeline_id_bigint.rb | 77 |
1 files changed, 77 insertions, 0 deletions
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 |