natdeanlewissoftwire
2 years ago
12 changed files with 89 additions and 18 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