summaryrefslogtreecommitdiff
path: root/db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb')
-rw-r--r--db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb b/db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb
new file mode 100644
index 00000000000..e5f1ba5cb87
--- /dev/null
+++ b/db/post_migrate/20221021145820_create_routing_table_for_builds_metadata_v2.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+class CreateRoutingTableForBuildsMetadataV2 < Gitlab::Database::Migration[2.0]
+ include Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_builds_metadata
+ PARENT_TABLE_NAME = :p_ci_builds_metadata
+ FIRST_PARTITION = 100
+ PARTITION_COLUMN = :partition_id
+
+ def up
+ return if connection.table_exists?(PARENT_TABLE_NAME) && partition_attached?
+
+ convert_table_to_first_list_partition(
+ table_name: TABLE_NAME,
+ partitioning_column: PARTITION_COLUMN,
+ parent_table_name: PARENT_TABLE_NAME,
+ initial_partitioning_value: FIRST_PARTITION,
+ lock_tables: [:ci_builds, :ci_builds_metadata]
+ )
+ end
+
+ def down
+ revert_converting_table_to_first_list_partition(
+ table_name: TABLE_NAME,
+ partitioning_column: PARTITION_COLUMN,
+ parent_table_name: PARENT_TABLE_NAME,
+ initial_partitioning_value: FIRST_PARTITION
+ )
+ end
+
+ private
+
+ def partition_attached?
+ connection.select_value(<<~SQL)
+ SELECT true FROM postgres_partitions WHERE name = '#{TABLE_NAME}';
+ SQL
+ end
+end