diff --git a/HISTORY.rst b/HISTORY.rst index d76243e841..3fa582de1b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -177,6 +177,8 @@ - Move the file patching code from the Dependency class to a generalized utility of the storages app. - Add book link to the documentation. +- Update mayan_statistics migration 0002 to rename + duplicate slugs. 3.2.11 (2019-XX-XX) =================== 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',