18004: Fixes a couple of race condition bugs related to caching remote users.
[arvados.git] / services / api / db / migrate / 20161222153434_split_expiry_to_trash_and_delete.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class SplitExpiryToTrashAndDelete < ActiveRecord::Migration[4.2]
6   def up
7     Collection.transaction do
8       add_column(:collections, :trash_at, :datetime)
9       add_index(:collections, :trash_at)
10       add_column(:collections, :is_trashed, :boolean, null: false, default: false)
11       add_index(:collections, :is_trashed)
12       rename_column(:collections, :expires_at, :delete_at)
13       add_index(:collections, :delete_at)
14
15       Collection.reset_column_information
16       Collection.
17         where('delete_at is not null and delete_at <= statement_timestamp()').
18         delete_all
19       Collection.
20         where('delete_at is not null').
21         update_all('is_trashed = true, trash_at = statement_timestamp()')
22       add_index(:collections, [:owner_uuid, :name],
23                 unique: true,
24                 where: 'is_trashed = false',
25                 name: 'index_collections_on_owner_uuid_and_name')
26       remove_index(:collections,
27                    name: 'collection_owner_uuid_name_unique')
28     end
29   end
30
31   def down
32     Collection.transaction do
33       remove_index(:collections, :delete_at)
34       rename_column(:collections, :delete_at, :expires_at)
35       add_index(:collections, [:owner_uuid, :name],
36                 unique: true,
37                 where: 'expires_at is null',
38                 name: 'collection_owner_uuid_name_unique')
39       remove_index(:collections,
40                    name: 'index_collections_on_owner_uuid_and_name')
41       remove_column(:collections, :is_trashed)
42       remove_index(:collections, :trash_at)
43       remove_column(:collections, :trash_at)
44     end
45   end
46 end