mirror of
https://github.com/netbox-community/netbox.git
synced 2026-02-05 14:56:24 -06:00
* Initial work on ObjectChange data migrations * Fix migration bug * Add migrators for MAC address assignments * Update reverting kwarg; allow pop() to fail * Cross-reference MAC address migrators * Split migrator logic across migrations * Add missing migrator
This commit is contained in:
@@ -100,3 +100,16 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def oc_rename_type(objectchange, reverting):
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if 'type' in data:
|
||||
data['form_factor'] = data.pop('type')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'dcim.rack': oc_rename_type,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import django.db.models.deletion
|
||||
from django.apps import apps
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@@ -15,7 +17,7 @@ def populate_mac_addresses(apps, schema_editor):
|
||||
assigned_object_type=interface_ct,
|
||||
assigned_object_id=interface.pk
|
||||
)
|
||||
for interface in Interface.objects.filter(mac_address__isnull=False)
|
||||
for interface in Interface.objects.using(db_alias).filter(mac_address__isnull=False)
|
||||
]
|
||||
MACAddress.objects.using(db_alias).bulk_create(mac_addresses, batch_size=100)
|
||||
|
||||
@@ -51,3 +53,43 @@ class Migration(migrations.Migration):
|
||||
name='mac_address',
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
# See peer migrator in virtualization.0048_populate_mac_addresses before making changes
|
||||
def oc_interface_primary_mac_address(objectchange, reverting):
|
||||
MACAddress = apps.get_model('dcim', 'MACAddress')
|
||||
interface_ct = ContentType.objects.get_by_natural_key('dcim', 'interface')
|
||||
|
||||
# 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
|
||||
|
||||
if after.get('mac_address') != before.get('mac_address'):
|
||||
# Create & assign the new MACAddress (if any)
|
||||
if after.get('mac_address'):
|
||||
mac = MACAddress.objects.create(
|
||||
mac_address=after['mac_address'],
|
||||
assigned_object_type=interface_ct,
|
||||
assigned_object_id=objectchange.changed_object_id,
|
||||
)
|
||||
after['primary_mac_address'] = mac.pk
|
||||
else:
|
||||
after['primary_mac_address'] = None
|
||||
# Delete the old MACAddress (if any)
|
||||
if before.get('mac_address'):
|
||||
MACAddress.objects.filter(
|
||||
mac_address=before['mac_address'],
|
||||
assigned_object_type=interface_ct,
|
||||
assigned_object_id=objectchange.changed_object_id,
|
||||
).delete()
|
||||
before['primary_mac_address'] = None
|
||||
|
||||
before.pop('mac_address', None)
|
||||
after.pop('mac_address', None)
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'dcim.interface': oc_interface_primary_mac_address,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user