mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-29 11:56:25 -06:00
Clean up migrations
This commit is contained in:
parent
1a1005218c
commit
3183f376bd
@ -1,40 +1,9 @@
|
|||||||
import dcim.fields
|
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
import taggit.managers
|
import taggit.managers
|
||||||
import utilities.json
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
import dcim.fields
|
||||||
def populate_macaddress_objects(apps, schema_editor):
|
import utilities.json
|
||||||
Interface = apps.get_model('dcim', 'Interface')
|
|
||||||
VMInterface = apps.get_model('virtualization', 'VMInterface')
|
|
||||||
MACAddress = apps.get_model('dcim', 'MACAddress')
|
|
||||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
||||||
interface_ct = ContentType.objects.get_for_model(Interface)
|
|
||||||
vminterface_ct = ContentType.objects.get_for_model(VMInterface)
|
|
||||||
mac_addresses = []
|
|
||||||
print()
|
|
||||||
print('Converting MAC addresses...')
|
|
||||||
for interface in Interface.objects.filter(_mac_address__isnull=False):
|
|
||||||
mac_addresses.append(
|
|
||||||
MACAddress(
|
|
||||||
mac_address=interface._mac_address,
|
|
||||||
assigned_object_type=interface_ct,
|
|
||||||
assigned_object_id=interface.id,
|
|
||||||
# interface=interface,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for vminterface in VMInterface.objects.filter(_mac_address__isnull=False):
|
|
||||||
mac_addresses.append(
|
|
||||||
MACAddress(
|
|
||||||
mac_address=vminterface._mac_address,
|
|
||||||
assigned_object_type=vminterface_ct,
|
|
||||||
assigned_object_id=vminterface.id,
|
|
||||||
# vm_interface=vm_interface,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
MACAddress.objects.bulk_create(mac_addresses)
|
|
||||||
print(f'Created {len(mac_addresses)} MAC address objects.')
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
@ -42,15 +11,9 @@ class Migration(migrations.Migration):
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
('dcim', '0198_natural_ordering'),
|
('dcim', '0198_natural_ordering'),
|
||||||
('extras', '0122_charfield_null_choices'),
|
('extras', '0122_charfield_null_choices'),
|
||||||
('virtualization', '0047_vminterface_rename_mac_address'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.RenameField(
|
|
||||||
model_name='interface',
|
|
||||||
old_name='mac_address',
|
|
||||||
new_name='_mac_address',
|
|
||||||
),
|
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='MACAddress',
|
name='MACAddress',
|
||||||
fields=[
|
fields=[
|
||||||
@ -71,8 +34,4 @@ class Migration(migrations.Migration):
|
|||||||
'ordering': ('mac_address',)
|
'ordering': ('mac_address',)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
migrations.RunPython(
|
|
||||||
code=populate_macaddress_objects,
|
|
||||||
reverse_code=migrations.RunPython.noop
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
from django.db import migrations
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('dcim', '0199_macaddress'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RemoveField(
|
|
||||||
model_name='interface',
|
|
||||||
name='_mac_address',
|
|
||||||
),
|
|
||||||
]
|
|
42
netbox/dcim/migrations/0200_populate_mac_addresses.py
Normal file
42
netbox/dcim/migrations/0200_populate_mac_addresses.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def populate_mac_addresses(apps, schema_editor):
|
||||||
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||||
|
Interface = apps.get_model('dcim', 'Interface')
|
||||||
|
MACAddress = apps.get_model('dcim', 'MACAddress')
|
||||||
|
interface_ct = ContentType.objects.get_for_model(Interface)
|
||||||
|
|
||||||
|
mac_addresses = [
|
||||||
|
MACAddress(
|
||||||
|
mac_address=interface._mac_address,
|
||||||
|
assigned_object_type=interface_ct,
|
||||||
|
assigned_object_id=interface.pk
|
||||||
|
)
|
||||||
|
for interface in Interface.objects.filter(_mac_address__isnull=False)
|
||||||
|
]
|
||||||
|
MACAddress.objects.bulk_create(mac_addresses, batch_size=100)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('dcim', '0199_macaddress'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
# Rename mac_address field to avoid conflict with property
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='interface',
|
||||||
|
old_name='mac_address',
|
||||||
|
new_name='_mac_address',
|
||||||
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
code=populate_mac_addresses,
|
||||||
|
reverse_code=migrations.RunPython.noop
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='interface',
|
||||||
|
name='_mac_address',
|
||||||
|
),
|
||||||
|
]
|
@ -1,16 +1,43 @@
|
|||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def populate_mac_addresses(apps, schema_editor):
|
||||||
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||||
|
VMInterface = apps.get_model('virtualization', 'VMInterface')
|
||||||
|
MACAddress = apps.get_model('dcim', 'MACAddress')
|
||||||
|
vminterface_ct = ContentType.objects.get_for_model(VMInterface)
|
||||||
|
|
||||||
|
mac_addresses = [
|
||||||
|
MACAddress(
|
||||||
|
mac_address=vminterface._mac_address,
|
||||||
|
assigned_object_type=vminterface_ct,
|
||||||
|
assigned_object_id=vminterface.pk
|
||||||
|
)
|
||||||
|
for vminterface in VMInterface.objects.filter(_mac_address__isnull=False)
|
||||||
|
]
|
||||||
|
MACAddress.objects.bulk_create(mac_addresses, batch_size=100)
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
('dcim', '0199_macaddress'),
|
||||||
('virtualization', '0046_natural_ordering'),
|
('virtualization', '0046_natural_ordering'),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
# Rename mac_address field to avoid conflict with property
|
||||||
migrations.RenameField(
|
migrations.RenameField(
|
||||||
model_name='vminterface',
|
model_name='vminterface',
|
||||||
old_name='mac_address',
|
old_name='mac_address',
|
||||||
new_name='_mac_address',
|
new_name='_mac_address',
|
||||||
),
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
code=populate_mac_addresses,
|
||||||
|
reverse_code=migrations.RunPython.noop
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='vminterface',
|
||||||
|
name='_mac_address',
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
from django.db import migrations
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('virtualization', '0047_vminterface_rename_mac_address'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RemoveField(
|
|
||||||
model_name='vminterface',
|
|
||||||
name='_mac_address',
|
|
||||||
),
|
|
||||||
]
|
|
Loading…
Reference in New Issue
Block a user