From 5a0d33a4a819590cfc12104fe09061b471f03df6 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 2 Jun 2025 09:45:44 -0400 Subject: [PATCH] Update reverting kwarg; allow pop() to fail --- .../migrations/0047_circuittermination__termination.py | 6 +++--- .../migrations/0051_virtualcircuit_group_assignment.py | 4 ++-- netbox/dcim/migrations/0200_populate_mac_addresses.py | 9 +++++---- netbox/ipam/migrations/0071_prefix_scope.py | 4 ++-- netbox/ipam/migrations/0080_populate_service_parent.py | 6 +++--- netbox/tenancy/migrations/0018_contact_groups.py | 8 ++++---- netbox/virtualization/migrations/0044_cluster_scope.py | 4 ++-- .../migrations/0048_populate_mac_addresses.py | 9 +++++---- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/netbox/circuits/migrations/0047_circuittermination__termination.py b/netbox/circuits/migrations/0047_circuittermination__termination.py index 546450ee7..6a6d08502 100644 --- a/netbox/circuits/migrations/0047_circuittermination__termination.py +++ b/netbox/circuits/migrations/0047_circuittermination__termination.py @@ -52,7 +52,7 @@ class Migration(migrations.Migration): ] -def oc_circuittermination_termination(objectchange, revert): +def oc_circuittermination_termination(objectchange, reverting): site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk provider_network_ct = ContentType.objects.get_by_natural_key('circuits', 'providernetwork').pk for data in (objectchange.prechange_data, objectchange.postchange_data): @@ -68,8 +68,8 @@ def oc_circuittermination_termination(objectchange, revert): 'termination_type': provider_network_ct, 'termination_id': provider_network_id, }) - data.pop('site') - data.pop('provider_network') + data.pop('site', None) + data.pop('provider_network', None) objectchange_migrators = { diff --git a/netbox/circuits/migrations/0051_virtualcircuit_group_assignment.py b/netbox/circuits/migrations/0051_virtualcircuit_group_assignment.py index 448a1e625..50f5fd5a6 100644 --- a/netbox/circuits/migrations/0051_virtualcircuit_group_assignment.py +++ b/netbox/circuits/migrations/0051_virtualcircuit_group_assignment.py @@ -85,7 +85,7 @@ class Migration(migrations.Migration): ] -def oc_circuitgroupassignment_member(objectchange, revert): +def oc_circuitgroupassignment_member(objectchange, reverting): circuit_ct = ContentType.objects.get_by_natural_key('circuits', 'circuit').pk for data in (objectchange.prechange_data, objectchange.postchange_data): if data is None: @@ -95,7 +95,7 @@ def oc_circuitgroupassignment_member(objectchange, revert): 'member_type': circuit_ct, 'member_id': circuit_id, }) - data.pop('circuit') + data.pop('circuit', None) objectchange_migrators = { diff --git a/netbox/dcim/migrations/0200_populate_mac_addresses.py b/netbox/dcim/migrations/0200_populate_mac_addresses.py index 48d2c746b..1b548b29c 100644 --- a/netbox/dcim/migrations/0200_populate_mac_addresses.py +++ b/netbox/dcim/migrations/0200_populate_mac_addresses.py @@ -55,11 +55,12 @@ class Migration(migrations.Migration): ] -def oc_interface_primary_mac_address(objectchange, revert): +def oc_interface_primary_mac_address(objectchange, reverting): MACAddress = apps.get_model('dcim', 'MACAddress') interface_ct = ContentType.objects.get_by_natural_key('dcim', 'interface') - if not revert: + # Swap data order if the change is being reverted + if not reverting: before, after = objectchange.prechange_data, objectchange.postchange_data else: before, after = objectchange.postchange_data, objectchange.prechange_data @@ -84,8 +85,8 @@ def oc_interface_primary_mac_address(objectchange, revert): ).delete() before['primary_mac_address'] = None - before.pop('mac_address') - after.pop('mac_address') + before.pop('mac_address', None) + after.pop('mac_address', None) objectchange_migrators = { diff --git a/netbox/ipam/migrations/0071_prefix_scope.py b/netbox/ipam/migrations/0071_prefix_scope.py index d7c2c2fcd..af3659303 100644 --- a/netbox/ipam/migrations/0071_prefix_scope.py +++ b/netbox/ipam/migrations/0071_prefix_scope.py @@ -47,7 +47,7 @@ class Migration(migrations.Migration): ] -def oc_prefix_scope(objectchange, revert): +def oc_prefix_scope(objectchange, reverting): site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk for data in (objectchange.prechange_data, objectchange.postchange_data): if data is None: @@ -57,7 +57,7 @@ def oc_prefix_scope(objectchange, revert): 'scope_type': site_ct, 'scope_id': site_id, }) - data.pop('site') + data.pop('site', None) objectchange_migrators = { diff --git a/netbox/ipam/migrations/0080_populate_service_parent.py b/netbox/ipam/migrations/0080_populate_service_parent.py index 62f13df18..df4e03af7 100644 --- a/netbox/ipam/migrations/0080_populate_service_parent.py +++ b/netbox/ipam/migrations/0080_populate_service_parent.py @@ -57,7 +57,7 @@ class Migration(migrations.Migration): ] -def oc_service_parent(objectchange, revert): +def oc_service_parent(objectchange, reverting): device_ct = ContentType.objects.get_by_natural_key('dcim', 'device').pk virtual_machine_ct = ContentType.objects.get_by_natural_key('virtualization', 'virtualmachine').pk for data in (objectchange.prechange_data, objectchange.postchange_data): @@ -73,8 +73,8 @@ def oc_service_parent(objectchange, revert): 'parent_object_type': virtual_machine_ct, 'parent_object_id': virtual_machine_id, }) - data.pop('device') - data.pop('virtual_machine') + data.pop('device', None) + data.pop('virtual_machine', None) objectchange_migrators = { diff --git a/netbox/tenancy/migrations/0018_contact_groups.py b/netbox/tenancy/migrations/0018_contact_groups.py index 72d2d9505..7f21190f5 100644 --- a/netbox/tenancy/migrations/0018_contact_groups.py +++ b/netbox/tenancy/migrations/0018_contact_groups.py @@ -68,13 +68,13 @@ class Migration(migrations.Migration): ] -def oc_contact_groups(objectchange, revert): +def oc_contact_groups(objectchange, reverting): for data in (objectchange.prechange_data, objectchange.postchange_data): if data is None: continue - if 'group' in data: - data['groups'] = [data['group']] if data['group'] else [] - data.pop('group') + # Set the M2M field `groups` to a list containing the group ID + data['groups'] = [data['group']] if data.get('group') else [] + data.pop('group', None) objectchange_migrators = { diff --git a/netbox/virtualization/migrations/0044_cluster_scope.py b/netbox/virtualization/migrations/0044_cluster_scope.py index 03d6481ff..7664d34f0 100644 --- a/netbox/virtualization/migrations/0044_cluster_scope.py +++ b/netbox/virtualization/migrations/0044_cluster_scope.py @@ -46,7 +46,7 @@ class Migration(migrations.Migration): ] -def oc_cluster_scope(objectchange, revert): +def oc_cluster_scope(objectchange, reverting): site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk for data in (objectchange.prechange_data, objectchange.postchange_data): if data is None: @@ -56,7 +56,7 @@ def oc_cluster_scope(objectchange, revert): 'scope_type': site_ct, 'scope_id': site_id, }) - data.pop('site') + data.pop('site', None) objectchange_migrators = { diff --git a/netbox/virtualization/migrations/0048_populate_mac_addresses.py b/netbox/virtualization/migrations/0048_populate_mac_addresses.py index 6de748fe8..c3aefdc68 100644 --- a/netbox/virtualization/migrations/0048_populate_mac_addresses.py +++ b/netbox/virtualization/migrations/0048_populate_mac_addresses.py @@ -54,11 +54,12 @@ class Migration(migrations.Migration): ] -def oc_vminterface_primary_mac_address(objectchange, revert): +def oc_vminterface_primary_mac_address(objectchange, reverting): MACAddress = apps.get_model('dcim', 'MACAddress') vminterface_ct = ContentType.objects.get_by_natural_key('virtualization', 'vminterface') - if not revert: + # Swap data order if the change is being reverted + if not reverting: before, after = objectchange.prechange_data, objectchange.postchange_data else: before, after = objectchange.postchange_data, objectchange.prechange_data @@ -83,8 +84,8 @@ def oc_vminterface_primary_mac_address(objectchange, revert): ).delete() before['primary_mac_address'] = None - before.pop('mac_address') - after.pop('mac_address') + before.pop('mac_address', None) + after.pop('mac_address', None) objectchange_migrators = {