5197: updated migration script
authorRadhika Chippada <radhika@curoverse.com>
Mon, 16 Feb 2015 20:31:30 +0000 (15:31 -0500)
committerRadhika Chippada <radhika@curoverse.com>
Mon, 16 Feb 2015 20:31:30 +0000 (15:31 -0500)
services/api/db/migrate/20150216193428_collection_name_owner_unique_only_non_expired.rb

index 1124d5efb3ad3eb79b0600bfdfc6c4c17368d2c8..92856882d4cb3ecba2b5620b8232a0294fefbb91 100644 (file)
@@ -1,6 +1,14 @@
 class CollectionNameOwnerUniqueOnlyNonExpired < ActiveRecord::Migration
+  def find_index
+    indexes = ActiveRecord::Base.connection.indexes('collections')
+    name_owner_index = indexes.select do |index|
+      index.name == 'collection_owner_uuid_name_unique'
+    end
+    name_owner_index
+  end
+
   def up
-    remove_index :collections, :name => 'collection_owner_uuid_name_unique'
+    remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
     add_index(:collections, [:owner_uuid, :name], unique: true,
               where: 'expires_at is null',
               name: 'collection_owner_uuid_name_unique')
@@ -8,8 +16,9 @@ class CollectionNameOwnerUniqueOnlyNonExpired < ActiveRecord::Migration
 
   def down
     # it failed during up. is it going to pass now? should we do nothing?
-    remove_index :collections, :name => 'collection_owner_uuid_name_unique'
-    add_index(:collections, [:owner_uuid, :name], unique: true,
-              name: 'collection_owner_uuid_name_unique')
+    remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
+    # and this is failing ...
+    # add_index(:collections, [:owner_uuid, :name], unique: true,
+    #          name: 'collection_owner_uuid_name_unique')
   end
 end