mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-22 11:23:10 +00:00
Fix doublons proprement en migration
This commit is contained in:
parent
d1de4e365c
commit
c8ede78c66
1 changed files with 31 additions and 0 deletions
31
machines/migrations/0107_fix_lowercase_domain.py
Normal file
31
machines/migrations/0107_fix_lowercase_domain.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
|
def fix_duplicate(apps, schema_editor):
|
||||||
|
db_alias = schema_editor.connection.alias
|
||||||
|
domain = apps.get_model("machines", "Domain")
|
||||||
|
machines_to_fix = list(filter(lambda m : not m.name.islower(), domain.objects.using(db_alias).all()))
|
||||||
|
for machine in machines_to_fix:
|
||||||
|
try:
|
||||||
|
machine.name = machine.name.lower()
|
||||||
|
machine.validate_unique()
|
||||||
|
machine.clean()
|
||||||
|
except ValidationError:
|
||||||
|
machine.name = machine.name.lower() + str(machine.interface_parent.id)
|
||||||
|
machine.clean()
|
||||||
|
machine.save()
|
||||||
|
|
||||||
|
def unfix_duplicate(apps, schema_editor):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [("machines", "0106_auto_20191120_0159")]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(fix_duplicate, unfix_duplicate),
|
||||||
|
]
|
Loading…
Reference in a new issue