Renumbered and updated the migrations merged in from the eventsaving work. Made the dochistory-adding migration idempotent, and added progress indication as this migration may take as much as an hour to run.

- Legacy-Id: 11854
This commit is contained in:
Henrik Levkowetz 2016-08-24 15:37:25 +00:00
parent d868371aff
commit cf5ac68b09
3 changed files with 29 additions and 8 deletions

View file

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
from django.db import migrations
def fix_buggy_author_foreignkey(apps, schema_editor):
@ -17,6 +19,10 @@ def save_all_documents_in_history(apps, schema_editor):
DocumentAuthor = apps.get_model("doc", "DocumentAuthor")
DocHistoryAuthor = apps.get_model("doc", "DocHistoryAuthor")
sys.stderr.write('\n'
' Ensuring that all documents have document history entries.\n'
' This could take as much as an hour to run.\n')
def canonical_name(self):
name = self.name
state = State.objects.filter(document=self, type_id=self.type_id).first()
@ -38,6 +44,8 @@ def save_all_documents_in_history(apps, schema_editor):
def save_document_in_history(doc):
"""Save a snapshot of document and related objects in the database."""
def get_model_fields_as_dict(obj):
return dict((field.name, getattr(obj, field.name))
for field in obj._meta.fields
@ -48,8 +56,21 @@ def save_all_documents_in_history(apps, schema_editor):
fields["doc"] = doc
fields["name"] = canonical_name(doc)
dochist = DocHistory(**fields)
dochist.save()
objs = DocHistory.objects.filter(**fields)
if objs.exists():
try:
dochist = objs.get(**fields)
sys.stderr.write('.')
except DocHistory.MultipleObjectsReturned:
dochist_list = list(objs)
for dochist in dochist_list[1:]:
dochist.delete()
dochist = dochist_list[0]
sys.stderr.write('-')
else:
dochist = DocHistory(**fields)
dochist.save()
sys.stderr.write('+')
# copy many to many
for field in doc._meta.many_to_many:
@ -82,8 +103,8 @@ def save_all_documents_in_history(apps, schema_editor):
class Migration(migrations.Migration):
dependencies = [
('doc', '0010_auto_20150930_0251'),
('group', '0007_auto_20150930_0758'),
('doc', '0012_auto_20160207_0537'),
('group', '0009_auto_20150930_0758'),
]
operations = [

View file

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os, datetime
@ -108,7 +108,7 @@ class Migration(migrations.Migration):
dependencies = [
('doc', '0010_auto_20150930_0251'),
('group', '0006_auto_20150718_0509'),
('group', '0008_auto_20160505_0523'),
]
operations = [