summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/async_constraints/migration_helpers.rb
blob: 77ca78a4d5c70f1801e2744e06f5085e659fe9ee (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

module Gitlab
  module Database
    module AsyncConstraints
      module MigrationHelpers
        # Prepares a foreign key for asynchronous validation.
        #
        # Stores the FK information in the postgres_async_constraint_validations
        # table to be executed later.
        #
        def prepare_async_foreign_key_validation(table_name, column_name = nil, name: nil)
          Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode!

          return unless async_constraint_validation_available?

          fk_name = name || concurrent_foreign_key_name(table_name, column_name)

          unless foreign_key_exists?(table_name, name: fk_name)
            raise missing_schema_object_message(table_name, "foreign key", fk_name)
          end

          async_validation = PostgresAsyncConstraintValidation
            .foreign_key_type
            .find_or_create_by!(name: fk_name, table_name: table_name)

          Gitlab::AppLogger.info(
            message: 'Prepared FK for async validation',
            table_name: async_validation.table_name,
            fk_name: async_validation.name)

          async_validation
        end

        def unprepare_async_foreign_key_validation(table_name, column_name = nil, name: nil)
          Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode!

          return unless async_constraint_validation_available?

          fk_name = name || concurrent_foreign_key_name(table_name, column_name)

          PostgresAsyncConstraintValidation
            .foreign_key_type
            .find_by(name: fk_name, table_name: table_name)
            .try(&:destroy!)
        end

        def prepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil)
          Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode!

          return unless async_constraint_validation_available?

          Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
            prepare_async_foreign_key_validation(partition.identifier, column_name, name: name)
          end
        end

        def unprepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil)
          Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode!

          return unless async_constraint_validation_available?

          Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
            unprepare_async_foreign_key_validation(partition.identifier, column_name, name: name)
          end
        end

        private

        def async_constraint_validation_available?
          PostgresAsyncConstraintValidation.table_available?
        end
      end
    end
  end
end