Merge commit '3b735dd9330e0989f51a76771c3303031154154e' into 21158-wf-page-list
[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[4.2]
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 replace_index(t)
19     i = fts_indexes[t]
20     t.classify.constantize.reset_column_information
21     execute "DROP INDEX IF EXISTS #{i}"
22     execute "CREATE INDEX #{i} ON #{t} USING gin(#{t.classify.constantize.full_text_tsvector})"
23   end
24
25   def up
26     # remove existing fts indexes and create up to date ones with no
27     # leading space
28     fts_indexes.keys.each do |t|
29       replace_index(t)
30     end
31   end
32
33   def down
34     fts_indexes.each do |t, i|
35       remove_index t.to_sym, :name => i
36     end
37   end
38 end