mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-21 16:31:38 +02:00
Fixes #19440: Ensure data migrations use the correct database connection
This commit is contained in:
@@ -11,7 +11,9 @@ def set_vid_ranges(apps, schema_editor):
|
||||
Convert the min_vid & max_vid fields to a range in the new vid_ranges ArrayField.
|
||||
"""
|
||||
VLANGroup = apps.get_model('ipam', 'VLANGroup')
|
||||
for group in VLANGroup.objects.all():
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
for group in VLANGroup.objects.using(db_alias).all():
|
||||
group.vid_ranges = [NumericRange(group.min_vid, group.max_vid, bounds='[]')]
|
||||
group._total_vlan_ids = group.max_vid - group.min_vid + 1
|
||||
group.save()
|
||||
|
||||
@@ -9,9 +9,11 @@ def copy_site_assignments(apps, schema_editor):
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Prefix = apps.get_model('ipam', 'Prefix')
|
||||
Site = apps.get_model('dcim', 'Site')
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
Prefix.objects.filter(site__isnull=False).update(
|
||||
scope_type=ContentType.objects.get_for_model(Site), scope_id=models.F('site_id')
|
||||
Prefix.objects.using(db_alias).filter(site__isnull=False).update(
|
||||
scope_type=ContentType.objects.get_for_model(Site),
|
||||
scope_id=models.F('site_id')
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -7,15 +7,16 @@ def populate_denormalized_fields(apps, schema_editor):
|
||||
Copy site ForeignKey values to the scope GFK.
|
||||
"""
|
||||
Prefix = apps.get_model('ipam', 'Prefix')
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
prefixes = Prefix.objects.filter(site__isnull=False).prefetch_related('site')
|
||||
prefixes = Prefix.objects.using(db_alias).filter(site__isnull=False).prefetch_related('site')
|
||||
for prefix in prefixes:
|
||||
prefix._region_id = prefix.site.region_id
|
||||
prefix._site_group_id = prefix.site.group_id
|
||||
prefix._site_id = prefix.site_id
|
||||
# Note: Location cannot be set prior to migration
|
||||
|
||||
Prefix.objects.bulk_update(prefixes, ['_region', '_site_group', '_site'], batch_size=100)
|
||||
Prefix.objects.using(db_alias).bulk_update(prefixes, ['_region', '_site_group', '_site'], batch_size=100)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
@@ -7,9 +7,10 @@ def set_null_values(apps, schema_editor):
|
||||
"""
|
||||
FHRPGroup = apps.get_model('ipam', 'FHRPGroup')
|
||||
IPAddress = apps.get_model('ipam', 'IPAddress')
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
FHRPGroup.objects.filter(auth_type='').update(auth_type=None)
|
||||
IPAddress.objects.filter(role='').update(role=None)
|
||||
FHRPGroup.objects.using(db_alias).filter(auth_type='').update(auth_type=None)
|
||||
IPAddress.objects.using(db_alias).filter(role='').update(role=None)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
@@ -2,36 +2,38 @@ from django.db import migrations
|
||||
from django.db.models import F
|
||||
|
||||
|
||||
def populate_service_parent_gfk(apps, schema_config):
|
||||
def populate_service_parent_gfk(apps, schema_editor):
|
||||
Service = apps.get_model('ipam', 'Service')
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Device = apps.get_model('dcim', 'device')
|
||||
VirtualMachine = apps.get_model('virtualization', 'virtualmachine')
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
Service.objects.filter(device_id__isnull=False).update(
|
||||
Service.objects.using(db_alias).filter(device_id__isnull=False).update(
|
||||
parent_object_type=ContentType.objects.get_for_model(Device),
|
||||
parent_object_id=F('device_id'),
|
||||
)
|
||||
|
||||
Service.objects.filter(virtual_machine_id__isnull=False).update(
|
||||
Service.objects.using(db_alias).filter(virtual_machine_id__isnull=False).update(
|
||||
parent_object_type=ContentType.objects.get_for_model(VirtualMachine),
|
||||
parent_object_id=F('virtual_machine_id'),
|
||||
)
|
||||
|
||||
|
||||
def repopulate_device_and_virtualmachine_relations(apps, schemaconfig):
|
||||
def repopulate_device_and_virtualmachine_relations(apps, schema_editor):
|
||||
Service = apps.get_model('ipam', 'Service')
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Device = apps.get_model('dcim', 'device')
|
||||
VirtualMachine = apps.get_model('virtualization', 'virtualmachine')
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
Service.objects.filter(
|
||||
Service.objects.using(db_alias).filter(
|
||||
parent_object_type=ContentType.objects.get_for_model(Device),
|
||||
).update(
|
||||
device_id=F('parent_object_id')
|
||||
)
|
||||
|
||||
Service.objects.filter(
|
||||
Service.objects.using(db_alias).filter(
|
||||
parent_object_type=ContentType.objects.get_for_model(VirtualMachine),
|
||||
).update(
|
||||
virtual_machine_id=F('parent_object_id')
|
||||
|
||||
Reference in New Issue
Block a user