20334: Batch permission updates during link dedup migration. 20334-slow-dedup-migration
authorTom Clegg <tom@curii.com>
Thu, 13 Apr 2023 15:47:46 +0000 (11:47 -0400)
committerTom Clegg <tom@curii.com>
Thu, 13 Apr 2023 15:47:46 +0000 (11:47 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

services/api/db/migrate/20221219165512_dedup_permission_links.rb

index ec9ea591d45c22d23e730fabe40db79519d757cf..6ae04cc954538298dcca52780d2a7e1cbca0644d 100644 (file)
@@ -2,37 +2,41 @@
 #
 # SPDX-License-Identifier: AGPL-3.0
 
+require 'update_permissions'
+
 class DedupPermissionLinks < ActiveRecord::Migration[5.2]
   include CurrentApiClient
   def up
     act_as_system_user do
-      rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
-        WHERE tail_uuid IS NOT NULL
-         AND head_uuid IS NOT NULL
-         AND link_class = 'permission'
-         AND name in ('can_read', 'can_write', 'can_manage')
-        GROUP BY (tail_uuid, head_uuid)
-        HAVING COUNT(uuid) > 1")
-      rows.each do |row|
-        Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
-        link = Link.find_by_uuid(row['uuid'])
-        # This no-op update has the side effect that the update hooks
-        # will merge the highest available permission into this one
-        # and then delete the others.
-        link.update_attributes!(properties: link.properties.dup)
-      end
+      batch_update_permissions do
+        rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
+          WHERE tail_uuid IS NOT NULL
+           AND head_uuid IS NOT NULL
+           AND link_class = 'permission'
+           AND name in ('can_read', 'can_write', 'can_manage')
+          GROUP BY (tail_uuid, head_uuid)
+          HAVING COUNT(uuid) > 1")
+        rows.each do |row|
+          Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
+          link = Link.find_by_uuid(row['uuid'])
+          # This no-op update has the side effect that the update hooks
+          # will merge the highest available permission into this one
+          # and then delete the others.
+          link.update_attributes!(properties: link.properties.dup)
+        end
 
-      rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
-        WHERE tail_uuid IS NOT NULL
-         AND head_uuid IS NOT NULL
-         AND link_class = 'permission'
-         AND name = 'can_login'
-        GROUP BY (tail_uuid, head_uuid, properties)
-        HAVING COUNT(uuid) > 1")
-      rows.each do |row|
-        Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
-        link = Link.find_by_uuid(row['uuid'])
-        link.update_attributes!(properties: link.properties.dup)
+        rows = ActiveRecord::Base.connection.select_all("SELECT MIN(uuid) AS uuid, COUNT(uuid) AS n FROM links
+          WHERE tail_uuid IS NOT NULL
+           AND head_uuid IS NOT NULL
+           AND link_class = 'permission'
+           AND name = 'can_login'
+          GROUP BY (tail_uuid, head_uuid, properties)
+          HAVING COUNT(uuid) > 1")
+        rows.each do |row|
+          Rails.logger.debug "DedupPermissionLinks: consolidating #{row['n']} links into #{row['uuid']}"
+          link = Link.find_by_uuid(row['uuid'])
+          link.update_attributes!(properties: link.properties.dup)
+        end
       end
     end
   end