Add 'sdk/java-v2/' from commit '55f103e336ca9fb8bf1720d2ef4ee8dd4e221118'
[arvados.git] / services / api / db / migrate / 20180607175050_properties_to_jsonb.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require './db/migrate/20161213172944_full_text_search_indexes'
6
7 class PropertiesToJsonb < ActiveRecord::Migration
8
9   @@tables_columns = [["nodes", "properties"],
10                       ["nodes", "info"],
11                       ["container_requests", "properties"],
12                       ["links", "properties"]]
13
14   def up
15     @@tables_columns.each do |table, column|
16       # Drop the FT index before changing column type to avoid
17       # "PG::DatatypeMismatch: ERROR: COALESCE types jsonb and text
18       # cannot be matched".
19       ActiveRecord::Base.connection.execute "DROP INDEX IF EXISTS #{table}_full_text_search_idx"
20       ActiveRecord::Base.connection.execute "ALTER TABLE #{table} ALTER COLUMN #{column} TYPE jsonb USING #{column}::jsonb"
21       ActiveRecord::Base.connection.execute "CREATE INDEX #{table}_index_on_#{column} ON #{table} USING gin (#{column})"
22     end
23     FullTextSearchIndexes.new.replace_index("container_requests")
24   end
25
26   def down
27     @@tables_columns.each do |table, column|
28       ActiveRecord::Base.connection.execute "DROP INDEX IF EXISTS #{table}_index_on_#{column}"
29       ActiveRecord::Base.connection.execute "ALTER TABLE #{table} ALTER COLUMN #{column} TYPE text"
30     end
31   end
32 end