Module: Gitlab::Database::AsyncConstraints::MigrationHelpers

Included in:
MigrationHelpers
Defined in:
lib/gitlab/database/async_constraints/migration_helpers.rb

Instance Method Summary collapse

Instance Method Details

#prepare_async_check_constraint_validation(table_name, name:) ⇒ Object

Prepares a check constraint for asynchronous validation.

Stores the constraint information in the postgres_async_foreign_key_validations table to be executed later.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 65

def prepare_async_check_constraint_validation(table_name, name:)
  ensure_async_constraint_validation_is_enabled do
    unless check_constraint_exists?(table_name, name)
      raise missing_schema_object_message(table_name, "check constraint", name)
    end

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

    Gitlab::AppLogger.info(
      message: 'Prepared check constraint for async validation',
      table_name: async_validation.table_name,
      constraint_name: async_validation.name)

    async_validation
  end
end

#prepare_async_foreign_key_validation(table_name, column_name = nil, name: nil) ⇒ Object

Prepares a foreign key for asynchronous validation.

Stores the FK information in the postgres_async_foreign_key_validations table to be executed later.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 12

def prepare_async_foreign_key_validation(table_name, column_name = nil, name: nil)
  ensure_async_constraint_validation_is_enabled do
    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
end

#prepare_partitioned_async_check_constraint_validation(table_name, name: nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 93

def prepare_partitioned_async_check_constraint_validation(table_name, name: nil)
  ensure_async_constraint_validation_is_enabled do
    Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
      prepare_async_check_constraint_validation(partition.identifier, name: name)
    end
  end
end

#prepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 44

def prepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil)
  ensure_async_constraint_validation_is_enabled do
    Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
      prepare_async_foreign_key_validation(partition.identifier, column_name, name: name)
    end
  end
end

#unprepare_async_check_constraint_validation(table_name, name:) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 84

def unprepare_async_check_constraint_validation(table_name, name:)
  ensure_async_constraint_validation_is_enabled do
    PostgresAsyncConstraintValidation
      .check_constraint_type
      .find_by(name: name, table_name: table_name)
      .try(&:destroy!)
  end
end

#unprepare_async_foreign_key_validation(table_name, column_name = nil, name: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 33

def unprepare_async_foreign_key_validation(table_name, column_name = nil, name: nil)
  ensure_async_constraint_validation_is_enabled do
    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
end

#unprepare_partitioned_async_check_constraint_validation(table_name, name: nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 101

def unprepare_partitioned_async_check_constraint_validation(table_name, name: nil)
  ensure_async_constraint_validation_is_enabled do
    Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
      unprepare_async_check_constraint_validation(partition.identifier, name: name)
    end
  end
end

#unprepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/gitlab/database/async_constraints/migration_helpers.rb', line 52

def unprepare_partitioned_async_foreign_key_validation(table_name, column_name = nil, name: nil)
  ensure_async_constraint_validation_is_enabled do
    Gitlab::Database::PostgresPartitionedTable.each_partition(table_name) do |partition|
      unprepare_async_foreign_key_validation(partition.identifier, column_name, name: name)
    end
  end
end