Browse Source

#4721: Tweak migrations to ensure Interface.device cannot be null

Jeremy Stretch 5 years ago
parent
commit
6abb7e8f4d

+ 9 - 3
netbox/dcim/migrations/0109_interface_remove_vm.py

@@ -1,6 +1,5 @@
-# Generated by Django 3.0.6 on 2020-06-22 16:03
-
-from django.db import migrations
+from django.db import migrations, models
+import django.db.models.deletion
 
 
 class Migration(migrations.Migration):
@@ -15,4 +14,11 @@ class Migration(migrations.Migration):
             model_name='interface',
             name='virtual_machine',
         ),
+        # device is now a required field
+        migrations.AlterField(
+            model_name='interface',
+            name='device',
+            field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, related_name='interfaces', to='dcim.Device'),
+            preserve_default=False,
+        ),
     ]

+ 2 - 2
netbox/virtualization/migrations/0016_replicate_interfaces.py

@@ -51,8 +51,8 @@ def replicate_interfaces(apps, schema_editor):
     # Verify that all interfaces have been replicated
     assert replicated_count == original_interfaces.count(), "Replicated interfaces count does not match original count!"
 
-    # Delete original VM interfaces
-    original_interfaces.delete()
+    # Delete all interfaces not assigned to a Device
+    Interface.objects.filter(device__isnull=True).delete()
 
 
 class Migration(migrations.Migration):