2021-06-02 16:02:38 -04:00
|
|
|
import sys
|
2021-05-24 16:51:05 -04:00
|
|
|
from django.db import migrations
|
|
|
|
|
2021-05-27 09:24:29 -04:00
|
|
|
from ipam.utils import rebuild_prefixes
|
|
|
|
|
2021-05-24 16:51:05 -04:00
|
|
|
|
|
|
|
def populate_prefix_hierarchy(apps, schema_editor):
|
|
|
|
"""
|
|
|
|
Populate _depth and _children attrs for all Prefixes.
|
|
|
|
"""
|
|
|
|
Prefix = apps.get_model('ipam', 'Prefix')
|
|
|
|
VRF = apps.get_model('ipam', 'VRF')
|
|
|
|
|
|
|
|
total_count = Prefix.objects.count()
|
2021-06-02 16:02:38 -04:00
|
|
|
if 'test' not in sys.argv:
|
|
|
|
print(f'\nUpdating {total_count} prefixes...')
|
2021-05-24 16:51:05 -04:00
|
|
|
|
2021-05-27 09:24:29 -04:00
|
|
|
# Rebuild the global table
|
|
|
|
rebuild_prefixes(None)
|
2021-05-24 16:51:05 -04:00
|
|
|
|
2021-05-27 09:24:29 -04:00
|
|
|
# Iterate through all VRFs, rebuilding each
|
|
|
|
for vrf in VRF.objects.all():
|
2021-06-04 10:53:13 -04:00
|
|
|
rebuild_prefixes(vrf.pk)
|
2021-05-24 16:51:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('ipam', '0047_prefix_depth_children'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(
|
|
|
|
code=populate_prefix_hierarchy,
|
|
|
|
reverse_code=migrations.RunPython.noop
|
|
|
|
),
|
|
|
|
]
|