1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class CollectionUseRegularUuids < ActiveRecord::Migration[4.2]
7 add_column :collections, :name, :string
8 add_column :collections, :description, :string
9 add_column :collections, :properties, :text
10 add_column :collections, :expires_at, :date
11 remove_column :collections, :locator
13 say_with_time "Step 1. Move manifest hashes into portable_data_hash field" do
14 ActiveRecord::Base.connection.execute("update collections set portable_data_hash=uuid, uuid=null")
17 say_with_time "Step 2. Create new collection objects from the name links in the table." do
19 from links inner join collections on head_uuid=collections.portable_data_hash
20 where link_class='name' and collections.uuid is null
22 links = ActiveRecord::Base.connection.select_all %{
23 select links.uuid, head_uuid, tail_uuid, links.name,
24 manifest_text, links.created_at, links.modified_at, links.modified_by_client_uuid, links.modified_by_user_uuid
28 ActiveRecord::Base.connection.execute %{
29 insert into collections (uuid, portable_data_hash, owner_uuid, name, manifest_text, created_at, modified_at, modified_by_client_uuid, modified_by_user_uuid, updated_at)
30 values (#{ActiveRecord::Base.connection.quote Collection.generate_uuid},
31 #{ActiveRecord::Base.connection.quote d['head_uuid']},
32 #{ActiveRecord::Base.connection.quote d['tail_uuid']},
33 #{ActiveRecord::Base.connection.quote d['name']},
34 #{ActiveRecord::Base.connection.quote d['manifest_text']},
35 #{ActiveRecord::Base.connection.quote d['created_at']},
36 #{ActiveRecord::Base.connection.quote d['modified_at']},
37 #{ActiveRecord::Base.connection.quote d['modified_by_client_uuid']},
38 #{ActiveRecord::Base.connection.quote d['modified_by_user_uuid']},
39 #{ActiveRecord::Base.connection.quote d['modified_at']})
42 ActiveRecord::Base.connection.execute "delete from links where links.uuid in (select links.uuid #{from_clause})"
45 say_with_time "Step 3. Create new collection objects from the can_read links in the table." do
47 from links inner join collections on head_uuid=collections.portable_data_hash
48 where link_class='permission' and links.name='can_read' and collections.uuid is null
50 links = ActiveRecord::Base.connection.select_all %{
51 select links.uuid, head_uuid, tail_uuid, manifest_text, links.created_at, links.modified_at
55 ActiveRecord::Base.connection.execute %{
56 insert into collections (uuid, portable_data_hash, owner_uuid, manifest_text, created_at, modified_at, modified_by_client_uuid, modified_by_user_uuid, updated_at)
57 values (#{ActiveRecord::Base.connection.quote Collection.generate_uuid},
58 #{ActiveRecord::Base.connection.quote d['head_uuid']},
59 #{ActiveRecord::Base.connection.quote d['tail_uuid']},
60 #{ActiveRecord::Base.connection.quote d['manifest_text']},
61 #{ActiveRecord::Base.connection.quote d['created_at']},
62 #{ActiveRecord::Base.connection.quote d['modified_at']},
63 #{ActiveRecord::Base.connection.quote d['modified_by_client_uuid']},
64 #{ActiveRecord::Base.connection.quote d['modified_by_user_uuid']},
65 #{ActiveRecord::Base.connection.quote d['modified_at']})
68 ActiveRecord::Base.connection.execute "delete from links where links.uuid in (select links.uuid #{from_clause})"
71 say_with_time "Step 4. Migrate remaining orphan collection objects" do
72 links = ActiveRecord::Base.connection.select_all %{
73 select portable_data_hash, owner_uuid, manifest_text, created_at, modified_at
75 where uuid is null and portable_data_hash not in (select portable_data_hash from collections where uuid is not null)
78 ActiveRecord::Base.connection.execute %{
79 insert into collections (uuid, portable_data_hash, owner_uuid, manifest_text, created_at, modified_at, modified_by_client_uuid, modified_by_user_uuid, updated_at)
80 values (#{ActiveRecord::Base.connection.quote Collection.generate_uuid},
81 #{ActiveRecord::Base.connection.quote d['portable_data_hash']},
82 #{ActiveRecord::Base.connection.quote d['owner_uuid']},
83 #{ActiveRecord::Base.connection.quote d['manifest_text']},
84 #{ActiveRecord::Base.connection.quote d['created_at']},
85 #{ActiveRecord::Base.connection.quote d['modified_at']},
86 #{ActiveRecord::Base.connection.quote d['modified_by_client_uuid']},
87 #{ActiveRecord::Base.connection.quote d['modified_by_user_uuid']},
88 #{ActiveRecord::Base.connection.quote d['modified_at']})
93 say_with_time "Step 5. Delete old collection objects." do
94 ActiveRecord::Base.connection.execute("delete from collections where uuid is null")
97 say_with_time "Step 6. Delete permission links where tail_uuid is a collection (invalid records)" do
98 ActiveRecord::Base.connection.execute %{
99 delete from links where links.uuid in (select links.uuid
101 where tail_uuid like '________________________________+%' and link_class='permission' )
105 say_with_time "Step 7. Migrate collection -> collection provenance links to jobs" do
108 where head_uuid like '________________________________+%' and tail_uuid like '________________________________+%' and links.link_class = 'provenance'
110 links = ActiveRecord::Base.connection.select_all %{
111 select links.uuid, head_uuid, tail_uuid, links.created_at, links.modified_at, links.modified_by_client_uuid, links.modified_by_user_uuid, links.owner_uuid
115 newuuid = Job.generate_uuid
116 ActiveRecord::Base.connection.execute %{
117 insert into jobs (uuid, script_parameters, output, running, success, created_at, modified_at, modified_by_client_uuid, modified_by_user_uuid, owner_uuid, updated_at)
118 values (#{ActiveRecord::Base.connection.quote newuuid},
119 #{ActiveRecord::Base.connection.quote "---\ninput: "+d['tail_uuid']},
120 #{ActiveRecord::Base.connection.quote d['head_uuid']},
121 #{ActiveRecord::Base.connection.quote false},
122 #{ActiveRecord::Base.connection.quote true},
123 #{ActiveRecord::Base.connection.quote d['created_at']},
124 #{ActiveRecord::Base.connection.quote d['modified_at']},
125 #{ActiveRecord::Base.connection.quote d['modified_by_client_uuid']},
126 #{ActiveRecord::Base.connection.quote d['modified_by_user_uuid']},
127 #{ActiveRecord::Base.connection.quote d['owner_uuid']},
128 #{ActiveRecord::Base.connection.quote d['modified_at']})
131 ActiveRecord::Base.connection.execute "delete from links where links.uuid in (select links.uuid #{from_clause})"
134 say_with_time "Step 8. Migrate remaining links with head_uuid pointing to collections" do
136 from links inner join collections on links.head_uuid=portable_data_hash
137 where collections.uuid is not null
139 links = ActiveRecord::Base.connection.select_all %{
140 select links.uuid, collections.uuid as collectionuuid, tail_uuid, link_class, links.properties,
141 links.name, links.created_at, links.modified_at, links.modified_by_client_uuid, links.modified_by_user_uuid, links.owner_uuid
145 ActiveRecord::Base.connection.execute %{
146 insert into links (uuid, head_uuid, tail_uuid, link_class, name, properties, created_at, modified_at, modified_by_client_uuid, modified_by_user_uuid, owner_uuid, updated_at)
147 values (#{ActiveRecord::Base.connection.quote Link.generate_uuid},
148 #{ActiveRecord::Base.connection.quote d['collectionuuid']},
149 #{ActiveRecord::Base.connection.quote d['tail_uuid']},
150 #{ActiveRecord::Base.connection.quote d['link_class']},
151 #{ActiveRecord::Base.connection.quote d['name']},
152 #{ActiveRecord::Base.connection.quote d['properties']},
153 #{ActiveRecord::Base.connection.quote d['created_at']},
154 #{ActiveRecord::Base.connection.quote d['modified_at']},
155 #{ActiveRecord::Base.connection.quote d['modified_by_client_uuid']},
156 #{ActiveRecord::Base.connection.quote d['modified_by_user_uuid']},
157 #{ActiveRecord::Base.connection.quote d['owner_uuid']},
158 #{ActiveRecord::Base.connection.quote d['modified_at']})
161 ActiveRecord::Base.connection.execute "delete from links where links.uuid in (select links.uuid #{from_clause})"
164 say_with_time "Step 9. Delete any remaining name links" do
165 ActiveRecord::Base.connection.execute("delete from links where link_class='name'")
168 say_with_time "Step 10. Validate links table" do
169 links = ActiveRecord::Base.connection.select_all %{
170 select links.uuid, head_uuid, tail_uuid, link_class, name
172 where head_uuid like '________________________________+%' or tail_uuid like '________________________________+%'
182 raise ActiveRecord::IrreversibleMigration, "Can't downmigrate changes to collections and links without potentially losing data."