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

20231109120223_ensure_id_uniqueness_for_p_ci_builds_v4.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 994755dbd2856277d7357deb0a51defa1ec76667 (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
# frozen_string_literal: true

class EnsureIdUniquenessForPCiBuildsV4 < Gitlab::Database::Migration[2.2]
  include Gitlab::Database::SchemaHelpers
  include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum

  enable_lock_retries!
  milestone '16.7'

  TABLE_NAME = :p_ci_builds
  FUNCTION_NAME = :assign_p_ci_builds_id_value
  TRIGGER_NAME = :assign_p_ci_builds_id_trigger

  def up
    return unless should_run?

    lock_tables(TABLE_NAME, :ci_builds)

    Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition|
      drop_trigger(partition.identifier, TRIGGER_NAME, if_exists: true)
    end

    create_trigger(TABLE_NAME, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE INSERT')
  end

  def down
    return unless should_run?

    drop_trigger(TABLE_NAME, TRIGGER_NAME, if_exists: true)
    return if trigger_exists?(:ci_builds, TRIGGER_NAME)

    Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition|
      create_trigger(partition.identifier, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE INSERT')
    end
  end

  private

  def should_run?
    can_execute_on?(:ci_builds)
  end
end