You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/assets/migrations/0093_auto_20220403_1627.py

53 lines
1.5 KiB

# Generated by Django 3.1.14 on 2022-04-02 08:27
from django.utils import timezone
3 years ago
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}
3 years ago
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)
]