Ticket #24794: 0008_auto_20150510_0506.py

File 0008_auto_20150510_0506.py, 1.4 KB (added by Vernon Cole, 9 years ago)

My attempt to break up the migration into smaller chunks.

Line 
1# -*- coding: utf-8 -*-
2from __future__ import unicode_literals
3from django.db import models, migrations, transaction
4import datetime
5
6# Python code to calculate call end times, because we can't do it in SQL
7CHUNK_SIZE = 1000
8UPDATE_FIELDS = ['call_end_utc']
9def calculate_end_time(apps, schema_editor):
10 transaction.set_autocommit(False)
11 Cdr = apps.get_model("phx", "Cdr")
12 last_chunk = 0
13 new_items = True
14 while new_items:
15 new_items = False
16 cdr = None
17 for cdr in Cdr.objects.all()[last_chunk:last_chunk+CHUNK_SIZE]:
18 if cdr.call_end_utc is None:
19 cdr.call_end_utc = cdr.call_utc + datetime.timedelta(seconds=cdr.exact_call_duration)
20 cdr.save(update_fields=UPDATE_FIELDS)
21 transaction.commit()
22 if cdr is not None:
23 new_items = True
24 last_chunk += CHUNK_SIZE
25
26def do_nothing(apps, schema_editor):
27 pass # we need a callable for the reverse direction, so we use this.
28
29
30class Migration(migrations.Migration):
31
32 dependencies = [
33 ('phx', '0007_cdr_call_end_utc'),
34 ]
35
36 operations = [
37 migrations.RunPython(calculate_end_time, do_nothing, atomic=False)
38
39 #migrations.AlterField(
40 # model_name='cdr',
41 # name='call_end_utc',
42 # field=models.DateTimeField(db_index=True, help_text='UTC Timestamp when call disconnected.'),
43 #),
44 ]
Back to Top