From d3283abc08560c48ca037514f0c0e4186de9f41f Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 28 Jul 2021 00:10:24 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- ...lize_ci_build_trace_chunks_bigint_conversion.rb | 67 ++++++++++++++++++++++ db/schema_migrations/20210714015537 | 1 + db/structure.sql | 4 +- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 db/post_migrate/20210714015537_finalize_ci_build_trace_chunks_bigint_conversion.rb create mode 100644 db/schema_migrations/20210714015537 (limited to 'db') diff --git a/db/post_migrate/20210714015537_finalize_ci_build_trace_chunks_bigint_conversion.rb b/db/post_migrate/20210714015537_finalize_ci_build_trace_chunks_bigint_conversion.rb new file mode 100644 index 00000000000..9195b662776 --- /dev/null +++ b/db/post_migrate/20210714015537_finalize_ci_build_trace_chunks_bigint_conversion.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +class FinalizeCiBuildTraceChunksBigintConversion < ActiveRecord::Migration[6.1] + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + TABLE_NAME = 'ci_build_trace_chunks' + + def up + ensure_batched_background_migration_is_finished( + job_class_name: 'CopyColumnUsingBackgroundMigrationJob', + table_name: TABLE_NAME, + column_name: 'id', + job_arguments: [['build_id'], ['build_id_convert_to_bigint']] + ) + + swap + end + + def down + swap + end + + private + + def swap + # This is to replace the existing "index_ci_build_trace_chunks_on_build_id_and_chunk_index" UNIQUE, btree (build_id, chunk_index) + add_concurrent_index TABLE_NAME, [:build_id_convert_to_bigint, :chunk_index], unique: true, name: 'i_ci_build_trace_chunks_build_id_convert_to_bigint_chunk_index' + + # Add a foreign key on `build_id_convert_to_bigint` before we swap the columns and drop the old FK () + add_concurrent_foreign_key TABLE_NAME, :ci_builds, column: :build_id_convert_to_bigint, on_delete: :cascade, name: 'fk_rails_1013b761f2_tmp' + + with_lock_retries(raise_on_exhaustion: true) do + # We'll need ACCESS EXCLUSIVE lock on the related tables, + # lets make sure it can be acquired from the start + execute "LOCK TABLE #{TABLE_NAME}, ci_builds IN ACCESS EXCLUSIVE MODE" + + # Swap column names + temp_name = 'build_id_tmp' + execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(:build_id)} TO #{quote_column_name(temp_name)}" + execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(:build_id_convert_to_bigint)} TO #{quote_column_name(:build_id)}" + execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(temp_name)} TO #{quote_column_name(:build_id_convert_to_bigint)}" + + # We need to update the trigger function in order to make PostgreSQL to + # regenerate the execution plan for it. This is to avoid type mismatch errors like + # "type of parameter 15 (bigint) does not match that when preparing the plan (integer)" + function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table(TABLE_NAME).name(:build_id, :build_id_convert_to_bigint) + execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL" + + # Swap defaults + change_column_default TABLE_NAME, :build_id, nil + change_column_default TABLE_NAME, :build_id_convert_to_bigint, 0 + + # Rename the index on the `bigint` column to match the new column name + # (we already hold an exclusive lock, so no need to use DROP INDEX CONCURRENTLY here) + execute 'DROP INDEX index_ci_build_trace_chunks_on_build_id_and_chunk_index' + rename_index TABLE_NAME, 'i_ci_build_trace_chunks_build_id_convert_to_bigint_chunk_index', 'index_ci_build_trace_chunks_on_build_id_and_chunk_index' + + # Drop original FK on the old int4 `build_id` (fk_rails_1013b761f2) + remove_foreign_key TABLE_NAME, name: 'fk_rails_1013b761f2' + # We swapped the columns but the FK for buil_id is still using the temporary name for the build_id_convert_to_bigint column + # So we have to also swap the FK name now that we dropped the other one with the same + rename_constraint(TABLE_NAME, 'fk_rails_1013b761f2_tmp', 'fk_rails_1013b761f2') + end + end +end diff --git a/db/schema_migrations/20210714015537 b/db/schema_migrations/20210714015537 new file mode 100644 index 00000000000..eb1c88e29a0 --- /dev/null +++ b/db/schema_migrations/20210714015537 @@ -0,0 +1 @@ +2f6441a5d5e3c4aad6b88c3944436dab213b7eeb2f35453657ffac8c0733efc1 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index cadaa69a87d..9f631d08b93 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -10475,13 +10475,13 @@ ALTER SEQUENCE ci_build_report_results_build_id_seq OWNED BY ci_build_report_res CREATE TABLE ci_build_trace_chunks ( id bigint NOT NULL, - build_id integer NOT NULL, + build_id_convert_to_bigint integer DEFAULT 0 NOT NULL, chunk_index integer NOT NULL, data_store integer NOT NULL, raw_data bytea, checksum bytea, lock_version integer DEFAULT 0 NOT NULL, - build_id_convert_to_bigint bigint DEFAULT 0 NOT NULL + build_id bigint NOT NULL ); CREATE SEQUENCE ci_build_trace_chunks_id_seq -- cgit v1.2.1