Merge branch '8784-dir-listings'
[arvados.git] / services / api / db / migrate / 20150216193428_collection_name_owner_unique_only_non_expired.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class CollectionNameOwnerUniqueOnlyNonExpired < ActiveRecord::Migration
6   def find_index
7     indexes = ActiveRecord::Base.connection.indexes('collections')
8     name_owner_index = indexes.select do |index|
9       index.name == 'collection_owner_uuid_name_unique'
10     end
11     name_owner_index
12   end
13
14   def up
15     remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
16     add_index(:collections, [:owner_uuid, :name], unique: true,
17               where: 'expires_at is null',
18               name: 'collection_owner_uuid_name_unique')
19   end
20
21   def down
22     # it failed during up. is it going to pass now? should we do nothing?
23     remove_index :collections, :name => 'collection_owner_uuid_name_unique' if !find_index.empty?
24     add_index(:collections, [:owner_uuid, :name], unique: true,
25               name: 'collection_owner_uuid_name_unique')
26   end
27 end