diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
commit | 585826cb22ecea5998a2c2a4675735c94bdeedac (patch) | |
tree | 5b05f0b30d33cef48963609e8a18a4dff260eab3 /db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb | |
parent | df221d036e5d0c6c0ee4d55b9c97f481ee05dee8 (diff) |
Add latest changes from gitlab-org/gitlab@16-6-stable-eev16.6.0-rc42
Diffstat (limited to 'db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb')
-rw-r--r-- | db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb b/db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb new file mode 100644 index 00000000000..4065bad5fb5 --- /dev/null +++ b/db/post_migrate/20231019003052_swap_columns_for_ci_pipelines_pipeline_id_bigint_v2.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +class SwapColumnsForCiPipelinesPipelineIdBigintV2 < Gitlab::Database::Migration[2.1] + include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum + include Gitlab::Database::MigrationHelpers::Swapping + + disable_ddl_transaction! + + TABLE_NAME = :ci_pipelines + TRIGGER_FUNCTION_NAME = :trigger_1bd97da9c1a4 + COLUMN_NAME = :auto_canceled_by_id + BIGINT_COLUMN_NAME = :auto_canceled_by_id_convert_to_bigint + FK_NAME = :fk_262d4c2d19 + BIGINT_FK_NAME = :fk_67e4288f3a + INDEX_NAME = :index_ci_pipelines_on_auto_canceled_by_id + BIGINT_INDEX_NAME = :index_ci_pipelines_on_auto_canceled_by_id_bigint + + def up + return if should_skip? || column_type_of?(:bigint) + + swap + end + + def down + return if should_skip? || column_type_of?(:integer) + + swap + end + + private + + def should_skip? + !can_execute_on?(TABLE_NAME) + end + + def column_type_of?(type) + column_for(TABLE_NAME, COLUMN_NAME).sql_type.to_s == type.to_s + end + + def swap + # rubocop:disable Migration/WithLockRetriesDisallowedMethod + with_lock_retries(raise_on_exhaustion: true) do + # Lock the tables involved. + lock_tables(TABLE_NAME) + + # Rename the columns to swap names + swap_columns(TABLE_NAME, COLUMN_NAME, BIGINT_COLUMN_NAME) + + # Reset the trigger function + reset_trigger_function(TRIGGER_FUNCTION_NAME) + + # Swap fkey constraint + swap_foreign_keys(TABLE_NAME, FK_NAME, BIGINT_FK_NAME) + + # Swap index + swap_indexes(TABLE_NAME, INDEX_NAME, BIGINT_INDEX_NAME) + end + # rubocop:enable Migration/WithLockRetriesDisallowedMethod + end +end |