Add data migrations to the sources app migraton 0019 to ensure all labels are unique before performing the schema migations.
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
3.1.8 (2018-10-XX)
|
||||
==================
|
||||
* Reorganize documentation into topics and chapters.
|
||||
* Add Workflows and API chapters.
|
||||
* Add new material from the Wiki to the documentation.
|
||||
* Add data migrations to the sources app migraton 0019 to ensure all labels
|
||||
are unique before performing the schema migations.
|
||||
|
||||
3.1.7 (2018-10-14)
|
||||
==================
|
||||
* Fix an issue with some browsers not firing the .load event on cached
|
||||
|
||||
@@ -5,6 +5,28 @@ from __future__ import unicode_literals
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def make_labels_unique(apps, schema_editor):
|
||||
Source = apps.get_model('sources', 'Source')
|
||||
|
||||
for source in Source.objects.using(schema_editor.connection.alias).all():
|
||||
# Look for sources with the same label
|
||||
duplicate_queryset = Source.objects.using(schema_editor.connection.alias).filter(label=source.label).exclude(pk=source.pk)
|
||||
if duplicate_queryset:
|
||||
# If a duplicate is found, append the id to the original source
|
||||
# label
|
||||
source.label = '{}__{}'.format(source.label, source.pk)
|
||||
source.save()
|
||||
|
||||
|
||||
def make_labels_unique_reverse(apps, schema_editor):
|
||||
Source = apps.get_model('sources', 'Source')
|
||||
|
||||
for source in Source.objects.using(schema_editor.connection.alias).all():
|
||||
if source.label.endswith('__{}'.format(source.pk)):
|
||||
source.label = source.label.replace('__{}'.format(source.pk), '')
|
||||
source.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
@@ -12,6 +34,9 @@ class Migration(migrations.Migration):
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
make_labels_unique, reverse_code=make_labels_unique_reverse
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='source',
|
||||
name='label',
|
||||
|
||||
Reference in New Issue
Block a user