Skip to content

Commit

Permalink
WIP Add task to migration subscription to new prices
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 22, 2024
1 parent c314f96 commit 64f8790
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/tasks/subscriptions.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace :subscriptions do
desc 'Migrate Stripe subscription to new price'
task migrate_price: :environment do
old_price, new_price = *load_prices

scope = Stripe::Subscription.list(price: old_price.id)
count = scope.data.size

scope.auto_paging_each.with_index do |subscription, index|
item = subscription.items.first
Stripe::Subscription.update(
subscription.id,
items: [{ id: item.id, price: new_price.id }],
proration_behavior: 'none'
)

erase_line
print "Migrated subscriptions #{index + 1}/#{count}"
end

erase_line
puts "Migrating all subscriptions completed."
end

def load_prices
old_price = AlaveteliPro::Price.retrieve(ENV['OLD_PRICE']) if ENV['OLD_PRICE']

Check warning on line 26 in lib/tasks/subscriptions.rake

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Modifier form of `if` makes the line too long. (https://rubystyle.guide#if-as-a-modifier) Raw Output: lib/tasks/subscriptions.rake:26:64: C: Style/IfUnlessModifier: Modifier form of `if` makes the line too long. (https://rubystyle.guide#if-as-a-modifier)

Check warning on line 26 in lib/tasks/subscriptions.rake

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [82/80] (https://rubystyle.guide#max-line-length) Raw Output: lib/tasks/subscriptions.rake:26:81: C: Layout/LineLength: Line is too long. [82/80] (https://rubystyle.guide#max-line-length)
new_price = AlaveteliPro::Price.retrieve(ENV['NEW_PRICE']) if ENV['NEW_PRICE']

Check warning on line 27 in lib/tasks/subscriptions.rake

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Modifier form of `if` makes the line too long. (https://rubystyle.guide#if-as-a-modifier) Raw Output: lib/tasks/subscriptions.rake:27:64: C: Style/IfUnlessModifier: Modifier form of `if` makes the line too long. (https://rubystyle.guide#if-as-a-modifier)

Check warning on line 27 in lib/tasks/subscriptions.rake

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [82/80] (https://rubystyle.guide#max-line-length) Raw Output: lib/tasks/subscriptions.rake:27:81: C: Layout/LineLength: Line is too long. [82/80] (https://rubystyle.guide#max-line-length)

if !old_price
puts "ERROR: Can't find OLD_PRICE"
exit 1
elsif !new_price
puts "ERROR: Can't find NEW_PRICE"
exit 1
elsif old_price.recurring != new_price.recurring
puts "ERROR: Price interval and interval_count need to match"
exit 1
end

[old_price, new_price]
end

def erase_line
# https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
print "\e[1G\e[K"
end
end

0 comments on commit 64f8790

Please sign in to comment.