summaryrefslogtreecommitdiff
path: root/db/post_migrate/20220213104531_create_indexes_on_integration_type_new.rb
blob: 3a9f48dec4445883d40ec7e20162cfc2b75d715e (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
# frozen_string_literal: true

# Reproduce the indices on integrations.type on integrations.type_new
class CreateIndexesOnIntegrationTypeNew < Gitlab::Database::Migration[1.0]
  disable_ddl_transaction!

  TABLE_NAME = :integrations
  COLUMN = :type_new

  def indices
    [
      {
        name: "index_integrations_on_project_and_#{COLUMN}_where_inherit_null",
        columns: [:project_id, COLUMN],
        where: 'inherit_from_id IS NULL'
      },
      {
        name: "index_integrations_on_project_id_and_#{COLUMN}_unique",
        columns: [:project_id, COLUMN],
        unique: true
      },
      {
        name: "index_integrations_on_#{COLUMN}",
        columns: [COLUMN]
      },
      {
        name: "index_integrations_on_#{COLUMN}_and_instance_partial",
        columns: [COLUMN, :instance],
        where: 'instance = true'
      },
      {
        name: "index_integrations_on_#{COLUMN}_and_template_partial",
        columns: [COLUMN, :template],
        where: 'template = true'
      },
      {
        # column names are limited to 63 characters, so this one is re-worded for clarity
        name: "index_integrations_on_#{COLUMN}_id_when_active_and_has_project",
        columns: [COLUMN, :id],
        where: '((active = true) AND (project_id IS NOT NULL))'
      },
      {
        name: "index_integrations_on_unique_group_id_and_#{COLUMN}",
        columns: [:group_id, COLUMN]
      }
    ]
  end

  def up
    indices.each do |index|
      add_concurrent_index TABLE_NAME, index[:columns], index.except(:columns)
    end
  end

  def down
    indices.each do |index|
      remove_concurrent_index_by_name TABLE_NAME, index[:name]
    end
  end
end