1 class NormalizeCollectionUuid < ActiveRecord::Migration
3 %w(head tail).each do |ht|
4 results = ActiveRecord::Base.connection.execute(<<-EOS)
7 LEFT JOIN collections c
8 ON links.#{ht}_uuid = c.uuid
9 WHERE (#{ht}_kind='arvados#collection' or #{ht}_uuid ~ '^[0-9a-f]{32,}')
10 AND #{ht}_uuid IS NOT NULL
11 AND #{ht}_uuid NOT IN (SELECT uuid FROM collections)
13 puts "#{results.first['count'].to_i} links with #{ht}_uuid pointing nowhere."
18 # Normalize uuids in the collections table to
19 # {hash}+{size}. Existing uuids might be {hash},
20 # {hash}+{size}+K@{instance-name}, {hash}+K@{instance-name}, etc.
23 puts "Normalizing collection UUIDs."
27 SET uuid = regexp_replace(uuid,'\\+.*','') || '+' || length(manifest_text)
28 WHERE uuid !~ '^[0-9a-f]{32,}\\+[0-9]+$'
29 AND (regexp_replace(uuid,'\\+.*','') || '+' || length(manifest_text))
30 NOT IN (SELECT uuid FROM collections)
34 puts "Updating links by stripping +K@.* from *_uuid attributes."
38 SET head_uuid = regexp_replace(head_uuid,'\\+K@.*','')
39 WHERE head_uuid like '%+K@%'
43 SET tail_uuid = regexp_replace(tail_uuid,'\\+K@.*','')
44 WHERE tail_uuid like '%+K@%'
48 puts "Updating links by searching bare collection hashes using regexp."
50 # Next, update {hash} (and any other non-normalized forms) to
51 # {hash}+{size}. This can only work where the corresponding
52 # collection is found in the collections table (otherwise we can't
54 %w(head tail).each do |ht|
57 SET #{ht}_uuid = c.uuid
59 WHERE #{ht}_uuid IS NOT NULL
60 AND (#{ht}_kind='arvados#collection' or #{ht}_uuid ~ '^[0-9a-f]{32,}')
61 AND #{ht}_uuid NOT IN (SELECT uuid FROM collections)
62 AND regexp_replace(#{ht}_uuid,'\\+.*','') = regexp_replace(c.uuid,'\\+.*','')
63 AND c.uuid ~ '^[0-9a-f]{32,}\\+[0-9]+$'
68 puts "Stripping \"+K@.*\" from jobs.output, jobs.log, job_tasks.output."
72 SET output = regexp_replace(output,'\\+K@.*','')
73 WHERE output ~ '^[0-9a-f]{32,}\\+[0-9]+\\+K@\\w+$'
77 SET log = regexp_replace(log,'\\+K@.*','')
78 WHERE log ~ '^[0-9a-f]{32,}\\+[0-9]+\\+K@\\w+$'
82 SET output = regexp_replace(output,'\\+K@.*','')
83 WHERE output ~ '^[0-9a-f]{32,}\\+[0-9]+\\+K@\\w+$'