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

20221102090943_create_second_partition_for_builds_metadata.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6923e6f6cbafdb6ed3396635a4caae47ac0d375a (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

class CreateSecondPartitionForBuildsMetadata < Gitlab::Database::Migration[2.0]
  TABLE_NAME = 'p_ci_builds_metadata'
  BUILDS_TABLE = 'ci_builds'
  NEXT_PARTITION_ID = 101
  PARTITION_NAME = 'gitlab_partitions_dynamic.ci_builds_metadata_101'

  disable_ddl_transaction!

  def up
    return unless Gitlab.com?

    with_lock_retries(**lock_args) do
      connection.execute(<<~SQL)
        LOCK TABLE #{BUILDS_TABLE} IN SHARE UPDATE EXCLUSIVE MODE;
        LOCK TABLE ONLY #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE;
      SQL

      connection.execute(<<~SQL)
        CREATE TABLE IF NOT EXISTS #{PARTITION_NAME}
          PARTITION OF #{TABLE_NAME}
          FOR VALUES IN (#{NEXT_PARTITION_ID});
      SQL
    end
  end

  def down
    return unless Gitlab.com?
    return unless table_exists?(PARTITION_NAME)

    with_lock_retries(**lock_args) do
      connection.execute(<<~SQL)
        LOCK TABLE #{BUILDS_TABLE}, #{TABLE_NAME}, #{PARTITION_NAME} IN ACCESS EXCLUSIVE MODE;
      SQL

      connection.execute(<<~SQL)
        ALTER TABLE #{TABLE_NAME} DETACH PARTITION #{PARTITION_NAME};
      SQL

      connection.execute(<<~SQL)
        DROP TABLE IF EXISTS #{PARTITION_NAME} CASCADE;
      SQL
    end
  end

  private

  def lock_args
    {
      raise_on_exhaustion: true,
      timing_configuration: lock_timing_configuration
    }
  end

  def lock_timing_configuration
    iterations = Gitlab::Database::WithLockRetries::DEFAULT_TIMING_CONFIGURATION
    aggressive_iterations = Array.new(5) { [10.seconds, 1.minute] }

    iterations + aggressive_iterations
  end
end