1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/db/migrate/20190521124609_migrate_profile_to_invoicing_profile.rb

42 lines
1.2 KiB
Ruby
Raw Normal View History

# 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
class MigrateProfileToInvoicingProfile < ActiveRecord::Migration[4.2]
2019-05-21 16:07:40 +02:00
def up
User.all.each do |u|
p = u.profile
Rails.logger.warn "User #{u.id} has no profile" and next unless p
ip = InvoicingProfile.create!(
user: u,
2019-05-21 16:07:40 +02:00
first_name: p.first_name,
last_name: p.last_name,
email: u.email
)
2023-02-24 17:26:55 +01:00
Address.find_by(placeable_id: p.id, placeable_type: 'Profile')&.update(
placeable: ip
)
2023-02-24 17:26:55 +01:00
Organization.find_by(profile_id: p.id)&.update(
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(
first_name: ip.first_name,
last_name: ip.last_name
)
2023-02-24 17:26:55 +01:00
Address.find_by(placeable_id: ip.id, placeable_type: 'InvoicingProfile')&.update(
placeable: profile
)
2023-02-24 17:26:55 +01:00
Organization.find_by(invoicing_profile_id: ip.id)&.update(
profile_id: profile.id
2019-05-21 16:07:40 +02:00
)
end
end
end