Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
[bug 1139713] Fix received_ts migration
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Mar 5, 2015
1 parent 3619dc8 commit d853fa9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 0 additions & 1 deletion fjord/heartbeat/migrations/0007_auto_20150305_1119.py
Expand Up @@ -38,7 +38,6 @@ def set_received_ts(apps, schema_editor):

ans.received_ts = dt
ans.save()
break


class Migration(migrations.Migration):
Expand Down
56 changes: 56 additions & 0 deletions fjord/heartbeat/migrations/0008_auto_20150305_1442.py
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""Set received_ts using the value in updated_ts. It's not the same
thing, but it's "close enough" for an initial value where we don't
have anything better to base it on.
This is just like 0007, but since I screwed that one up, we have to
do it again.
"""
from __future__ import unicode_literals

from datetime import datetime

from django.conf import settings
from django.db import models, migrations

import pytz


def no_op(apps, schema_editor):
pass


def set_received_ts(apps, schema_editor):
"""Sets received_ts based on updated_ts"""
Answer = apps.get_model('heartbeat', 'Answer')

qs = Answer.objects.filter(received_ts=datetime(2011, 9, 1, 9, 0))
for ans in qs:
# Note: Answer.updated_ts is milliseconds since epoch. We're
# assuming it's in UTC, so we convert it to server time.
dt = datetime.fromtimestamp(ans.updated_ts / 1000)

# Apply UTC
utc_tz = pytz.timezone('UTC')
dt = utc_tz.localize(dt)

# Then switch to server time
local_tz = pytz.timezone(settings.TIME_ZONE)
dt = dt.astimezone(local_tz)

ans.received_ts = dt
ans.save()


class Migration(migrations.Migration):

dependencies = [
('heartbeat', '0007_auto_20150305_1119'),
]

operations = [
# Note: This can't be backed out, but for the sake of testing
# and convenience, we provide a no-op.
migrations.RunPython(set_received_ts, reverse_code=no_op),
]

0 comments on commit d853fa9

Please sign in to comment.