diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
commit | e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch) | |
tree | 2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb | |
parent | ffda4e7bcac36987f936b4ba515995a6698698f0 (diff) |
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb')
-rw-r--r-- | db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb b/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb new file mode 100644 index 00000000000..7533de86a2a --- /dev/null +++ b/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +class SwapCiBuildNeedsToBigIntForSelfHosts < Gitlab::Database::Migration[2.1] + include Gitlab::Database::MigrationHelpers::ConvertToBigint + + disable_ddl_transaction! + + TABLE_NAME = 'ci_build_needs' + + def up + return if should_skip? + return if temporary_column_already_dropped? + return if columns_already_swapped? + + swap + end + + def down + return if should_skip? + return if temporary_column_already_dropped? + return unless columns_already_swapped? + + swap + end + + private + + def swap + add_concurrent_index TABLE_NAME, :id_convert_to_bigint, unique: true, name: + 'index_ci_build_needs_on_id_convert_to_bigint' + + with_lock_retries(raise_on_exhaustion: true) do + execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE" + + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id TO id_tmp" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_convert_to_bigint TO id" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_tmp TO id_convert_to_bigint" + + function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table( + TABLE_NAME, connection: Ci::ApplicationRecord.connection + ).name( + :id, :id_convert_to_bigint + ) + + execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL" + + execute "ALTER SEQUENCE ci_build_needs_id_seq OWNED BY #{TABLE_NAME}.id" + change_column_default TABLE_NAME, :id, -> { "nextval('ci_build_needs_id_seq'::regclass)" } + change_column_default TABLE_NAME, :id_convert_to_bigint, 0 + + execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT ci_build_needs_pkey CASCADE" + rename_index TABLE_NAME, 'index_ci_build_needs_on_id_convert_to_bigint', 'ci_build_needs_pkey' + execute "ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT ci_build_needs_pkey PRIMARY KEY USING INDEX ci_build_needs_pkey" + end + end + + def should_skip? + com_or_dev_or_test_but_not_jh? + end + + def columns_already_swapped? + table_columns = columns(TABLE_NAME) + column_id = table_columns.find { |c| c.name == 'id' } + column_id_convert_to_bigint = table_columns.find { |c| c.name == 'id_convert_to_bigint' } + + column_id.sql_type == 'bigint' && column_id_convert_to_bigint.sql_type == 'integer' + end + + def temporary_column_already_dropped? + table_columns = columns(TABLE_NAME) + + !table_columns.find { |c| c.name == 'id_convert_to_bigint' } + end +end |