Skip to content

Commit

Permalink
move txn migration to a separate script
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jul 14, 2024
1 parent ed492e4 commit f062d81
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
from django.db import migrations, models


def forwards_func(apps, schema_editor):
from payments.plans import PricingPlan
from app_users.models import TransactionReason

# We get the model from the versioned app registry;
# if we directly import it, it'll be the wrong version
AppUserTransaction = apps.get_model("app_users", "AppUserTransaction")
db_alias = schema_editor.connection.alias
objects = AppUserTransaction.objects.using(db_alias)

for transaction in objects.filter(amount__gt=0):
# For old transactions, we didn't have a subscription field.
# It just so happened that all monthly subscriptions we offered had
# different amounts from the one-time purchases.
# This uses that heuristic to determine whether a transaction
# was a subscription payment or a one-time purchase.
transaction.reason = TransactionReason.ADDON
for plan in PricingPlan:
if (
transaction.amount == plan.credits
and transaction.charged_amount == plan.monthly_charge * 100
):
transaction.plan = plan.db_value
transaction.reason = TransactionReason.SUBSCRIBE
break
transaction.save(update_fields=["reason", "plan"])


class Migration(migrations.Migration):
Expand All @@ -48,10 +22,4 @@ class Migration(migrations.Migration):
name='reason',
field=models.IntegerField(choices=[(1, 'Deduct'), (2, 'Addon'), (3, 'Subscribe'), (4, 'Sub-Create'), (5, 'Sub-Cycle'), (6, 'Sub-Update'), (7, 'Auto-Recharge')], default=1, help_text='The reason for this transaction.<br><br>Deduct: Credits deducted due to a run.<br>Addon: User purchased an add-on.<br>Subscribe: Applies to subscriptions where no distinction was made between create, update and cycle.<br>Sub-Create: A subscription was created.<br>Sub-Cycle: A subscription advanced into a new period.<br>Sub-Update: A subscription was updated.<br>Auto-Recharge: Credits auto-recharged due to low balance.'),
),
migrations.RunPython(forwards_func, migrations.RunPython.noop),
migrations.AlterField(
model_name='appusertransaction',
name='reason',
field=models.IntegerField(choices=[(1, 'Deduct'), (2, 'Addon'), (3, 'Subscribe'), (4, 'Sub-Create'), (5, 'Sub-Cycle'), (6, 'Sub-Update'), (7, 'Auto-Recharge')], help_text='The reason for this transaction.<br><br>Deduct: Credits deducted due to a run.<br>Addon: User purchased an add-on.<br>Subscribe: Applies to subscriptions where no distinction was made between create, update and cycle.<br>Sub-Create: A subscription was created.<br>Sub-Cycle: A subscription advanced into a new period.<br>Sub-Update: A subscription was updated.<br>Auto-Recharge: Credits auto-recharged due to low balance.'),
),
]
18 changes: 18 additions & 0 deletions app_users/migrations/0019_alter_appusertransaction_reason.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-07-14 21:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app_users', '0018_appusertransaction_plan_appusertransaction_reason'),
]

operations = [
migrations.AlterField(
model_name='appusertransaction',
name='reason',
field=models.IntegerField(choices=[(1, 'Deduct'), (2, 'Addon'), (3, 'Subscribe'), (4, 'Sub-Create'), (5, 'Sub-Cycle'), (6, 'Sub-Update'), (7, 'Auto-Recharge')], help_text='The reason for this transaction.<br><br>Deduct: Credits deducted due to a run.<br>Addon: User purchased an add-on.<br>Subscribe: Applies to subscriptions where no distinction was made between create, update and cycle.<br>Sub-Create: A subscription was created.<br>Sub-Cycle: A subscription advanced into a new period.<br>Sub-Update: A subscription was updated.<br>Auto-Recharge: Credits auto-recharged due to low balance.'),
),
]
21 changes: 21 additions & 0 deletions scripts/migrate_txn_reason.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from app_users.models import TransactionReason, AppUserTransaction
from payments.plans import PricingPlan


def run():
for transaction in AppUserTransaction.objects.filter(amount__gt=0):
# For old transactions, we didn't have a subscription field.
# It just so happened that all monthly subscriptions we offered had
# different amounts from the one-time purchases.
# This uses that heuristic to determine whether a transaction
# was a subscription payment or a one-time purchase.
transaction.reason = TransactionReason.ADDON
for plan in PricingPlan:
if (
transaction.amount == plan.credits
and transaction.charged_amount == plan.monthly_charge * 100
):
transaction.plan = plan.db_value
transaction.reason = TransactionReason.SUBSCRIBE
break
transaction.save(update_fields=["reason", "plan"])

0 comments on commit f062d81

Please sign in to comment.