Browse Source
While migrating users from the previous service to the new one we discovered that email addresses are not unique in the previous service. This means that one user in the new service might relate to multiple users in the previous service. This change: - Adds a new LegacyUser model that can belong to a User. Each LegacyUser model has one old_user_id that corresponds to the user ID in the legacy service. - Updates the user import service so that we create this association for new users - Creates a Rake script to backfill the association for existing userspull/890/head
James Rose
2 years ago
committed by
GitHub
12 changed files with 89 additions and 17 deletions
@ -0,0 +1,5 @@
|
||||
class LegacyUser < ApplicationRecord |
||||
belongs_to :user |
||||
|
||||
validates :old_user_id, uniqueness: true |
||||
end |
@ -0,0 +1,12 @@
|
||||
class CreateLegacyUsers < ActiveRecord::Migration[7.0] |
||||
def change |
||||
create_table :legacy_users do |t| |
||||
t.string :old_user_id |
||||
t.integer :user_id |
||||
|
||||
t.timestamps |
||||
end |
||||
|
||||
add_index :legacy_users, :old_user_id, unique: true |
||||
end |
||||
end |
@ -0,0 +1,11 @@
|
||||
namespace :core do |
||||
# TODO: Remove once ran on all environments. |
||||
desc "Creates a LegacyUser object for any existing Users" |
||||
task sync_legacy_users: :environment do |
||||
User.where.not(old_user_id: nil).includes(:legacy_users).find_each do |user| |
||||
next if user.legacy_users.where(old_user_id: user.old_user_id).any? |
||||
|
||||
user.legacy_users.create!(old_user_id: user.old_user_id) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,6 @@
|
||||
FactoryBot.define do |
||||
factory :legacy_user do |
||||
old_user_id {} |
||||
user |
||||
end |
||||
end |
Loading…
Reference in new issue