4523: Dry up migration and test cases.
authorTom Clegg <tom@curoverse.com>
Mon, 29 Dec 2014 18:58:58 +0000 (13:58 -0500)
committerTom Clegg <tom@curoverse.com>
Mon, 29 Dec 2014 18:58:58 +0000 (13:58 -0500)
services/api/db/migrate/20141208164553_owner_uuid_index.rb
services/api/test/unit/arvados_model_test.rb

index 9ba0c5a3499bb67d0c2b618d1742e4958a671e2a..0859d4681801ac3acbe427f2cbc6b1a1263e3091 100644 (file)
@@ -1,15 +1,9 @@
 class OwnerUuidIndex < ActiveRecord::Migration
   def tables_with_owner_uuid
-    all_tables = ActiveRecord::Base.connection.tables
-    my_tables = []
-    all_tables.each do |table|
+    ActiveRecord::Base.connection.tables.select do |table|
       columns = ActiveRecord::Base.connection.columns(table)
-      uuid_column = columns.select do |column|
-        column.name == 'owner_uuid'
-      end
-      my_tables << table if !uuid_column.empty?
+      columns.collect(&:name).include? 'owner_uuid'
     end
-    my_tables
   end
 
   def up
index be78c8394a71f4b5ba90a31cfc68156d709f4499..8c7576478912199371360b43154e3318480f49dc 100644 (file)
@@ -87,43 +87,26 @@ class ArvadosModelTest < ActiveSupport::TestCase
     end
   end
 
-  test "unique uuid index exists on all models with the column uuid" do
-    tables = ActiveRecord::Base.connection.tables
-    tables.each do |table|
-      columns = ActiveRecord::Base.connection.columns(table)
-
-      uuid_column = columns.select do |column|
-        column.name == 'uuid'
-      end
-
-      if !uuid_column.empty?
-        indexes = ActiveRecord::Base.connection.indexes(table)
-        uuid_index = indexes.select do |index|
-          index.columns == ['uuid'] and index.unique == true
-        end
-
-        assert !uuid_index.empty?, "#{table} does not have unique uuid index"
-      end
-    end
-  end
-
-  test "owner uuid index exists on all models with the owner_uuid column" do
-    all_tables = ActiveRecord::Base.connection.tables
-
-    all_tables.each do |table|
-      columns = ActiveRecord::Base.connection.columns(table)
-
-      uuid_column = columns.select do |column|
-        column.name == 'owner_uuid'
-      end
-
-      if !uuid_column.empty?
-        indexes = ActiveRecord::Base.connection.indexes(table)
-        owner_uuid_index = indexes.select do |index|
-          index.columns == ['owner_uuid']
+  [['uuid', {unique: true}],
+   ['owner_uuid', {}]].each do |the_column, requires|
+    test "unique index on all models with #{the_column}" do
+      checked = 0
+      ActiveRecord::Base.connection.tables.each do |table|
+        columns = ActiveRecord::Base.connection.columns(table)
+
+        next unless columns.collect(&:name).include? the_column
+
+        indexes = ActiveRecord::Base.connection.indexes(table).reject do |index|
+          requires.map do |key, val|
+            index.send(key) == val
+          end.include? false
         end
-        assert !owner_uuid_index.empty?, "#{table} does not have owner_uuid index"
+        assert_includes indexes.collect(&:columns), [the_column], 'no index'
+        checked += 1
       end
+      # Sanity check: make sure we didn't just systematically miss everything.
+      assert_operator(10, :<, checked,
+                      "Only #{checked} tables have a #{the_column}?!")
     end
   end
 end