Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20221021145820_create_routing_table_for_builds_metadata_v2.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5f1ba5cb876d56e978513c91e0c625a3cb222cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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