4523: add owner_uuid index on all tables that support this column.
[arvados.git] / services / api / db / migrate / 20141208164553_owner_uuid_index.rb
1 class OwnerUuidIndex < ActiveRecord::Migration
2   def tables_with_owner_uuid
3     all_tables = ActiveRecord::Base.connection.tables
4     my_tables = []
5     all_tables.each do |table|
6       columns = ActiveRecord::Base.connection.columns(table)
7       uuid_column = columns.select do |column|
8         column.name == 'owner_uuid'
9       end
10       my_tables << table if !uuid_column.empty?
11     end
12     my_tables
13   end
14
15   def up
16     tables_with_owner_uuid.each do |table|
17       add_index table.to_sym, :owner_uuid
18     end
19   end
20
21   def down
22     tables_with_owner_uuid.each do |table|
23       remove_index table.to_sym, :owner_uuid
24     end
25   end
26 end