Merge branch 'master' into 15106-trgm-text-search
[arvados.git] / services / api / db / migrate / 20190523180148_add_trigram_index_for_text_search.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class AddTrigramIndexForTextSearch < ActiveRecord::Migration[5.0]
6   def trgm_indexes
7     {
8       "collections" => "collections_trgm_text_search_idx",
9       "container_requests" => "container_requests_trgm_text_search_idx",
10       "groups" => "groups_trgm_text_search_idx",
11       "jobs" => "jobs_trgm_text_search_idx",
12       "pipeline_instances" => "pipeline_instances_trgm_text_search_idx",
13       "pipeline_templates" => "pipeline_templates_trgm_text_search_idx",
14       "workflows" => "workflows_trgm_text_search_idx",
15     }
16   end
17
18   def up
19     trgm_indexes.each do |model, indx|
20       execute "CREATE INDEX #{indx} ON #{model} USING gin((#{model.classify.constantize.full_text_trgm}) gin_trgm_ops)"
21     end
22   end
23
24   def down
25     trgm_indexes.each do |_, indx|
26       execute "DROP INDEX IF EXISTS #{indx}"
27     end
28   end
29 end