diff --git a/db/post_migrate/20250307080100_switch_record_change_tracking_to_p_ci_runner_machines_table.rb b/db/post_migrate/20250307080100_switch_record_change_tracking_to_p_ci_runner_machines_table.rb index 52bcb1bea607d44e2522eca931504addfb15b2e4..391ffcec90cb07182f22baad056f5853b36b6c9e 100644 --- a/db/post_migrate/20250307080100_switch_record_change_tracking_to_p_ci_runner_machines_table.rb +++ b/db/post_migrate/20250307080100_switch_record_change_tracking_to_p_ci_runner_machines_table.rb @@ -1,50 +1,14 @@ # frozen_string_literal: true class SwitchRecordChangeTrackingToPCiRunnerMachinesTable < Gitlab::Database::Migration[2.2] - include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers - - disable_ddl_transaction! - milestone '17.10' - PARTITIONED_TABLE = :ci_runner_machines - TABLES = %i[ - instance_type_ci_runner_machines group_type_ci_runner_machines project_type_ci_runner_machines ci_runner_machines - ].freeze - def up - # Remove current LFK trigger that is currently assigned to ci_runner_machines_archived, due to the call to - # replace_with_partitioned_table: - # CREATE TRIGGER ci_runner_machines_loose_fk_trigger AFTER DELETE ON ci_runner_machines_archived - # REFERENCING OLD TABLE AS old_table - # FOR EACH STATEMENT EXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records(); - drop_trigger(:ci_runner_machines_archived, :ci_runner_machines_loose_fk_trigger, if_exists: true) - - # Reattaching the new trigger function to the existing partitioned tables - # but with an overridden table name - TABLES.each do |table| - with_lock_retries do - untrack_record_deletions(table) - - track_record_deletions_override_table_name(table, PARTITIONED_TABLE) - end - end + # no-op due to https://gitlab.com/gitlab-com/gl-infra/production/-/issues/19476, retried in + # RetrySwitchRecordChangeTrackingToPCiRunnerMachinesTable end def down - TABLES.each do |table| - with_lock_retries do - untrack_record_deletions(table) - end - end - - # Mimic track_record_deletions but in a way to restore the previous state - # (i.e. trigger on ci_runner_machines_archived but still named ci_runner_machines_loose_fk_trigger) - execute(<<~SQL.squish) - CREATE TRIGGER #{record_deletion_trigger_name(PARTITIONED_TABLE)} - AFTER DELETE ON ci_runner_machines_archived REFERENCING OLD TABLE AS old_table - FOR EACH STATEMENT - EXECUTE FUNCTION #{INSERT_FUNCTION_NAME}(); - SQL + # no-op end end diff --git a/db/post_migrate/20250307080101_retry_switch_record_change_tracking_to_p_ci_runner_machines_table.rb b/db/post_migrate/20250307080101_retry_switch_record_change_tracking_to_p_ci_runner_machines_table.rb new file mode 100644 index 0000000000000000000000000000000000000000..73fe185c7b5f60cda13a159b345591107c5f0768 --- /dev/null +++ b/db/post_migrate/20250307080101_retry_switch_record_change_tracking_to_p_ci_runner_machines_table.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +class RetrySwitchRecordChangeTrackingToPCiRunnerMachinesTable < Gitlab::Database::Migration[2.2] + include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers + + disable_ddl_transaction! + + milestone '17.10' + + PARTITIONED_TABLE = :ci_runner_machines + TABLES = %i[ + instance_type_ci_runner_machines group_type_ci_runner_machines project_type_ci_runner_machines ci_runner_machines + ].freeze + + def up + # Remove current LFK trigger that is currently assigned to ci_runner_machines_archived, due to the call to + # replace_with_partitioned_table: + # CREATE TRIGGER ci_runner_machines_loose_fk_trigger AFTER DELETE ON ci_runner_machines_archived + # REFERENCING OLD TABLE AS old_table + # FOR EACH STATEMENT EXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records(); + with_lock_retries do + # rubocop:disable Migration/WithLockRetriesDisallowedMethod -- false positive + drop_trigger(:ci_runner_machines_archived, :ci_runner_machines_loose_fk_trigger) + # rubocop:enable Migration/WithLockRetriesDisallowedMethod + end + + # Reattaching the new trigger function to the existing partitioned tables + # but with an overridden table name + TABLES.each do |table| + with_lock_retries do + untrack_record_deletions(table) + + track_record_deletions_override_table_name(table, PARTITIONED_TABLE) + end + end + end + + def down + TABLES.each do |table| + with_lock_retries do + untrack_record_deletions(table) + end + end + + # Mimic track_record_deletions but in a way to restore the previous state + # (i.e. trigger on ci_runner_machines_archived but still named ci_runner_machines_loose_fk_trigger) + with_lock_retries do + execute(<<~SQL.squish) + CREATE TRIGGER #{record_deletion_trigger_name(PARTITIONED_TABLE)} + AFTER DELETE ON ci_runner_machines_archived REFERENCING OLD TABLE AS old_table + FOR EACH STATEMENT + EXECUTE FUNCTION #{INSERT_FUNCTION_NAME}(); + SQL + end + end +end diff --git a/db/schema_migrations/20250307080101 b/db/schema_migrations/20250307080101 new file mode 100644 index 0000000000000000000000000000000000000000..5a604458634381653d40106d31609c5563425434 --- /dev/null +++ b/db/schema_migrations/20250307080101 @@ -0,0 +1 @@ +3c998c5fb4772cdd55ce6018628cc76189793c2d4afd03de2a9affa98d0025a8 \ No newline at end of file