Re-order migrations

This commit is contained in:
Daniel Sheppard 2025-09-14 10:49:44 -05:00
parent 912e6e4fb1
commit 955c64b68c
3 changed files with 15 additions and 19 deletions

View File

@ -7,7 +7,7 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ipam', '0081_remove_service_device_virtual_machine_add_parent_gfk_index'),
('ipam', '0082_add_prefix_network_containment_indexes'),
]
operations = [

View File

@ -6,6 +6,7 @@ import time
from django.db import migrations, models
from ipam.choices import PrefixStatusChoices
from ipam.models import Prefix
def draw_progress(count, total, length=20):
@ -19,7 +20,7 @@ def draw_progress(count, total, length=20):
sys.stdout.flush()
def set_prefix(apps, schema_editor, model, attr='address', parent_model='Prefix'):
def set_prefix(apps, schema_editor, model, attr='address', parent_attr='prefix', parent_model='Prefix'):
start = time.time()
ChildModel = apps.get_model('ipam', model)
ParentModel = apps.get_model('ipam', parent_model)
@ -29,6 +30,8 @@ def set_prefix(apps, schema_editor, model, attr='address', parent_model='Prefix'
if total == 0:
return
print('\r\n')
print(f'Migrating {parent_model}')
print('\r\n')
i = 0
draw_progress(i, total, 50)
@ -39,10 +42,13 @@ def set_prefix(apps, schema_editor, model, attr='address', parent_model='Prefix'
prefix__net_contains_or_equals=str(address_attr.ip),
prefix__net_mask_length__lte=address_attr.prefixlen,
)
if hasattr(ParentModel, 'vrf'):
prefixes = prefixes.filter(vrf=address.vrf)
address.prefix = prefixes.last()
address.save()
setattr(address, parent_attr, prefixes.last())
try:
address.save()
except Exception as e:
print(f'Error at {address}')
raise e
draw_progress(i, total, 50)
end = time.time()
@ -68,7 +74,7 @@ def unset_iprange_prefix(apps, schema_editor):
def set_prefix_aggregate(apps, schema_editor):
set_prefix(apps, schema_editor, 'Prefix', 'prefix', 'Aggregate')
set_prefix(apps, schema_editor, 'Prefix', 'prefix', 'aggregate', 'Aggregate')
def unset_prefix_aggregate(apps, schema_editor):
@ -118,7 +124,7 @@ def unset_prefix_parent(apps, schema_editor):
class Migration(migrations.Migration):
dependencies = [
('ipam', '0082_ipaddress_iprange_prefix_parent'),
('ipam', '0083_ipaddress_iprange_prefix_parent'),
]
operations = [

View File

@ -82,18 +82,8 @@ def update_prefix_parents(prefix, delete=False):
prefixes = prefix.children.all()
for pfx in prefixes:
parent = Prefix.objects.exclude(pk=pfx.pk).exclude(pk=prefix.pk).filter(
Q(
vrf=pfx.vrf,
prefix__net_contains=str(pfx.prefix)
) | Q(
vrf=None,
status=PrefixStatusChoices.STATUS_CONTAINER,
prefix__net_contains=str(pfx.prefix),
)
).last()
# Set contained addresses to the containing prefix if it exists
pfx.parent = parent
pfx.parent = prefix.parent
else:
# Get all possible addresses
prefixes = prefix.children.all() | Prefix.objects.filter(