Merge branch '11100-cr-output-ttl'
[arvados.git] / services / api / db / migrate / 20161222153434_split_expiry_to_trash_and_delete.rb
1 class SplitExpiryToTrashAndDelete < ActiveRecord::Migration
2   def up
3     Collection.transaction do
4       add_column(:collections, :trash_at, :datetime)
5       add_index(:collections, :trash_at)
6       add_column(:collections, :is_trashed, :boolean, null: false, default: false)
7       add_index(:collections, :is_trashed)
8       rename_column(:collections, :expires_at, :delete_at)
9       add_index(:collections, :delete_at)
10
11       Collection.reset_column_information
12       Collection.
13         where('delete_at is not null and delete_at <= statement_timestamp()').
14         delete_all
15       Collection.
16         where('delete_at is not null').
17         update_all('is_trashed = true, trash_at = statement_timestamp()')
18       add_index(:collections, [:owner_uuid, :name],
19                 unique: true,
20                 where: 'is_trashed = false',
21                 name: 'index_collections_on_owner_uuid_and_name')
22       remove_index(:collections,
23                    name: 'collection_owner_uuid_name_unique')
24     end
25   end
26
27   def down
28     Collection.transaction do
29       remove_index(:collections, :delete_at)
30       rename_column(:collections, :delete_at, :expires_at)
31       add_index(:collections, [:owner_uuid, :name],
32                 unique: true,
33                 where: 'expires_at is null',
34                 name: 'collection_owner_uuid_name_unique')
35       remove_index(:collections,
36                    name: 'index_collections_on_owner_uuid_and_name')
37       remove_column(:collections, :is_trashed)
38       remove_index(:collections, :trash_at)
39       remove_column(:collections, :trash_at)
40     end
41   end
42 end