Merge branch '8784-dir-listings'
[arvados.git] / services / api / db / migrate / 20150526180251_leading_space_on_full_text_index.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require "./db/migrate/20150123142953_full_text_search.rb"
6
7 class LeadingSpaceOnFullTextIndex < ActiveRecord::Migration
8   def up
9     # Inspect one of the full-text indexes (chosen arbitrarily) to
10     # determine whether this migration is needed.
11     ft_index_name = 'jobs_full_text_search_idx'
12     ActiveRecord::Base.connection.indexes('jobs').each do |idx|
13       if idx.name == ft_index_name
14         if idx.columns.first.index "((((' '"
15           # Index is already correct. This happens if the source tree
16           # already had the new version of full_text_tsvector by the
17           # time the initial FullTextSearch migration ran.
18           $stderr.puts "This migration is not needed."
19         else
20           # Index was created using the old full_text_tsvector. Drop
21           # and re-create all full text indexes.
22           FullTextSearch.new.migrate(:down)
23           FullTextSearch.new.migrate(:up)
24         end
25         return
26       end
27     end
28     raise "Did not find index '#{ft_index_name}'. Earlier migration missed??"
29   end
30
31   def down
32     $stderr.puts <<EOS
33 Down-migration is not supported for this change, and might be unnecessary.
34
35 If you run a code base older than 20150526180251 against this
36 database, full text search will be slow even on collections where it
37 used to work well. If this is a concern, first check out the desired
38 older version of the code base, and then run
39 "rake db:migrate:down VERSION=20150123142953"
40 followed by
41 "rake db:migrate:up VERSION=20150123142953"
42 .
43 EOS
44   end
45 end