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")
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)
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")
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)
40 # no-op -- restoring redundant records would still be redundant