0004_auto_20160205_1329.py
1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
def migrate_frames(apps, schemaeditor):
History = apps.get_model('history', 'History')
HistoryFrame = apps.get_model('history', 'HistoryFrame')
frame_keys = {}
for history_item in History.objects.exclude(lexeme=None):
tb = history_item.transaction_began
lexeme = history_item.lexeme
key = (tb, lexeme)
user = history_item.user
if key in frame_keys:
frame_user, frame = frame_keys[key]
assert frame_user == user
else:
frame = HistoryFrame.objects.create(
transaction_began=tb,
lexeme=lexeme,
user=user)
frame_keys[key] = (user, frame)
history_item.frame = frame
history_item.save()
frame_keys = {}
for history_item in History.objects.exclude(pattern=None):
tb = history_item.transaction_began
pattern = history_item.pattern
key = (tb, pattern)
user = history_item.user
if key in frame_keys:
frame_user, frame = frame_keys[key]
assert frame_user == user
else:
frame = HistoryFrame.objects.create(
transaction_began=tb,
pattern=pattern,
user=user)
frame_keys[key] = (user, frame)
history_item.frame = frame
history_item.save()
class Migration(migrations.Migration):
dependencies = [
('history', '0003_auto_20160122_1651'),
]
operations = [
migrations.RunPython(migrate_frames, lambda x, y: None),
]