|
|
|
# Generated by Django 3.1.14 on 2022-04-02 08:27
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.db import migrations, models
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_hardware(apps, *args):
|
|
|
|
host_model = apps.get_model('assets', 'Host')
|
|
|
|
asset_model = apps.get_model('assets', 'Asset')
|
|
|
|
hardware_model = apps.get_model('assets', 'DeviceInfo')
|
|
|
|
|
|
|
|
created = 0
|
|
|
|
batch_size = 1000
|
|
|
|
|
|
|
|
excludes = ['id', 'host', 'date_updated']
|
|
|
|
fields = [f.name for f in hardware_model._meta.fields]
|
|
|
|
fields = [name for name in fields if name not in excludes]
|
|
|
|
|
|
|
|
while True:
|
|
|
|
start = created
|
|
|
|
end = created + batch_size
|
|
|
|
hosts = host_model.objects.all()[start:end]
|
|
|
|
asset_ids = [h.asset_ptr_id for h in hosts]
|
|
|
|
assets = asset_model.objects.filter(id__in=asset_ids)
|
|
|
|
asset_mapper = {a.id: a for a in assets}
|
|
|
|
|
|
|
|
if not hosts:
|
|
|
|
break
|
|
|
|
|
|
|
|
hardware_infos = []
|
|
|
|
for host in hosts:
|
|
|
|
hardware = hardware_model()
|
|
|
|
asset = asset_mapper[host.asset_ptr_id]
|
|
|
|
hardware.host = host
|
|
|
|
hardware.date_updated = timezone.now()
|
|
|
|
for name in fields:
|
|
|
|
setattr(hardware, name, getattr(asset, name))
|
|
|
|
hardware_infos.append(hardware)
|
|
|
|
|
|
|
|
hardware_model.objects.bulk_create(hardware_infos, ignore_conflicts=True)
|
|
|
|
created += len(hardware_infos)
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('assets', '0092_hardware'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(migrate_hardware)
|
|
|
|
]
|