Skip to content

Commit

Permalink
Merge pull request #4104 from spaghetticode/migrate-addresses-in-batches
Browse files Browse the repository at this point in the history
Update billing address migration tasks with batch limit
  • Loading branch information
kennyadsl authored Jun 24, 2021
2 parents ac3a2f8 + 45c364b commit bc73004
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
namespace :solidus do
namespace :migrations do
namespace :migrate_default_billing_addresses_to_address_book do
task up: :environment do
print "Migrating default billing addresses to address book ... "
task :up, [:batch_size] => [:environment] do |_t, args|
batch_size = args[:batch_size] || 100_000
print "Migrating default billing addresses to address book in batches of #{batch_size} ... "
if Spree::UserAddress.where(default_billing: true).any?
Spree.user_class.joins(:bill_address).update_all(bill_address_id: nil) # rubocop:disable Rails/SkipsModelValidations
Spree.user_class.joins(:bill_address).in_batches(of: batch_size).each do |batch|
batch.update_all(bill_address_id: nil) # rubocop:disable Rails/SkipsModelValidations
end
end
Spree::UserAddress.joins(
<<~SQL
JOIN spree_users ON spree_user_addresses.user_id = spree_users.id
AND spree_user_addresses.address_id = spree_users.bill_address_id
SQL
).update_all(default_billing: true)
).in_batches(of: batch_size).each do |batch|
batch.update_all(default_billing: true) # rubocop:disable Rails/SkipsModelValidations
end

puts "Success"
end

task down: :environment do
Spree::UserAddress.update_all(default_billing: false) # rubocop:disable Rails/SkipsModelValidations
task :down, [:batch_size] => [:environment] do |_t, args|
batch_size = args[:batch_size] || 100_000
Spree::UserAddress.in_batches(of: batch_size).update_all(default_billing: false) # rubocop:disable Rails/SkipsModelValidations
puts "Rolled back default billing address migration to address book"
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

it 'runs' do
expect { task.invoke }.to output(
"Migrating default billing addresses to address book ... Success\n"
"Migrating default billing addresses to address book in batches of 100000 ... Success\n"
).to_stdout
end

Expand Down Expand Up @@ -63,7 +63,7 @@

it 'runs' do
expect { task.invoke }.to output(
"Migrating default billing addresses to address book ... Success\n"
"Migrating default billing addresses to address book in batches of 100000 ... Success\n"
).to_stdout
end

Expand Down

0 comments on commit bc73004

Please sign in to comment.