2019-05-28 16:02:55 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-24 11:38:25 +02:00
|
|
|
require 'integrity/archive_helper'
|
|
|
|
|
2019-05-28 16:02:55 +02:00
|
|
|
# migrate the invoices from being attached to a user to invoicing_profiles which are GDPR compliant
|
2020-03-24 16:45:27 +01:00
|
|
|
class MigrateInvoiceToInvoicingProfile < ActiveRecord::Migration[4.2]
|
2019-05-22 17:49:22 +02:00
|
|
|
def up
|
|
|
|
# first, check the footprints
|
2021-05-24 11:38:25 +02:00
|
|
|
Integrity::ArchiveHelper.check_footprints
|
2019-05-28 16:02:55 +02:00
|
|
|
|
2019-05-22 17:49:22 +02:00
|
|
|
# if everything is ok, proceed with migration
|
2019-05-28 16:02:55 +02:00
|
|
|
# remove and save periods in memory
|
2021-05-24 11:38:25 +02:00
|
|
|
periods = Integrity::ArchiveHelper.backup_and_remove_periods
|
2019-05-28 16:02:55 +02:00
|
|
|
# migrate invoices
|
|
|
|
puts 'Migrating invoices. This may take a while...'
|
|
|
|
Invoice.order(:id).all.each do |i|
|
2019-05-29 14:28:14 +02:00
|
|
|
user = User.find(i.user_id)
|
2019-06-12 12:22:38 +02:00
|
|
|
operator = User.find_by(id: i.operator_id)
|
2019-05-29 14:28:14 +02:00
|
|
|
i.update_column('invoicing_profile_id', user.invoicing_profile.id)
|
2019-06-11 10:02:48 +02:00
|
|
|
i.update_column('statistic_profile_id', user.statistic_profile.id)
|
2019-06-12 12:22:38 +02:00
|
|
|
i.update_column('operator_profile_id', operator&.invoicing_profile&.id)
|
2019-05-22 17:49:22 +02:00
|
|
|
i.update_column('user_id', nil)
|
2019-06-12 12:22:38 +02:00
|
|
|
i.update_column('operator_id', nil)
|
2019-05-22 17:49:22 +02:00
|
|
|
end
|
2019-05-28 16:02:55 +02:00
|
|
|
# chain all records
|
|
|
|
InvoiceItem.order(:id).all.each(&:chain_record)
|
|
|
|
Invoice.order(:id).all.each(&:chain_record)
|
|
|
|
# write memory dump into database
|
2021-05-24 11:38:25 +02:00
|
|
|
Integrity::ArchiveHelper.restore_periods(periods)
|
2019-05-22 17:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
2019-05-28 16:02:55 +02:00
|
|
|
# here we don't check footprints to save processing time and because this is pointless when reverting the migrations
|
|
|
|
|
|
|
|
# remove and save periods in memory
|
2021-05-24 11:38:25 +02:00
|
|
|
periods = Integrity::ArchiveHelper.backup_and_remove_periods
|
2019-05-28 16:02:55 +02:00
|
|
|
# reset invoices
|
2019-05-22 17:49:22 +02:00
|
|
|
Invoice.order(:created_at).all.each do |i|
|
|
|
|
i.update_column('user_id', i.invoicing_profile.user_id)
|
2019-06-12 12:22:38 +02:00
|
|
|
i.update_column('operator_id', i.operator_profile.user_id)
|
2019-05-22 17:49:22 +02:00
|
|
|
i.update_column('invoicing_profile_id', nil)
|
2019-06-11 10:02:48 +02:00
|
|
|
i.update_column('statistic_profile_id', nil)
|
2019-06-12 12:22:38 +02:00
|
|
|
i.update_column('operator_profile_id', nil)
|
2019-05-22 17:49:22 +02:00
|
|
|
end
|
2019-05-28 16:02:55 +02:00
|
|
|
# chain all records
|
|
|
|
InvoiceItem.order(:id).all.each(&:chain_record)
|
|
|
|
Invoice.order(:id).all.each(&:chain_record)
|
|
|
|
# write memory dump into database
|
2021-05-24 11:38:25 +02:00
|
|
|
Integrity::ArchiveHelper.restore_periods(periods)
|
2019-05-22 17:49:22 +02:00
|
|
|
end
|
|
|
|
end
|