diff --git a/db/post_migrate/20240202120723_finalize_bigint_conversion_of_geo_event_id.rb b/db/post_migrate/20240202120723_finalize_bigint_conversion_of_geo_event_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..71634d468dce9cb960382c52983573a054767bd6 --- /dev/null +++ b/db/post_migrate/20240202120723_finalize_bigint_conversion_of_geo_event_id.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class FinalizeBigintConversionOfGeoEventId < Gitlab::Database::Migration[2.2] + include Gitlab::Database::MigrationHelpers::ConvertToBigint + + disable_ddl_transaction! + + milestone '16.9' + restrict_gitlab_migration gitlab_schema: :gitlab_main + + TABLE_NAME = 'geo_event_log' + COLUMN_NAME = 'geo_event_id' + BIGINT_COLUMN_NAME = 'geo_event_id_convert_to_bigint' + + def up + ensure_batched_background_migration_is_finished( + job_class_name: 'CopyColumnUsingBackgroundMigrationJob', + table_name: TABLE_NAME, + column_name: COLUMN_NAME, + job_arguments: [[COLUMN_NAME], [BIGINT_COLUMN_NAME]] + ) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20240205084846_swap_bigint_geo_event_id.rb b/db/post_migrate/20240205084846_swap_bigint_geo_event_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..bfda8aac539fc27d159be596b1a57c6944bb872e --- /dev/null +++ b/db/post_migrate/20240205084846_swap_bigint_geo_event_id.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +class SwapBigintGeoEventId < Gitlab::Database::Migration[2.2] + include Gitlab::Database::MigrationHelpers::ConvertToBigint + + milestone '16.9' + + disable_ddl_transaction! + + TABLE_NAME = 'geo_event_log' + COLUMN_NAME = 'geo_event_id' + INDEX_NAME = 'index_geo_event_log_on_geo_event_id' + BIGINT_COLUMN_NAME = 'geo_event_id_convert_to_bigint' + + # For the FK from 'geo_event_log' table referencing 'geo_events' + FK_SOURCE_TABLE_NAME = 'geo_events' + FK_NAME = 'fk_geo_event_log_on_geo_event_id' + TEMP_FK_NAME = 'fk_geo_event_id_convert_to_bigint' + + def up + swap + end + + def down + swap + end + + def swap + add_bigint_column_indexes TABLE_NAME, COLUMN_NAME + + unless foreign_key_exists?(TABLE_NAME, name: TEMP_FK_NAME) + add_concurrent_foreign_key TABLE_NAME, FK_SOURCE_TABLE_NAME, + name: TEMP_FK_NAME, + on_delete: :cascade, + column: BIGINT_COLUMN_NAME + end + + with_lock_retries(raise_on_exhaustion: true) do + # Lock the table to avoid deadlocks + execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE" + + # Swap the column names + temp_name = quote_column_name('id_tmp') + id_name = quote_column_name(COLUMN_NAME) + id_convert_to_bigint_name = quote_column_name(BIGINT_COLUMN_NAME) + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{id_name} TO #{temp_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{id_convert_to_bigint_name} TO #{id_name}" + execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN #{temp_name} TO #{id_convert_to_bigint_name}" + + # Reset the trigger function + function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table( + TABLE_NAME, connection: connection).name( + COLUMN_NAME, + BIGINT_COLUMN_NAME + ) + execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL" + end + + # Rename the temporary FK + execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT #{FK_NAME} CASCADE" + rename_constraint TABLE_NAME, TEMP_FK_NAME, FK_NAME + + # Rename index + execute "DROP INDEX CONCURRENTLY #{INDEX_NAME}" + rename_index TABLE_NAME, bigint_index_name(INDEX_NAME), INDEX_NAME + end +end diff --git a/db/schema_migrations/20240202120723 b/db/schema_migrations/20240202120723 new file mode 100644 index 0000000000000000000000000000000000000000..292e174d223c53f28b27d28cf3676e726fd4b12d --- /dev/null +++ b/db/schema_migrations/20240202120723 @@ -0,0 +1 @@ +16c4e020edd4300dc246ccb8b2f101de8e9c8767dfae918659940e461776d598 \ No newline at end of file diff --git a/db/schema_migrations/20240205084846 b/db/schema_migrations/20240205084846 new file mode 100644 index 0000000000000000000000000000000000000000..1e30139eb280ec0e183d62f542fde19beb34ce0d --- /dev/null +++ b/db/schema_migrations/20240205084846 @@ -0,0 +1 @@ +b2d951df7d6f27ef267984f233e30e9263b3db5de0107f914e5999738b81cb87 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 0296442ee4df3db7f12b82236e0176b3f8c2967e..2defc153e855e73f6a26c7cda2fab90ef541aef4 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -17347,8 +17347,8 @@ CREATE TABLE geo_event_log ( hashed_storage_attachments_event_id bigint, reset_checksum_event_id bigint, cache_invalidation_event_id bigint, - geo_event_id integer, - geo_event_id_convert_to_bigint bigint + geo_event_id_convert_to_bigint integer, + geo_event_id bigint ); CREATE SEQUENCE geo_event_log_id_seq