2020-03-24 16:45:27 +01:00
|
|
|
# frozen_string_literal:true
|
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# From this migration, we split the user's profile into multiple tables:
|
|
|
|
# InvoicingProfile is intended to keep invoicing data about the user after his account was deleted
|
2020-03-24 16:45:27 +01:00
|
|
|
class MigrateProfileToInvoicingProfile < ActiveRecord::Migration[4.2]
|
2019-05-21 16:07:40 +02:00
|
|
|
def up
|
2019-05-22 17:49:22 +02:00
|
|
|
User.all.each do |u|
|
|
|
|
p = u.profile
|
2022-07-26 17:27:33 +02:00
|
|
|
Rails.logger.warn "User #{u.id} has no profile" and next unless p
|
2019-05-22 17:49:22 +02:00
|
|
|
|
2019-05-22 12:45:45 +02:00
|
|
|
ip = InvoicingProfile.create!(
|
2019-05-22 17:49:22 +02:00
|
|
|
user: u,
|
2019-05-21 16:07:40 +02:00
|
|
|
first_name: p.first_name,
|
2019-05-29 14:28:14 +02:00
|
|
|
last_name: p.last_name,
|
|
|
|
email: u.email
|
2019-05-22 12:45:45 +02:00
|
|
|
)
|
2023-02-24 17:26:55 +01:00
|
|
|
Address.find_by(placeable_id: p.id, placeable_type: 'Profile')&.update(
|
2019-05-22 12:45:45 +02:00
|
|
|
placeable: ip
|
|
|
|
)
|
2023-02-24 17:26:55 +01:00
|
|
|
Organization.find_by(profile_id: p.id)&.update(
|
2019-05-22 12:45:45 +02:00
|
|
|
invoicing_profile_id: ip.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
InvoicingProfile.all.each do |ip|
|
|
|
|
profile = ip.user.profile
|
2023-02-24 17:26:55 +01:00
|
|
|
profile.update(
|
2019-05-22 12:45:45 +02:00
|
|
|
first_name: ip.first_name,
|
2019-05-22 17:49:22 +02:00
|
|
|
last_name: ip.last_name
|
2019-05-22 12:45:45 +02:00
|
|
|
)
|
2023-02-24 17:26:55 +01:00
|
|
|
Address.find_by(placeable_id: ip.id, placeable_type: 'InvoicingProfile')&.update(
|
2019-05-22 12:45:45 +02:00
|
|
|
placeable: profile
|
|
|
|
)
|
2023-02-24 17:26:55 +01:00
|
|
|
Organization.find_by(invoicing_profile_id: ip.id)&.update(
|
2019-05-22 12:45:45 +02:00
|
|
|
profile_id: profile.id
|
2019-05-21 16:07:40 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|