20259: Add documentation for banner and tooltip features
[arvados.git] / services / api / db / migrate / 20221219165512_dedup_permission_links.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class DedupPermissionLinks < ActiveRecord::Migration[5.2]
6   include CurrentApiClient
7   def up
8     act_as_system_user do
9       rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
10         WHERE tail_uuid IS NOT NULL
11          AND head_uuid IS NOT NULL
12          AND link_class = 'permission'
13          AND name in ('can_read', 'can_write', 'can_manage')
14         GROUP BY (tail_uuid, head_uuid)
15         HAVING COUNT(uuid) > 1")
16       rows.each do |row|
17         Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
18         link = Link.find_by_uuid(row['uuid'])
19         # This no-op update has the side effect that the update hooks
20         # will merge the highest available permission into this one
21         # and then delete the others.
22         link.update_attributes!(properties: link.properties.dup)
23       end
24
25       rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
26         WHERE tail_uuid IS NOT NULL
27          AND head_uuid IS NOT NULL
28          AND link_class = 'permission'
29          AND name = 'can_login'
30         GROUP BY (tail_uuid, head_uuid, properties)
31         HAVING COUNT(uuid) > 1")
32       rows.each do |row|
33         Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
34         link = Link.find_by_uuid(row['uuid'])
35         link.update_attributes!(properties: link.properties.dup)
36       end
37     end
38   end
39   def down
40     # no-op -- restoring redundant records would still be redundant
41   end
42 end