From 6d942272247d60146a472e923228d7234e157d2f Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 22 Nov 2019 21:23:35 -0400 Subject: [PATCH] Update statistics migration to rename duplicates Signed-off-by: Roberto Rosario --- HISTORY.rst | 2 ++ .../migrations/0002_auto_20191116_0236.py | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 18f024ff03..73b9b0f627 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -12,6 +12,8 @@ - Self-heal statistics results model when multiple results are created using the same slug value. Forum topic 1404. +- Update mayan_statistics migration 0002 to rename + duplicate slugs. 3.2.10 (2019-11-19) =================== diff --git a/mayan/apps/mayan_statistics/migrations/0002_auto_20191116_0236.py b/mayan/apps/mayan_statistics/migrations/0002_auto_20191116_0236.py index 132fb0f448..7970a2f630 100644 --- a/mayan/apps/mayan_statistics/migrations/0002_auto_20191116_0236.py +++ b/mayan/apps/mayan_statistics/migrations/0002_auto_20191116_0236.py @@ -1,10 +1,30 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.25 on 2019-11-16 02:36 from __future__ import unicode_literals from django.db import migrations, models +def operation_rename_duplicates(apps, schema_editor): + StatisticResult = apps.get_model( + app_label='mayan_statistics', model_name='StatisticResult' + ) + slugs = [] + + for entry in StatisticResult.objects.using(schema_editor.connection.alias).all(): + if entry.slug in slugs: + counter = 1 + while(True): + attempt = '{}_{}'.format(entry.slug, counter) + if attempt not in slugs: + break + else: + counter = counter + 1 + + entry.slug = attempt + entry.save() + + slugs.append(entry.slug) + + class Migration(migrations.Migration): dependencies = [ @@ -12,6 +32,10 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython( + code=operation_rename_duplicates, + reverse_code=migrations.RunPython.noop + ), migrations.AlterField( model_name='statisticresult', name='slug',