about summary refs log tree commit diff
path: root/db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb
blob: a3cc854d7c0595892ab0db9a3145fcfd84e9300d (plain) (blame)
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
require Rails.root.join('lib', 'mastodon', 'migration_helpers')

class AddCaseInsensitiveBtreeIndexToTags < ActiveRecord::Migration[5.2]
  include Mastodon::MigrationHelpers

  disable_ddl_transaction!

  def up
    begin
      safety_assured { execute 'CREATE UNIQUE INDEX CONCURRENTLY index_tags_on_name_lower_btree ON tags (lower(name) text_pattern_ops)' }
    rescue ActiveRecord::StatementInvalid => e
      remove_index :tags, name: 'index_tags_on_name_lower_btree'
      raise CorruptionError, 'index_tags_on_name_lower_btree' if e.is_a?(ActiveRecord::RecordNotUnique)

      raise e
    end

    remove_index :tags, name: 'index_tags_on_name_lower'
  end

  def down
    safety_assured { execute 'CREATE UNIQUE INDEX CONCURRENTLY index_tags_on_name_lower ON tags (lower(name))' }
    remove_index :tags, name: 'index_tags_on_name_lower_btree'
  end
end