diff options
Diffstat (limited to 'Post/migrations')
-rw-r--r-- | Post/migrations/0003_auto_20150603_0913.py | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/Post/migrations/0003_auto_20150603_0913.py b/Post/migrations/0003_auto_20150603_0913.py index 46729ee..321f09f 100644 --- a/Post/migrations/0003_auto_20150603_0913.py +++ b/Post/migrations/0003_auto_20150603_0913.py @@ -3,10 +3,47 @@ from __future__ import unicode_literals from django.db import models, migrations +import Levenshtein + def add_lev_distance_data(apps, schema_editor): + BuildFailure = apps.get_model("Post", "BuildFailure") - for buildfail in BuildFailure.objects.all(): - buildfailure.save(recalc_lev_distance=True) + + # copied from model see + # https://docs.djangoproject.com/en/1.8/topics/migrations/#historical-models + + def calc_lev_distance(obj): + if BuildFailure.objects.all().count() == 0: + return 0 + + # Use the last 400 characters of the ERROR_DETAILS. + # This is where the error message is likely to occour and + # reduces the computational load on calculating the Levenshtein + # distance. + seed = BuildFailure.objects.first().ERROR_DETAILS[-400:] + lv = Levenshtein.distance(str(seed), str(obj.ERROR_DETAILS[-400:])) + + # Offset the distance calculated against the length of the error. + return lv + len (obj.ERROR_DETAILS) + + + offset = 0 + pagesize = 1000 + count = BuildFailure.objects.all().count() + + while offset < count: + objs = BuildFailure.objects.all()[offset : offset + pagesize] + for f in objs: + if f.LEV_DISTANCE is None: + f.LEV_DISTANCE = calc_lev_distance(f) + f.save() + + del objs + offset = offset + pagesize + +def remove_lev_distance_data(apps, schema_editor): + # Nothing to do at this point + pass class Migration(migrations.Migration): @@ -20,5 +57,6 @@ class Migration(migrations.Migration): name='LEV_DISTANCE', field=models.IntegerField(null=True, blank=True), preserve_default=True, - ), + ), + migrations.RunPython(add_lev_distance_data, remove_lev_distance_data), ] |