From 5bf2cd533f851a87a2a7d753696760748423a3d6 Mon Sep 17 00:00:00 2001 From: David Kim Date: Thu, 18 Sep 2025 17:52:26 +1000 Subject: [PATCH] WIP: handle default FK name consistently on partitioned table --- .../async_constraints/migration_helpers.rb | 16 ------------- .../foreign_key_helpers.rb | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/gitlab/database/async_constraints/migration_helpers.rb b/lib/gitlab/database/async_constraints/migration_helpers.rb index 0e53b12586aac3..4615ea6ee5bc89 100644 --- a/lib/gitlab/database/async_constraints/migration_helpers.rb +++ b/lib/gitlab/database/async_constraints/migration_helpers.rb @@ -41,22 +41,6 @@ def unprepare_async_foreign_key_validation(table_name, column_name = nil, name: end end - def prepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) - ensure_async_constraint_validation_is_enabled do - Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition| - prepare_async_foreign_key_validation(partition.identifier, column_name, name: name) - end - end - end - - def unprepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) - ensure_async_constraint_validation_is_enabled do - Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition| - unprepare_async_foreign_key_validation(partition.identifier, column_name, name: name) - end - end - end - # Prepares a check constraint for asynchronous validation. # # Stores the constraint information in the postgres_async_foreign_key_validations diff --git a/lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb b/lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb index 67d16b48d2f715..0cbb40f5f0d89c 100644 --- a/lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb +++ b/lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb @@ -135,6 +135,30 @@ def swap_partitioned_foreign_keys(table_name, old_foreign_key, new_foreign_key) swap_foreign_keys(partitioned_table.name, old_foreign_key, new_foreign_key) end + # Prepares a partitioned foreign key for asynchronous validation. + # + # This method prepares each partition of the table for async validation + # using the same default naming convention as add_concurrent_partitioned_foreign_key. + def prepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) + fk_name = name || concurrent_partitioned_foreign_key_name(table_name, column_name) + + Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition| + prepare_async_foreign_key_validation(partition.identifier, column_name, name: fk_name) + end + end + + # Removes a partitioned foreign key from asynchronous validation. + # + # This method removes each partition of the table from async validation + # using the same default naming convention as add_concurrent_partitioned_foreign_key. + def unprepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) + fk_name = name || concurrent_partitioned_foreign_key_name(table_name, column_name) + + Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition| + unprepare_async_foreign_key_validation(partition.identifier, column_name, name: fk_name) + end + end + private # Returns the name for a concurrent partitioned foreign key. -- GitLab