Skip to content

Commit

Permalink
Adds data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Aug 8, 2024
1 parent d71efd7 commit 28a2061
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db/migrate/20240806144333_create_remote_identities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
Expand Down
55 changes: 55 additions & 0 deletions db/migrate/20240808125617_populate_remote_identities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

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

OAuthClientToken.update_all(origin_user_id: nil)
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

RemoteIdentity.delete_all
end
end

0 comments on commit 28a2061

Please sign in to comment.