4019: Add index for efficient jsonb query on collection properties
[arvados.git] / services / api / db / migrate / 20140519205916_create_keep_services.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class CreateKeepServices < ActiveRecord::Migration
6   include CurrentApiClient
7
8   def change
9     act_as_system_user do
10       create_table :keep_services do |t|
11         t.string :uuid, :null => false
12         t.string :owner_uuid, :null => false
13         t.string :modified_by_client_uuid
14         t.string :modified_by_user_uuid
15         t.datetime :modified_at
16         t.string   :service_host
17         t.integer  :service_port
18         t.boolean  :service_ssl_flag
19         t.string   :service_type
20
21         t.timestamps
22       end
23       add_index :keep_services, :uuid, :unique => true
24
25       add_column :keep_disks, :keep_service_uuid, :string
26
27       KeepDisk.reset_column_information
28
29       services = {}
30
31       KeepDisk.find_each do |k|
32         services["#{k[:service_host]}_#{k[:service_port]}_#{k[:service_ssl_flag]}"] = {
33           service_host: k[:service_host],
34           service_port: k[:service_port],
35           service_ssl_flag: k[:service_ssl_flag],
36           service_type: 'disk',
37           owner_uuid: k[:owner_uuid]
38         }
39       end
40
41       services.each do |k, v|
42         v['uuid'] = KeepService.create(v).uuid
43       end
44
45       KeepDisk.find_each do |k|
46         k.keep_service_uuid = services["#{k[:service_host]}_#{k[:service_port]}_#{k[:service_ssl_flag]}"]['uuid']
47         k.save
48       end
49
50       remove_column :keep_disks, :service_host
51       remove_column :keep_disks, :service_port
52       remove_column :keep_disks, :service_ssl_flag
53     end
54   end
55 end