1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class DedupPermissionLinks < ActiveRecord::Migration[5.2]
6 include CurrentApiClient
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
18 Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
19 link = Link.find_by_uuid(row['uuid'])
20 # This no-op update has the side effect that the update hooks
21 # will merge the highest available permission into this one
22 # and then delete the others.
23 link.update_attributes!(properties: link.properties.dup)
26 rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
27 WHERE tail_uuid IS NOT NULL
28 AND head_uuid IS NOT NULL
29 AND link_class = 'permission'
30 AND name = 'can_login'
31 GROUP BY (tail_uuid, head_uuid, properties)
32 HAVING COUNT(uuid) > 1
35 Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
36 link = Link.find_by_uuid(row['uuid'])
37 link.update_attributes!(properties: link.properties.dup)
42 # no-op -- restoring redundant records would still be redundant