mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-13 03:49:36 -06:00
Re-order migrations
This commit is contained in:
parent
912e6e4fb1
commit
955c64b68c
@ -7,7 +7,7 @@ from django.db import migrations, models
|
|||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
('ipam', '0081_remove_service_device_virtual_machine_add_parent_gfk_index'),
|
('ipam', '0082_add_prefix_network_containment_indexes'),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
@ -6,6 +6,7 @@ import time
|
|||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
from ipam.choices import PrefixStatusChoices
|
from ipam.choices import PrefixStatusChoices
|
||||||
|
from ipam.models import Prefix
|
||||||
|
|
||||||
|
|
||||||
def draw_progress(count, total, length=20):
|
def draw_progress(count, total, length=20):
|
||||||
@ -19,7 +20,7 @@ def draw_progress(count, total, length=20):
|
|||||||
sys.stdout.flush()
|
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()
|
start = time.time()
|
||||||
ChildModel = apps.get_model('ipam', model)
|
ChildModel = apps.get_model('ipam', model)
|
||||||
ParentModel = apps.get_model('ipam', parent_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:
|
if total == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
print('\r\n')
|
||||||
|
print(f'Migrating {parent_model}')
|
||||||
print('\r\n')
|
print('\r\n')
|
||||||
i = 0
|
i = 0
|
||||||
draw_progress(i, total, 50)
|
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_contains_or_equals=str(address_attr.ip),
|
||||||
prefix__net_mask_length__lte=address_attr.prefixlen,
|
prefix__net_mask_length__lte=address_attr.prefixlen,
|
||||||
)
|
)
|
||||||
if hasattr(ParentModel, 'vrf'):
|
|
||||||
prefixes = prefixes.filter(vrf=address.vrf)
|
setattr(address, parent_attr, prefixes.last())
|
||||||
address.prefix = prefixes.last()
|
try:
|
||||||
address.save()
|
address.save()
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error at {address}')
|
||||||
|
raise e
|
||||||
draw_progress(i, total, 50)
|
draw_progress(i, total, 50)
|
||||||
|
|
||||||
end = time.time()
|
end = time.time()
|
||||||
@ -68,7 +74,7 @@ def unset_iprange_prefix(apps, schema_editor):
|
|||||||
|
|
||||||
|
|
||||||
def set_prefix_aggregate(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):
|
def unset_prefix_aggregate(apps, schema_editor):
|
||||||
@ -118,7 +124,7 @@ def unset_prefix_parent(apps, schema_editor):
|
|||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
('ipam', '0082_ipaddress_iprange_prefix_parent'),
|
('ipam', '0083_ipaddress_iprange_prefix_parent'),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
|||||||
@ -82,18 +82,8 @@ def update_prefix_parents(prefix, delete=False):
|
|||||||
prefixes = prefix.children.all()
|
prefixes = prefix.children.all()
|
||||||
|
|
||||||
for pfx in prefixes:
|
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
|
# Set contained addresses to the containing prefix if it exists
|
||||||
pfx.parent = parent
|
pfx.parent = prefix.parent
|
||||||
else:
|
else:
|
||||||
# Get all possible addresses
|
# Get all possible addresses
|
||||||
prefixes = prefix.children.all() | Prefix.objects.filter(
|
prefixes = prefix.children.all() | Prefix.objects.filter(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user