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/0102_auto_20220803_1859.py

53 lines
1.7 KiB

# Generated by Django 3.2.14 on 2022-08-03 10:59
import time
from django.db import migrations
def migrate_asset_protocols(apps, schema_editor):
asset_model = apps.get_model('assets', 'Asset')
protocol_model = apps.get_model('assets', 'Protocol')
asset_protocol_through = asset_model.protocols.through
count = 0
bulk_size = 1000
print("\nStart migrate asset protocols")
protocol_map = {}
while True:
start = time.time()
assets = asset_model.objects.all()[count:count+bulk_size]
if not assets:
break
count += len(assets)
assets_protocols = []
for asset in assets:
old_protocols = asset._protocols
for name_port in old_protocols.split(','):
name_port_list = name_port.split('/')
if len(name_port_list) != 2:
continue
name, port = name_port_list
protocol = protocol_map.get(name_port)
if not protocol:
protocol = protocol_model.objects.get_or_create(
defaults={'name': name, 'port': port},
name=name, port=port
)[0]
assets_protocols.append(asset_protocol_through(asset_id=asset.id, protocol_id=protocol.id))
asset_model.protocols.through.objects.bulk_create(assets_protocols, ignore_conflicts=True)
print("Create asset protocols: {}-{} using: {:.2f}s".format(
count - len(assets), count, time.time()-start
))
class Migration(migrations.Migration):
dependencies = [
('assets', '0101_auto_20220803_1448'),
]
operations = [
migrations.RunPython(migrate_asset_protocols)
]