Changeset View
Changeset View
Standalone View
Standalone View
looper/migrations/0035_order_name.py
| # Generated by Django 2.1.2 on 2018-10-05 10:55 | # Generated by Django 2.1.2 on 2018-10-05 10:55 | ||||
| from typing import Any | |||||
| from django.db import migrations, models | from django.db import migrations, models | ||||
| from django.db.transaction import atomic | from django.db.transaction import atomic | ||||
| @atomic | @atomic | ||||
| def set_default_name(apps, schema_editor): | def set_default_name(apps: Any, schema_editor: Any) -> Any: | ||||
| # We can't import the Person model directly as it may be a newer | # We can't import the Person model directly as it may be a newer | ||||
| # version than this migration expects. We use the historical version. | # version than this migration expects. We use the historical version. | ||||
| Order = apps.get_model('looper', 'Order') | Order = apps.get_model('looper', 'Order') | ||||
| query = Order.objects.filter(name='').select_related('subscription__plan', | query = Order.objects.filter(name='').select_related( | ||||
| 'subscription__plan__product') | 'subscription__plan', 'subscription__plan__product' | ||||
| ) | |||||
| for order in query: | for order in query: | ||||
| order.name = f'{order.subscription.plan.product.name} / {order.subscription.plan.name}' | order.name = f'{order.subscription.plan.product.name} / {order.subscription.plan.name}' | ||||
| order.save(update_fields={'name'}) | order.save(update_fields={'name'}) | ||||
| def noop(apps, schema_editor): | def noop(apps: Any, schema_editor: Any) -> Any: | ||||
| # Reverting the data isn't necessary, as the whole column will be dropped anyway. | # Reverting the data isn't necessary, as the whole column will be dropped anyway. | ||||
| pass | pass | ||||
| class Migration(migrations.Migration): | class Migration(migrations.Migration): | ||||
| dependencies = [ | dependencies = [ | ||||
| ('looper', '0034_order_paid_at'), | ('looper', '0034_order_paid_at'), | ||||
| ] | ] | ||||
| operations = [ | operations = [ | ||||
| migrations.AddField( | migrations.AddField( | ||||
| model_name='order', | model_name='order', | ||||
| name='name', | name='name', | ||||
| field=models.CharField(default='', | field=models.CharField( | ||||
| help_text='What the order is for; shown to the customer', | default='', help_text='What the order is for; shown to the customer', max_length=255 | ||||
| max_length=255), | ), | ||||
| preserve_default=False, | preserve_default=False, | ||||
| ), | ), | ||||
| migrations.RunPython(set_default_name, noop) | migrations.RunPython(set_default_name, noop), | ||||
| ] | ] | ||||