From 3183f376bd74b4a31e58686df87f44ce9e200208 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 15 Nov 2024 13:05:15 -0500 Subject: [PATCH] Clean up migrations --- netbox/dcim/migrations/0199_macaddress.py | 45 +------------------ .../0200_interface_remove_mac_address.py | 15 ------- .../migrations/0200_populate_mac_addresses.py | 42 +++++++++++++++++ .../0047_vminterface_rename_mac_address.py | 27 +++++++++++ .../0048_vminterface_remove__mac_address.py | 15 ------- 5 files changed, 71 insertions(+), 73 deletions(-) delete mode 100644 netbox/dcim/migrations/0200_interface_remove_mac_address.py create mode 100644 netbox/dcim/migrations/0200_populate_mac_addresses.py delete mode 100644 netbox/virtualization/migrations/0048_vminterface_remove__mac_address.py diff --git a/netbox/dcim/migrations/0199_macaddress.py b/netbox/dcim/migrations/0199_macaddress.py index 5e27fa184..f9cc6a8df 100644 --- a/netbox/dcim/migrations/0199_macaddress.py +++ b/netbox/dcim/migrations/0199_macaddress.py @@ -1,40 +1,9 @@ -import dcim.fields import django.db.models.deletion import taggit.managers -import utilities.json from django.db import migrations, models - -def populate_macaddress_objects(apps, schema_editor): - Interface = apps.get_model('dcim', 'Interface') - VMInterface = apps.get_model('virtualization', 'VMInterface') - MACAddress = apps.get_model('dcim', 'MACAddress') - ContentType = apps.get_model('contenttypes', 'ContentType') - interface_ct = ContentType.objects.get_for_model(Interface) - vminterface_ct = ContentType.objects.get_for_model(VMInterface) - mac_addresses = [] - print() - print('Converting MAC addresses...') - for interface in Interface.objects.filter(_mac_address__isnull=False): - mac_addresses.append( - MACAddress( - mac_address=interface._mac_address, - assigned_object_type=interface_ct, - assigned_object_id=interface.id, - # interface=interface, - ) - ) - for vminterface in VMInterface.objects.filter(_mac_address__isnull=False): - mac_addresses.append( - MACAddress( - mac_address=vminterface._mac_address, - assigned_object_type=vminterface_ct, - assigned_object_id=vminterface.id, - # vm_interface=vm_interface, - ) - ) - MACAddress.objects.bulk_create(mac_addresses) - print(f'Created {len(mac_addresses)} MAC address objects.') +import dcim.fields +import utilities.json class Migration(migrations.Migration): @@ -42,15 +11,9 @@ class Migration(migrations.Migration): dependencies = [ ('dcim', '0198_natural_ordering'), ('extras', '0122_charfield_null_choices'), - ('virtualization', '0047_vminterface_rename_mac_address'), ] operations = [ - migrations.RenameField( - model_name='interface', - old_name='mac_address', - new_name='_mac_address', - ), migrations.CreateModel( name='MACAddress', fields=[ @@ -71,8 +34,4 @@ class Migration(migrations.Migration): 'ordering': ('mac_address',) }, ), - migrations.RunPython( - code=populate_macaddress_objects, - reverse_code=migrations.RunPython.noop - ) ] diff --git a/netbox/dcim/migrations/0200_interface_remove_mac_address.py b/netbox/dcim/migrations/0200_interface_remove_mac_address.py deleted file mode 100644 index 1f6c87e1a..000000000 --- a/netbox/dcim/migrations/0200_interface_remove_mac_address.py +++ /dev/null @@ -1,15 +0,0 @@ -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('dcim', '0199_macaddress'), - ] - - operations = [ - migrations.RemoveField( - model_name='interface', - name='_mac_address', - ), - ] diff --git a/netbox/dcim/migrations/0200_populate_mac_addresses.py b/netbox/dcim/migrations/0200_populate_mac_addresses.py new file mode 100644 index 000000000..98bf83f5c --- /dev/null +++ b/netbox/dcim/migrations/0200_populate_mac_addresses.py @@ -0,0 +1,42 @@ +from django.db import migrations + + +def populate_mac_addresses(apps, schema_editor): + ContentType = apps.get_model('contenttypes', 'ContentType') + Interface = apps.get_model('dcim', 'Interface') + MACAddress = apps.get_model('dcim', 'MACAddress') + interface_ct = ContentType.objects.get_for_model(Interface) + + mac_addresses = [ + MACAddress( + mac_address=interface._mac_address, + assigned_object_type=interface_ct, + assigned_object_id=interface.pk + ) + for interface in Interface.objects.filter(_mac_address__isnull=False) + ] + MACAddress.objects.bulk_create(mac_addresses, batch_size=100) + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0199_macaddress'), + ] + + operations = [ + # Rename mac_address field to avoid conflict with property + migrations.RenameField( + model_name='interface', + old_name='mac_address', + new_name='_mac_address', + ), + migrations.RunPython( + code=populate_mac_addresses, + reverse_code=migrations.RunPython.noop + ), + migrations.RemoveField( + model_name='interface', + name='_mac_address', + ), + ] diff --git a/netbox/virtualization/migrations/0047_vminterface_rename_mac_address.py b/netbox/virtualization/migrations/0047_vminterface_rename_mac_address.py index bfbe33d61..092b9eb50 100644 --- a/netbox/virtualization/migrations/0047_vminterface_rename_mac_address.py +++ b/netbox/virtualization/migrations/0047_vminterface_rename_mac_address.py @@ -1,16 +1,43 @@ from django.db import migrations +def populate_mac_addresses(apps, schema_editor): + ContentType = apps.get_model('contenttypes', 'ContentType') + VMInterface = apps.get_model('virtualization', 'VMInterface') + MACAddress = apps.get_model('dcim', 'MACAddress') + vminterface_ct = ContentType.objects.get_for_model(VMInterface) + + mac_addresses = [ + MACAddress( + mac_address=vminterface._mac_address, + assigned_object_type=vminterface_ct, + assigned_object_id=vminterface.pk + ) + for vminterface in VMInterface.objects.filter(_mac_address__isnull=False) + ] + MACAddress.objects.bulk_create(mac_addresses, batch_size=100) + + class Migration(migrations.Migration): dependencies = [ + ('dcim', '0199_macaddress'), ('virtualization', '0046_natural_ordering'), ] operations = [ + # Rename mac_address field to avoid conflict with property migrations.RenameField( model_name='vminterface', old_name='mac_address', new_name='_mac_address', ), + migrations.RunPython( + code=populate_mac_addresses, + reverse_code=migrations.RunPython.noop + ), + migrations.RemoveField( + model_name='vminterface', + name='_mac_address', + ), ] diff --git a/netbox/virtualization/migrations/0048_vminterface_remove__mac_address.py b/netbox/virtualization/migrations/0048_vminterface_remove__mac_address.py deleted file mode 100644 index bfa0e09df..000000000 --- a/netbox/virtualization/migrations/0048_vminterface_remove__mac_address.py +++ /dev/null @@ -1,15 +0,0 @@ -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('virtualization', '0047_vminterface_rename_mac_address'), - ] - - operations = [ - migrations.RemoveField( - model_name='vminterface', - name='_mac_address', - ), - ]