2021-05-25 17:28:35 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Following the scheme of the previous migration (20210521085710_add_object_to_invoice_item.rb)
|
|
|
|
# we'll save the bought objects associated to a payment schedule into this data table.
|
|
|
|
class CreatePaymentScheduleObjects < ActiveRecord::Migration[5.2]
|
|
|
|
def up
|
|
|
|
create_table :payment_schedule_objects do |t|
|
|
|
|
t.references :object, polymorphic: true
|
|
|
|
t.belongs_to :payment_schedule, foreign_key: true
|
|
|
|
t.boolean :main
|
|
|
|
t.string :footprint
|
|
|
|
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
|
2021-05-31 12:19:28 +02:00
|
|
|
# PaymentSchedules were not in archives until now, so there's no need to regenerate the archives
|
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
# migrate data
|
|
|
|
PaymentSchedule.all.each do |payment_schedule|
|
|
|
|
PaymentScheduleObject.create!(
|
|
|
|
payment_schedule: payment_schedule,
|
|
|
|
object_id: payment_schedule.scheduled_id,
|
|
|
|
object_type: payment_schedule.scheduled_type,
|
|
|
|
main: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
PaymentSchedule.where(scheduled_type: 'Reservation').each do |payment_schedule|
|
|
|
|
PaymentScheduleObject.create!(
|
|
|
|
payment_schedule: payment_schedule,
|
|
|
|
object_id: payment_schedule.payment_schedule_items.first.details['subscription_id'],
|
|
|
|
object_type: 'Subscription'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-03 15:02:48 +02:00
|
|
|
execute %(
|
2021-06-07 09:56:03 +02:00
|
|
|
UPDATE payment_schedule_items
|
2021-06-03 15:02:48 +02:00
|
|
|
SET payment_method = 'card'
|
|
|
|
WHERE payment_method = 'stripe'
|
|
|
|
)
|
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
remove_column :payment_schedules, :scheduled_id
|
|
|
|
remove_column :payment_schedules, :scheduled_type
|
|
|
|
PaymentScheduleItem.update_all("details = details - 'subscription_id'")
|
|
|
|
|
2021-06-07 14:38:47 +02:00
|
|
|
PaymentScheduleItem.connection.schema_cache.clear!
|
|
|
|
PaymentScheduleItem.reset_column_information
|
|
|
|
PaymentSchedule.connection.schema_cache.clear!
|
|
|
|
PaymentSchedule.reset_column_information
|
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
# chain records
|
|
|
|
puts 'Chaining all record. This may take a while...'
|
|
|
|
PaymentScheduleItem.order(:id).all.each(&:chain_record)
|
|
|
|
PaymentSchedule.order(:id).all.each(&:chain_record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
add_column :payment_schedules, :scheduled_id, :integer
|
|
|
|
add_column :payment_schedules, :scheduled_type, :string
|
|
|
|
|
|
|
|
# migrate data
|
|
|
|
PaymentScheduleObject.where(main: true).each do |pso|
|
2021-05-27 11:31:07 +02:00
|
|
|
execute %(
|
|
|
|
UPDATE payment_schedules
|
|
|
|
SET scheduled_id = #{pso.object_id},
|
|
|
|
scheduled_type = '#{pso.object_type}'
|
|
|
|
WHERE id = #{pso.payment_schedule.id}
|
2021-05-25 17:28:35 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
PaymentScheduleObject.where(object_type: 'Subscription').each do |pso|
|
|
|
|
pso.payment_schedule.payment_schedule_items.each do |psi|
|
|
|
|
psi.details['subscription_id'] = pso.object_id
|
2021-05-27 11:31:07 +02:00
|
|
|
psi.save(validate: false)
|
2021-05-25 17:28:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-03 15:02:48 +02:00
|
|
|
execute %(
|
2021-06-07 09:56:03 +02:00
|
|
|
UPDATE payment_schedule_items
|
2021-06-03 15:02:48 +02:00
|
|
|
SET payment_method = 'stripe'
|
|
|
|
WHERE payment_method = 'card'
|
|
|
|
)
|
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
drop_table :payment_schedule_objects
|
|
|
|
|
2021-06-07 14:38:47 +02:00
|
|
|
PaymentScheduleItem.connection.schema_cache.clear!
|
|
|
|
PaymentScheduleItem.reset_column_information
|
|
|
|
PaymentSchedule.connection.schema_cache.clear!
|
|
|
|
PaymentSchedule.reset_column_information
|
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
# chain records
|
|
|
|
puts 'Chaining all record. This may take a while...'
|
|
|
|
PaymentScheduleItem.order(:id).all.each(&:chain_record)
|
|
|
|
PaymentSchedule.order(:id).all.each(&:chain_record)
|
|
|
|
end
|
|
|
|
end
|