Replace :truncate_data definition with new class

And cover it with a test.
This commit is contained in:
Pau Perez
2020-03-06 17:34:45 +01:00
parent 78fd785f0c
commit 60d29d619f
2 changed files with 48 additions and 66 deletions

View File

@@ -1,3 +1,8 @@
# frozen_string_literal: true
require 'highline'
require 'tasks/data/truncate_data'
# This task can be used to significantly reduce the size of a database
# This is used for example when loading live data into a staging server
# This way the staging server is not overloaded with too much data
@@ -7,75 +12,18 @@ namespace :ofn do
task truncate: :environment do
guard_and_warn
sql_delete_from "
spree_inventory_units #{where_order_id_in_orders_to_delete}"
sql_delete_from "
spree_inventory_units
where shipment_id in (select id from spree_shipments #{where_order_id_in_orders_to_delete})"
truncate_adjustments
sql_delete_from "spree_line_items #{where_order_id_in_orders_to_delete}"
sql_delete_from "spree_payments #{where_order_id_in_orders_to_delete}"
sql_delete_from "spree_shipments #{where_order_id_in_orders_to_delete}"
Spree::ReturnAuthorization.delete_all
truncate_order_cycle_data
sql_delete_from "proxy_orders #{where_oc_id_in_ocs_to_delete}"
sql_delete_from "spree_orders #{where_oc_id_in_ocs_to_delete}"
sql_delete_from "order_cycle_schedules #{where_oc_id_in_ocs_to_delete}"
sql_delete_from "order_cycles #{where_ocs_to_delete}"
Spree::TokenizedPermission.where("created_at < '#{date}'").delete_all
Spree::StateChange.delete_all
Spree::LogEntry.delete_all
sql_delete_from "sessions"
TruncateData.new.call
end
def sql_delete_from(sql)
ActiveRecord::Base.connection.execute("delete from #{sql}")
end
def guard_and_warn
if Rails.env.production?
Rails.logger.info("This task cannot be executed in production")
exit
end
private
def date
3.months.ago
end
def where_ocs_to_delete
"where orders_close_at < '#{date}'"
end
def where_oc_id_in_ocs_to_delete
"where order_cycle_id in (select id from order_cycles #{where_ocs_to_delete} )"
end
def where_order_id_in_orders_to_delete
"where order_id in (select id from spree_orders #{where_oc_id_in_ocs_to_delete})"
end
def truncate_adjustments
sql_delete_from "spree_adjustments where source_type = 'Spree::Order'
and source_id in (select id from spree_orders #{where_oc_id_in_ocs_to_delete})"
sql_delete_from "spree_adjustments where source_type = 'Spree::Shipment'
and source_id in (select id from spree_shipments #{where_order_id_in_orders_to_delete})"
sql_delete_from "spree_adjustments where source_type = 'Spree::Payment'
and source_id in (select id from spree_payments #{where_order_id_in_orders_to_delete})"
sql_delete_from "spree_adjustments where source_type = 'Spree::LineItem'
and source_id in (select id from spree_line_items #{where_order_id_in_orders_to_delete})"
end
def truncate_order_cycle_data
sql_delete_from "coordinator_fees #{where_oc_id_in_ocs_to_delete}"
sql_delete_from "
exchange_variants where exchange_id
in (select id from exchanges #{where_oc_id_in_ocs_to_delete})"
sql_delete_from "
exchange_fees where exchange_id
in (select id from exchanges #{where_oc_id_in_ocs_to_delete})"
sql_delete_from "exchanges #{where_oc_id_in_ocs_to_delete}"
message = "\n <%= color('This will permanently change DB contents', :yellow) %>,
are you sure you want to proceed? (y/N)"
exit unless HighLine.new.agree(message) { |q| q.default = "n" }
end
end
end