Merge branch 'master' into 5720-ajax-loading-error
[arvados.git] / services / api / db / migrate / 20140519205916_create_keep_services.rb
1 class CreateKeepServices < ActiveRecord::Migration
2   include CurrentApiClient
3
4   def change
5     act_as_system_user do
6       create_table :keep_services do |t|
7         t.string :uuid, :null => false
8         t.string :owner_uuid, :null => false
9         t.string :modified_by_client_uuid
10         t.string :modified_by_user_uuid
11         t.datetime :modified_at
12         t.string   :service_host
13         t.integer  :service_port
14         t.boolean  :service_ssl_flag
15         t.string   :service_type
16
17         t.timestamps
18       end
19       add_index :keep_services, :uuid, :unique => true
20
21       add_column :keep_disks, :keep_service_uuid, :string
22
23       KeepDisk.reset_column_information
24
25       services = {}
26
27       KeepDisk.find_each do |k|
28         services["#{k[:service_host]}_#{k[:service_port]}_#{k[:service_ssl_flag]}"] = {
29           service_host: k[:service_host],
30           service_port: k[:service_port],
31           service_ssl_flag: k[:service_ssl_flag],
32           service_type: 'disk',
33           owner_uuid: k[:owner_uuid]
34         }
35       end
36
37       services.each do |k, v|
38         v['uuid'] = KeepService.create(v).uuid
39       end
40
41       KeepDisk.find_each do |k|
42         k.keep_service_uuid = services["#{k[:service_host]}_#{k[:service_port]}_#{k[:service_ssl_flag]}"]['uuid']
43         k.save
44       end
45
46       remove_column :keep_disks, :service_host
47       remove_column :keep_disks, :service_port
48       remove_column :keep_disks, :service_ssl_flag
49     end
50   end
51 end