summaryrefslogtreecommitdiff
path: root/db/post_migrate/20230310111859_recreate_user_type_migration_indexes.rb
blob: 539ce99a3193b1e952243686a690a2de73f14d97 (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
# frozen_string_literal: true

class RecreateUserTypeMigrationIndexes < Gitlab::Database::Migration[2.1]
  disable_ddl_transaction!

  INCORRECT_BILLABLE_INDEX = 'index_users_for_active_billable_users_migration'
  BILLABLE_INDEX = 'migrate_index_users_for_active_billable_users'

  def up
    # Temporary index to migrate human user_type. See https://gitlab.com/gitlab-org/gitlab/-/issues/386474
    add_concurrent_index :users, :id, name: BILLABLE_INDEX,
      where: "state = 'active' AND ((user_type IS NULL OR user_type = 0) OR (user_type = ANY (ARRAY[0, 6, 4, 13]))) " \
             "AND ((user_type IS NULL OR user_type = 0) OR (user_type = ANY (ARRAY[0, 4, 5])))"

    remove_concurrent_index_by_name :users, INCORRECT_BILLABLE_INDEX
  end

  def down
    add_concurrent_index :users, :id, name: INCORRECT_BILLABLE_INDEX,
      where: "state = 'active' AND ((user_type IS NULL OR user_type = 0) OR (user_type = ANY (ARRAY[6, 4, 13]))) " \
             "AND ((user_type IS NULL OR user_type = 0) OR (user_type = ANY (ARRAY[4, 5])))"
    remove_concurrent_index_by_name :users, BILLABLE_INDEX
  end
end