Ticket #32805: 0017_forecast_issued_at.py

File 0017_forecast_issued_at.py, 2.1 KB (added by Matthew Cornell, 3 years ago)
Line 
1# Generated by Django 3.1.7 on 2021-04-29 14:19
2import datetime
3
4from django.db import migrations, models
5
6
7#
8# This file does both schema and data migrations for renaming the `Forecast.issue_date` field to `Forecast.issued_at`.
9# I edited the Django-generated file to get this. We default the conversion from date to datetime arbitrarily to 12
10# noon.
11#
12
13def forwards_func(apps, schema_editor):
14 # via https://docs.djangoproject.com/en/2.2/ref/migration-operations/#runpython : We get the model from the
15 # versioned app registry; if we directly import it, it'll be the wrong version:
16 Forecast = apps.get_model("forecast_app", "Forecast")
17
18 # this is the slow one-at-a-time approach. I tried using https://docs.djangoproject.com/en/2.2/ref/models/expressions/#f-expressions
19 # but was too complicated
20 for forecast in Forecast.objects.all().iterator():
21 forecast.issued_at = datetime.datetime.combine(forecast.issue_date, datetime.time(hour=12),
22 tzinfo=datetime.timezone.utc)
23 forecast.save()
24
25
26class Migration(migrations.Migration):
27 dependencies = [
28 ('forecast_app', '0016_job_json_fields'),
29 ]
30
31 operations = [
32 migrations.RemoveConstraint(
33 model_name='forecast',
34 name='unique_version',
35 ),
36 migrations.AddField(
37 model_name='forecast',
38 name='issued_at',
39 field=models.DateTimeField(db_index=True, default=None, null=True),
40 preserve_default=False,
41 ),
42 migrations.RunPython(forwards_func, reverse_code=migrations.RunPython.noop),
43 migrations.AlterField(
44 model_name='forecast',
45 name='issued_at',
46 field=models.DateTimeField(db_index=True, default=None, null=False),
47 ),
48 migrations.RemoveField(
49 model_name='forecast',
50 name='issue_date',
51 ),
52 migrations.AddConstraint(
53 model_name='forecast',
54 constraint=models.UniqueConstraint(fields=('forecast_model', 'time_zero', 'issued_at'),
55 name='unique_version'),
56 ),
57 ]
Back to Top