mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-13 19:18:16 -06:00
Split migrations into DDL and data migrations
From https://docs.djangoproject.com/en/2.2/ref/migration-operations/: Thus, on PostgreSQL, for example, you should avoid combining schema changes and RunPython operations in the same migration or you may hit errors like OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events.
This commit is contained in:
parent
4ee391d27c
commit
d092f14afb
@ -0,0 +1,27 @@
|
||||
# Generated by Django 2.2.5 on 2019-10-06 19:01
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('dcim', '0075_cable_devices'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={
|
||||
'model__in': ['consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport',
|
||||
'rearport', 'circuittermination']
|
||||
}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
]
|
@ -1,62 +0,0 @@
|
||||
# Generated by Django 2.2.5 on 2019-10-06 19:01
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def connected_interface_to_endpoint(apps, schema_editor):
|
||||
Interface = apps.get_model('dcim', 'Interface')
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
|
||||
model_type = ContentType.objects.get_for_model(Interface)
|
||||
for interface in Interface.objects.exclude(_connected_interface=None):
|
||||
interface.connected_endpoint_type = model_type
|
||||
interface.connected_endpoint_id = interface._connected_interface.pk
|
||||
interface.save()
|
||||
|
||||
|
||||
def connected_circuittermination_to_endpoint(apps, schema_editor):
|
||||
Interface = apps.get_model('dcim', 'Interface')
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
CircuitTermination = apps.get_model('circuits', 'CircuitTermination')
|
||||
|
||||
model_type = ContentType.objects.get_for_model(CircuitTermination)
|
||||
for interface in Interface.objects.exclude(_connected_circuittermination=None):
|
||||
interface.connected_endpoint_type = model_type
|
||||
interface.connected_endpoint_id = interface._connected_circuittermination.pk
|
||||
interface.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('dcim', '0075_cable_devices'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={
|
||||
'model__in': ['consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport',
|
||||
'rearport', 'circuittermination']
|
||||
}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
|
||||
migrations.RunPython(connected_interface_to_endpoint),
|
||||
migrations.RunPython(connected_circuittermination_to_endpoint),
|
||||
|
||||
migrations.RemoveField(
|
||||
model_name='interface',
|
||||
name='_connected_circuittermination',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='interface',
|
||||
name='_connected_interface',
|
||||
),
|
||||
]
|
61
netbox/dcim/migrations/0077_migrate_connected_endpoint.py
Normal file
61
netbox/dcim/migrations/0077_migrate_connected_endpoint.py
Normal file
@ -0,0 +1,61 @@
|
||||
# Generated by Django 2.2.5 on 2019-10-06 19:01
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def connected_interface_to_endpoint(apps, schema_editor):
|
||||
interface_model = apps.get_model('dcim', 'Interface')
|
||||
contenttype_model = apps.get_model('contenttypes', 'ContentType')
|
||||
|
||||
model_type = contenttype_model.objects.get_for_model(interface_model)
|
||||
for interface in interface_model.objects.exclude(_connected_interface=None):
|
||||
interface.connected_endpoint_type = model_type
|
||||
interface.connected_endpoint_id = interface._connected_interface.pk
|
||||
interface.save()
|
||||
|
||||
|
||||
def connected_endpoint_to_interface(apps, schema_editor):
|
||||
interface_model = apps.get_model('dcim', 'Interface')
|
||||
contenttype_model = apps.get_model('contenttypes', 'ContentType')
|
||||
|
||||
model_type = contenttype_model.objects.get_for_model(interface_model)
|
||||
for interface in interface_model.objects.filter(connected_endpoint_type=model_type):
|
||||
interface._connected_interface = interface.connected_endpoint
|
||||
interface.save()
|
||||
|
||||
|
||||
def connected_circuittermination_to_endpoint(apps, schema_editor):
|
||||
interface_model = apps.get_model('dcim', 'Interface')
|
||||
contenttype_model = apps.get_model('contenttypes', 'ContentType')
|
||||
circuittermination_model = apps.get_model('circuits', 'CircuitTermination')
|
||||
|
||||
model_type = contenttype_model.objects.get_for_model(circuittermination_model)
|
||||
for interface in interface_model.objects.exclude(_connected_circuittermination=None):
|
||||
interface.connected_endpoint_type = model_type
|
||||
interface.connected_endpoint_id = interface._connected_circuittermination.pk
|
||||
interface.save()
|
||||
|
||||
|
||||
def connected_endpoint_to_circuittermination(apps, schema_editor):
|
||||
interface_model = apps.get_model('dcim', 'Interface')
|
||||
contenttype_model = apps.get_model('contenttypes', 'ContentType')
|
||||
circuittermination_model = apps.get_model('circuits', 'CircuitTermination')
|
||||
|
||||
model_type = contenttype_model.objects.get_for_model(circuittermination_model)
|
||||
for interface in interface_model.objects.filter(connected_endpoint_type=model_type):
|
||||
interface._connected_circuittermination = interface.connected_endpoint
|
||||
interface.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('dcim', '0076_add_generic_connected_endpoint'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(connected_interface_to_endpoint,
|
||||
connected_endpoint_to_interface),
|
||||
migrations.RunPython(connected_circuittermination_to_endpoint,
|
||||
connected_endpoint_to_circuittermination),
|
||||
]
|
@ -0,0 +1,21 @@
|
||||
# Generated by Django 2.2.5 on 2019-10-06 19:01
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('dcim', '0077_migrate_connected_endpoint'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='interface',
|
||||
name='_connected_circuittermination',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='interface',
|
||||
name='_connected_interface',
|
||||
),
|
||||
]
|
Loading…
Reference in New Issue
Block a user