Merge pull request #3579 from luisramos0/sanitize_data

Add sanitize and truncate data tasks
This commit is contained in:
Luis Ramos
2019-03-22 10:29:57 +00:00
committed by GitHub
3 changed files with 155 additions and 43 deletions

View File

@@ -1,43 +0,0 @@
def update_address(address, user)
unless address.nil?
address.firstname = user[:first_name]
address.lastname = user[:last_name]
address.phone = user[:phone]
address.save!
end
end
def sanitize_data
canned_users = [ { :first_name => "Bob", :last_name => "jones", :email => "bob@jones.com", :phone => "0123456789" },
{ :first_name => "cindy", :last_name => "rest", :email => "cindy@gmail.com", :phone => "0123456789" },
{ :first_name => "Pete", :last_name => "smith", :email => "pete@gmail.com", :phone => "0123456789" },
{ :first_name => "Tony", :last_name => "ballantyne", :email => "tony@gmail.com", :phone => "0123456789" },
{ :first_name => "Ben", :last_name => "raven", :email => "ben@gmail.com", :phone => "0123456789" },
{ :first_name => "Robyn", :last_name => "monster", :email => "robyn@gmail.com", :phone => "0123456789" },
{ :first_name => "Nako", :last_name => "tolkein", :email => "nako@gmail.com", :phone => "0123456789" },
{ :first_name => "Helen", :last_name => "mitcham", :email => "helen@gmail.com", :phone => "0123456789" },
{ :first_name => "Emma", :last_name => "low", :email => "emma@gmail.com", :phone => "0123456789" },
{ :first_name => "Mandy", :last_name => "Trust", :email => "Mandy@trust.com", :phone => "0123456789" } ]
Spree::Order.all.each_with_index do |order, index|
canned_user = canned_users[index%canned_users.size]
puts "updating order #{order.id} with #{canned_user[:first_name]}"
order.email = canned_user[:email]
update_address(order.bill_address, canned_user)
update_address(order.ship_address, canned_user)
order.save!
end
Spree::User.all.each_with_index do |user, index|
unless user.email == "admin@openfoodweb.org"
canned_user = canned_users[index%canned_users.size]
puts "updating user #{user.id} with #{canned_user[:first_name]}"
user.email = "#{canned_user[:email]}#{index}"
user.save!
end
end
end

View File

@@ -0,0 +1,75 @@
require 'highline'
namespace :ofn do
namespace :data do
desc 'Anonymize data'
task anonymize: :environment do
guard_and_warn
anonymize_users_data
Spree::Address.update_all("
firstname = concat('Ms. Number', id), lastname = 'Jones', phone = '01234567890',
alternative_phone = '01234567890', address1 = 'Dummy address',
address2 = 'Dummy address continuation',
company = null, latitude = null, longitude = null")
anonymize_payments_data
anonymize_payments_accounts
Spree::TokenizedPermission.update_all("token = null")
ActiveRecord::Base.connection.execute("update spree_mail_methods
set environment = '#{Rails.env}'")
# Delete all preferences that may contain sensitive information
Spree::Preference
.where("key like '%gateway%' OR key like '%billing_integration%' OR key like '%s3%'")
.delete_all
end
def guard_and_warn
if Rails.env.production?
Rails.logger.info("This task cannot be executed in production")
exit
end
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
private
def anonymize_users_data
Spree::User.update_all("email = concat(id, '_ofn_user@example.com'),
login = concat(id, '_ofn_user@example.com'),
unconfirmed_email = concat(id, '_ofn_user@example.com')")
Customer.where("user_id IS NULL")
.update_all("email = concat(id, '_ofn_customer@example.com'),
name = concat('Customer Number ', id, ' (without connected User)')")
Customer.where("user_id IS NOT NULL")
.update_all("email = concat(user_id, '_ofn_user@example.com'),
name = concat('Customer Number ', id, ' - User ', user_id)")
Spree::Order.update_all("email = concat(id, '_ofn_order@example.com')")
end
def anonymize_payments_data
Spree::PaymentMethod.update_all("name = concat('Dummy Payment Method', id),
description = name,
environment = '#{Rails.env}'")
Spree::Payment.update_all("response_code = null, avs_response = null,
cvv_response_code = null, identifier = null,
cvv_response_message = null")
Spree::CreditCard.update_all("
month = 12, year = 2020, start_month = 12, start_year = 2000,
cc_type = 'VISA', first_name = 'Dummy', last_name = 'Dummy', last_digits = '2543'")
end
def anonymize_payments_accounts
Spree::PaypalExpressCheckout.update_all("token = null")
StripeAccount.delete_all
ActiveRecord::Base.connection.execute("delete from spree_paypal_accounts")
end
end
end

View File

@@ -0,0 +1,80 @@
# 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
namespace :ofn do
namespace :data do
desc 'Truncate data'
task truncate: :environment do
guard_and_warn
sql_delete_from "
spree_inventory_units #{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}"
sql_delete_from "billable_periods"
sql_delete_from "account_invoices"
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"
end
def sql_delete_from(sql)
ActiveRecord::Base.connection.execute("delete from #{sql}")
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}"
end
end
end