mirror of https://github.com/jumpserver/jumpserver
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
# 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')
|
|
|
|
count = 0
|
|
bulk_size = 1000
|
|
print("\nStart migrate asset protocols")
|
|
while True:
|
|
start = time.time()
|
|
assets = asset_model.objects.all()[count:count+bulk_size]
|
|
count += len(assets)
|
|
if not assets:
|
|
break
|
|
|
|
protocols = []
|
|
for asset in assets:
|
|
for protocol in asset.protocols.all():
|
|
protocols.append(protocol_model(
|
|
asset_id=asset.id,
|
|
protocol=protocol.protocol,
|
|
port=protocol.port,
|
|
))
|
|
protocol_model.objects.bulk_create(protocols, ignore_conflicts=True)
|
|
print("Create asset protocols: {}-{} using: {:.2f}s".format(
|
|
count - bulk_size, count, time.time()-start
|
|
))
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('assets', '0103_auto_20220803_1448'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(migrate_asset_protocols)
|
|
]
|