Commit 52bb1af1 authored by Krasimir Angelov's avatar Krasimir Angelov

Merge branch '325619-ci_builds-finalize-stage_id-conversion' into 'master'

Migration to swap ci_builds.stage_id column

See merge request gitlab-org/gitlab!66688
parents aa0a1d0c 4f2f2fc2
# frozen_string_literal: true
class FinalizeCiBuildsStageIdBigintConversion < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
TABLE_NAME = 'ci_builds'
def up
ensure_batched_background_migration_is_finished(
job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
table_name: TABLE_NAME,
column_name: 'id',
job_arguments: [%w[id stage_id], %w[id_convert_to_bigint stage_id_convert_to_bigint]]
)
swap_columns
end
def down
swap_columns
end
private
def swap_columns
# Create a copy of the original column's index on the new column
add_concurrent_index TABLE_NAME, :stage_id_convert_to_bigint, name: :index_ci_builds_on_converted_stage_id # rubocop:disable Migration/PreventIndexCreation
# Create a copy of the original column's FK on the new column
add_concurrent_foreign_key TABLE_NAME, :ci_stages, column: :stage_id_convert_to_bigint, on_delete: :cascade,
reverse_lock_order: true
with_lock_retries(raise_on_exhaustion: true) do
quoted_table_name = quote_table_name(TABLE_NAME)
quoted_referenced_table_name = quote_table_name(:ci_stages)
# Acquire locks up-front, not just to the build table but the FK's referenced table
execute "LOCK TABLE #{quoted_referenced_table_name}, #{quoted_table_name} IN ACCESS EXCLUSIVE MODE"
# Swap the column names of the two columns
temporary_name = 'stage_id_tmp'
execute "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(:stage_id)} TO #{quote_column_name(temporary_name)}"
execute "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(:stage_id_convert_to_bigint)} TO #{quote_column_name(:stage_id)}"
execute "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(temporary_name)} TO #{quote_column_name(:stage_id_convert_to_bigint)}"
# Reset the function so PG drops the plan cache for the incorrect integer type
function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table(TABLE_NAME)
.name([:id, :stage_id], [:id_convert_to_bigint, :stage_id_convert_to_bigint])
execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
# Remove the original column index, and rename the new column index to the original name
execute 'DROP INDEX index_ci_builds_on_stage_id'
rename_index TABLE_NAME, :index_ci_builds_on_converted_stage_id, :index_ci_builds_on_stage_id
# Remove the original column foreign key, and rename the new column foreign key to the original name
remove_foreign_key TABLE_NAME, name: concurrent_foreign_key_name(TABLE_NAME, :stage_id)
rename_constraint(
TABLE_NAME,
concurrent_foreign_key_name(TABLE_NAME, :stage_id_convert_to_bigint),
concurrent_foreign_key_name(TABLE_NAME, :stage_id))
end
end
end
da16754bf484ba9a585f9de055f242dbee311d7cf7d0ce67c834ebaed61b96d4
\ No newline at end of file
......@@ -11371,7 +11371,7 @@ CREATE TABLE ci_builds (
coverage_regex character varying,
auto_canceled_by_id integer,
retried boolean,
stage_id integer,
stage_id_convert_to_bigint integer,
protected boolean,
failure_reason integer,
scheduled_at timestamp with time zone,
......@@ -11382,7 +11382,7 @@ CREATE TABLE ci_builds (
processed boolean,
scheduling_type smallint,
id_convert_to_bigint bigint DEFAULT 0 NOT NULL,
stage_id_convert_to_bigint bigint,
stage_id bigint,
CONSTRAINT check_1e2fbd1b39 CHECK ((lock_version IS NOT NULL))
);
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