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

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

class AddMissingExpressionIndexes < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  INDEXES = [
    [:namespaces, :index_on_namespaces_lower_name, 'LOWER(name)'],
    [:namespaces, :index_on_namespaces_lower_path, 'LOWER(path)'],
    [:projects, :index_on_projects_lower_path, 'LOWER(path)'],
    [:routes, :index_on_routes_lower_path, 'LOWER(path)'],
    [:users, :index_on_users_lower_username, 'LOWER(username)'],
    [:users, :index_on_users_lower_email, 'LOWER(email)']
  ]

  def up
    # Those indexes had been introduced before, but they haven't been
    # captured in structure.sql. For installations that already have it,
    # this is a no-op - others will get it retroactively with
    # this migration.

    tables = Set.new

    INDEXES.each do |(table, name, expression)|
      unless index_name_exists?(table, name)
        add_concurrent_index table, expression, name: name
        tables.add(table)
      end
    end

    # Rebuild statistics on affected tables only
    tables.each do |table|
      execute("ANALYZE #{table}")
    end
  end

  def down
    # no-op
  end
end