Closes #19627: Object change migrators (#19628)

* 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:
Jeremy Stretch
2025-06-05 11:47:59 -04:00
committed by GitHub
parent bd8cf64ded
commit 179c06ec20
13 changed files with 261 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import django.db.models.deletion
from django.contrib.contenttypes.models import ContentType
from django.db import migrations, models
@@ -43,3 +44,20 @@ class Migration(migrations.Migration):
# Copy over existing site assignments
migrations.RunPython(code=copy_site_assignments, reverse_code=migrations.RunPython.noop),
]
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:
continue
if site_id := data.get('site'):
data.update({
'scope_type': site_ct,
'scope_id': site_id,
})
objectchange_migrators = {
'virtualization.cluster': oc_cluster_scope,
}

View File

@@ -87,3 +87,14 @@ class Migration(migrations.Migration):
),
),
]
def oc_cluster_remove_site(objectchange, reverting):
for data in (objectchange.prechange_data, objectchange.postchange_data):
if data is not None:
data.pop('site', None)
objectchange_migrators = {
'virtualization.cluster': oc_cluster_remove_site,
}

View File

@@ -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
@@ -50,3 +52,43 @@ class Migration(migrations.Migration):
name='mac_address',
),
]
# See peer migrator in dcim.0200_populate_mac_addresses before making changes
def oc_vminterface_primary_mac_address(objectchange, reverting):
MACAddress = apps.get_model('dcim', 'MACAddress')
vminterface_ct = ContentType.objects.get_by_natural_key('virtualization', 'vminterface')
# 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=vminterface_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=vminterface_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 = {
'virtualization.vminterface': oc_vminterface_primary_mac_address,
}