Merge branch '4253-user-repos-wip'
[arvados.git] / services / api / db / migrate / 20141208164553_owner_uuid_index.rb
1 class OwnerUuidIndex < ActiveRecord::Migration
2   def tables_with_owner_uuid
3     %w{api_clients authorized_keys collections groups humans
4        job_tasks jobs keep_disks keep_services links logs
5        nodes pipeline_instances pipeline_templates repositories
6        specimens traits users virtual_machines}
7   end
8
9   def up
10     tables_with_owner_uuid.each do |table|
11       add_index table.to_sym, :owner_uuid
12     end
13   end
14
15   def down
16     tables_with_owner_uuid.each do |table|
17       indexes = ActiveRecord::Base.connection.indexes(table)
18       owner_uuid_index = indexes.select do |index|
19         index.columns == ['owner_uuid']
20       end
21       if !owner_uuid_index.empty?
22         remove_index table.to_sym, :owner_uuid
23       end
24     end
25   end
26 end