mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-23 01:08:45 +02: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:
@@ -1,4 +1,5 @@
|
||||
import django.db.models.deletion
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@@ -44,3 +45,20 @@ class Migration(migrations.Migration):
|
||||
# Copy over existing site assignments
|
||||
migrations.RunPython(code=copy_site_assignments, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
|
||||
|
||||
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:
|
||||
continue
|
||||
if site_id := data.get('site'):
|
||||
data.update({
|
||||
'scope_type': site_ct,
|
||||
'scope_id': site_id,
|
||||
})
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.prefix': oc_prefix_scope,
|
||||
}
|
||||
|
||||
@@ -60,3 +60,14 @@ class Migration(migrations.Migration):
|
||||
name='site',
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def oc_prefix_remove_fields(objectchange, reverting):
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is not None:
|
||||
data.pop('site', None)
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.prefix': oc_prefix_remove_fields,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations
|
||||
from django.db.models import F
|
||||
|
||||
@@ -54,3 +55,26 @@ class Migration(migrations.Migration):
|
||||
reverse_code=repopulate_device_and_virtualmachine_relations,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
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):
|
||||
if data is None:
|
||||
continue
|
||||
if device_id := data.get('device'):
|
||||
data.update({
|
||||
'parent_object_type': device_ct,
|
||||
'parent_object_id': device_id,
|
||||
})
|
||||
elif virtual_machine_id := data.get('virtual_machine'):
|
||||
data.update({
|
||||
'parent_object_type': virtual_machine_ct,
|
||||
'parent_object_id': virtual_machine_id,
|
||||
})
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.service': oc_service_parent,
|
||||
}
|
||||
|
||||
@@ -37,3 +37,15 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def oc_service_remove_fields(objectchange, reverting):
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is not None:
|
||||
data.pop('device', None)
|
||||
data.pop('virtual_machine', None)
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.service': oc_service_remove_fields,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user