Merge branch '8784-dir-listings'
[arvados.git] / services / api / db / migrate / 20161213172944_full_text_search_indexes.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class FullTextSearchIndexes < ActiveRecord::Migration
6   def fts_indexes
7     {
8       "collections" => "collections_full_text_search_idx",
9       "container_requests" => "container_requests_full_text_search_idx",
10       "groups" => "groups_full_text_search_idx",
11       "jobs" => "jobs_full_text_search_idx",
12       "pipeline_instances" => "pipeline_instances_full_text_search_idx",
13       "pipeline_templates" => "pipeline_templates_full_text_search_idx",
14       "workflows" => "workflows_full_text_search_idx",
15     }
16   end
17
18   def up
19     # remove existing fts indexes and create up to date ones with no leading space
20     fts_indexes.each do |t, i|
21       t.classify.constantize.reset_column_information
22       ActiveRecord::Base.connection.indexes(t).each do |idx|
23         if idx.name == i
24           remove_index t.to_sym, :name => i
25           break
26         end
27       end
28       execute "CREATE INDEX #{i} ON #{t} USING gin(#{t.classify.constantize.full_text_tsvector});"
29     end
30   end
31
32   def down
33     fts_indexes.each do |t, i|
34       remove_index t.to_sym, :name => i
35     end
36   end
37 end