Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
migration fixes
Browse files Browse the repository at this point in the history
kavdev committed Jun 13, 2016

Unverified

This user has not yet uploaded their public signing key.
1 parent 1d046f3 commit 06f57c6
Showing 6 changed files with 763 additions and 498 deletions.
500 changes: 47 additions & 453 deletions djstripe/migrations/0009_auto_20160501_1838.py

Large diffs are not rendered by default.

45 changes: 0 additions & 45 deletions djstripe/migrations/0010_model_sync.py

This file was deleted.

471 changes: 471 additions & 0 deletions djstripe/migrations/0010_splitting_up_adds_from_alters.py

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions djstripe/migrations/0011_charge_captured_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import migrations
import djstripe.fields


class Migration(migrations.Migration):

dependencies = [
('djstripe', '0010_splitting_up_adds_from_alters'),
]

operations = [
migrations.AlterField(
model_name='charge',
name='captured',
field=djstripe.fields.StripeBooleanField(help_text='If the charge was created without capturing, this boolean represents whether or not it is still uncaptured or has since been captured.', default=False),
),
]
184 changes: 184 additions & 0 deletions djstripe/migrations/0012_model_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core import serializers
from django.db import migrations
from django.db.migrations.operations.special import RunPython
from tqdm import tqdm
from djstripe.exceptions import CustomerDoesNotExistLocallyException
from stripe.error import InvalidRequestError
from django.db.utils import IntegrityError


def resync_subscriptions(apps, schema_editor):
"""
Since subscription IDs were not previously stored, a direct migration will leave us
with a bunch of orphaned objects. It was decided [here](https://github.com/kavdev/dj-stripe/issues/162)
that a purge and re-sync would be the best option. No data that is currently available on stripe will
be deleted. Anything stored locally will be purged.
"""

# This is okay, since we're only doing a forward migration.
from djstripe.models import Subscription

import stripe
stripe.api_version = "2016-03-07"

if Subscription.objects.count():
print("Purging subscriptions. Don't worry, all active subscriptions will be re-synced from stripe. Just in case you didn't get the memo, we'll print out a json representation of each object for your records:")
print(serializers.serialize("json", Subscription.objects.all()))
Subscription.objects.all().delete()

print("Re-syncing subscriptions. This may take a while.")

for stripe_subscription in tqdm(iterable=Subscription.api_list(), desc="Sync", unit=" subscriptions"):
try:
Subscription.sync_from_stripe_data(stripe_subscription)
except CustomerDoesNotExistLocallyException:
tqdm.write("The customer for this subscription ({subscription_id}) does not exist locally (so we won't sync the subscription). You'll want to figure out how that happened.".format(subscription_id=stripe_subscription['id']))

print("Subscription re-sync complete.")


def resync_invoiceitems(apps, schema_editor):
"""
Since invoiceitem IDs were not previously stored (the ``stripe_id`` field held the id of the linked subsription),
a direct migration will leave us with a bunch of orphaned objects. It was decided [here](https://github.com/kavdev/dj-stripe/issues/162)
that a purge and re-sync would be the best option for subscriptions. That's being extended to InvoiceItems.
No data that is currently available on stripe will be deleted. Anything stored locally will be purged.
"""

# This is okay, since we're only doing a forward migration.
from djstripe.models import InvoiceItem

import stripe
stripe.api_version = "2016-03-07"

if InvoiceItem.objects.count():
print("Purging invoiceitems. Don't worry, all invoiceitems will be re-synced from stripe. Just in case you didn't get the memo, we'll print out a json representation of each object for your records:")
print(serializers.serialize("json", InvoiceItem.objects.all()))
InvoiceItem.objects.all().delete()

print("Re-syncing invoiceitems. This may take a while.")

for stripe_invoiceitem in tqdm(iterable=InvoiceItem.api_list(), desc="Sync", unit=" invoiceitems"):
try:
InvoiceItem.sync_from_stripe_data(stripe_invoiceitem)
except CustomerDoesNotExistLocallyException:
tqdm.write("The customer for this invoiceitem ({invoiceitem_id}) does not exist locally (so we won't sync the invoiceitem). You'll want to figure out how that happened.".format(invoiceitem_id=stripe_invoiceitem['id']))

print("InvoiceItem re-sync complete.")


def sync_charges(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Charge

import stripe
stripe.api_version = "2016-03-07"

if Charge.objects.count():
print("syncing charges. This may take a while.")

for charge in tqdm(Charge.objects.all(), desc="Sync", unit=" charges"):
try:
Charge.sync_from_stripe_data(charge.api_retrieve())
except InvalidRequestError:
tqdm.write("There was an error while syncing charge ({charge_id}).".format(charge_id=charge.stripe_id))

print("Charge sync complete.")


def sync_invoices(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Invoice

import stripe
stripe.api_version = "2016-03-07"

if Invoice.objects.count():
print("syncing invoices. This may take a while.")

for invoice in tqdm(iterable=Invoice.objects.all(), desc="Sync", unit=" invoices"):
try:
Invoice.sync_from_stripe_data(invoice.api_retrieve())
except InvalidRequestError:
tqdm.write("There was an error while syncing invoice ({invoice_id}).".format(invoice_id=invoice.stripe_id))

print("Invoice sync complete.")


def sync_transfers(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Transfer

import stripe
stripe.api_version = "2016-03-07"

if Transfer.objects.count():
print("syncing transfers. This may take a while.")

for transfer in tqdm(iterable=Transfer.objects.all(), desc="Sync", unit=" transfers"):
try:
Transfer.sync_from_stripe_data(transfer)
except InvalidRequestError:
tqdm.write("There was an error while syncing transfer ({transfer_id}).".format(transfer_id=transfer.stripe_id))

print("Transfer sync complete.")


def sync_plans(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Plan

import stripe
stripe.api_version = "2016-03-07"

if Plan.objects.count():
print("syncing plans. This may take a while.")

for plan in tqdm(iterable=Plan.objects.all(), desc="Sync", unit=" plans"):
try:
Plan.sync_from_stripe_data(plan)
except InvalidRequestError:
tqdm.write("There was an error while syncing plan ({plan_id}).".format(transfer_id=plan.stripe_id))

print("Transfer sync complete.")


def sync_customers(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Customer

import stripe
stripe.api_version = "2016-03-07"

if Customer.objects.count():
print("syncing customers. This may take a while.")

for customer in tqdm(Customer.objects.all(), desc="Sync", unit=" customers"):
try:
Customer.sync_from_stripe_data(customer.api_retrieve())
except InvalidRequestError:
tqdm.write("There was an error while syncing customer ({customer_id}).".format(customer_id=customer.stripe_id))
except IntegrityError:
print(customer.api_retrieve())
raise

print("Customer sync complete.")


class Migration(migrations.Migration):

dependencies = [
('djstripe', '0011_charge_captured_update'),
]

operations = [
RunPython(resync_subscriptions),
RunPython(resync_invoiceitems),
RunPython(sync_charges),
RunPython(sync_invoices),
RunPython(sync_transfers),
RunPython(sync_customers),
]
44 changes: 44 additions & 0 deletions djstripe/migrations/0013_sync_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-11 01:29
from __future__ import unicode_literals

from django.db import migrations
import djstripe.fields


class Migration(migrations.Migration):

dependencies = [
('djstripe', '0012_model_sync'),
]

operations = [
migrations.AlterField(
model_name='subscription',
name='stripe_id',
field=djstripe.fields.StripeIdField(max_length=50, unique=True),
),
migrations.AlterField(
model_name='invoiceitem',
name='stripe_id',
field=djstripe.fields.StripeIdField(max_length=50, unique=True),
),

# Removing placeholder fields. See 0010
migrations.RemoveField(
model_name='subscription',
name="plan_stripe_id",
),
migrations.RemoveField(
model_name='invoice',
name='charge_stripe_id'
),
migrations.RemoveField(
model_name='invoiceitem',
name="plan_stripe_id",
),
migrations.RemoveField(
model_name='invoiceitem',
name='subscription_stripe_id'
),
]

0 comments on commit 06f57c6

Please sign in to comment.