From 0c7ad61f67fd593726af3f996b86e5eea3642c34 Mon Sep 17 00:00:00 2001 From: Marcello Rocha Date: Mon, 12 Aug 2024 10:53:35 +0200 Subject: [PATCH 1/2] Update migration to use raw SQL rather than AR models --- ...240808125617_populate_remote_identities.rb | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/db/migrate/20240808125617_populate_remote_identities.rb b/db/migrate/20240808125617_populate_remote_identities.rb index 96639176f670..9ae7b25bdd31 100644 --- a/db/migrate/20240808125617_populate_remote_identities.rb +++ b/db/migrate/20240808125617_populate_remote_identities.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + #-- copyright # OpenProject is an open source project management software. # Copyright (C) 2012-2024 the OpenProject GmbH @@ -28,28 +30,25 @@ class PopulateRemoteIdentities < ActiveRecord::Migration[7.1] def up - fields = %i[user_id oauth_client_id origin_user_id] - - OAuthClientToken.where.not(origin_user_id: nil) - .select(:id, *fields) - .find_in_batches do |batch| - identities = batch.map { |record| record.slice(*fields) } - - RemoteIdentity.insert_all(identities, unique_by: %i[user_id oauth_client_id]) - end + execute_sql <<~SQL.squish + INSERT INTO remote_identities(user_id, oauth_client_id, origin_user_id, created_at, updated_at) + SELECT DISTINCT ON (user_id, oauth_client_id) user_id, oauth_client_id, origin_user_id, created_at, updated_at + FROM oauth_client_tokens + WHERE oauth_client_tokens.origin_user_id IS NOT NULL + SQL - OAuthClientToken.update_all(origin_user_id: nil) + execute_sql "UPDATE oauth_client_tokens SET origin_user_id = NULL" end def down - RemoteIdentity.find_in_batches do |batch| - batch.each do |identity| - OAuthClientToken - .where(user: identity.user, oauth_client: identity.oauth_client) - .update_all(origin_user_id: identity.origin_user_id) - end - end + execute_sql <<~SQL.squish + UPDATE oauth_client_tokens + SET origin_user_id = remote_identities.origin_user_id + FROM remote_identities + WHERE oauth_client_tokens.user_id = remote_identities.user_id + AND oauth_client_tokens.oauth_client_id = remote_identities.oauth_client_id + SQL - RemoteIdentity.delete_all + execute_sql "DELETE FROM remote_identities" end end From fa15643b99a6098cdc6b8a6cec61a7e665d57d9d Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Mon, 12 Aug 2024 10:11:32 +0100 Subject: [PATCH 2/2] add migration utils include --- db/migrate/20240808125617_populate_remote_identities.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20240808125617_populate_remote_identities.rb b/db/migrate/20240808125617_populate_remote_identities.rb index 9ae7b25bdd31..f526f1e084b4 100644 --- a/db/migrate/20240808125617_populate_remote_identities.rb +++ b/db/migrate/20240808125617_populate_remote_identities.rb @@ -28,7 +28,11 @@ # See COPYRIGHT and LICENSE files for more details. #++ +require_relative "migration_utils/utils" + class PopulateRemoteIdentities < ActiveRecord::Migration[7.1] + include ::Migration::Utils + def up execute_sql <<~SQL.squish INSERT INTO remote_identities(user_id, oauth_client_id, origin_user_id, created_at, updated_at)