perf: 优化资产迁移,避免冲突

pull/9942/head
ibuler 2 years ago committed by Jiangjie.Bai
parent 44d7165674
commit 082af029a7

@ -34,8 +34,9 @@ def migrate_database_to_asset(apps, *args):
_attrs = app.attrs or {} _attrs = app.attrs or {}
attrs.update(_attrs) attrs.update(_attrs)
name = 'DB-{}'.format(app.name)
db = db_model( db = db_model(
id=app.id, name=app.name, address=attrs['host'], id=app.id, name=name, address=attrs['host'],
protocols='{}/{}'.format(app.type, attrs['port']), protocols='{}/{}'.format(app.type, attrs['port']),
db_name=attrs['database'] or '', db_name=attrs['database'] or '',
platform=platforms_map[app.type], platform=platforms_map[app.type],
@ -61,8 +62,9 @@ def migrate_cloud_to_asset(apps, *args):
for app in applications: for app in applications:
attrs = app.attrs attrs = app.attrs
print("\t- Create cloud: {}".format(app.name)) print("\t- Create cloud: {}".format(app.name))
name = 'Cloud-{}'.format(app.name)
cloud = cloud_model( cloud = cloud_model(
id=app.id, name=app.name, id=app.id, name=name,
address=attrs.get('cluster', ''), address=attrs.get('cluster', ''),
protocols='k8s/443', platform=platform, protocols='k8s/443', platform=platform,
org_id=app.org_id, org_id=app.org_id,

@ -23,6 +23,7 @@ def migrate_app_perms_to_assets(apps, schema_editor):
asset_permission = asset_permission_model() asset_permission = asset_permission_model()
for attr in attrs: for attr in attrs:
setattr(asset_permission, attr, getattr(app_perm, attr)) setattr(asset_permission, attr, getattr(app_perm, attr))
asset_permission.name = f"App-{app_perm.name}"
asset_permissions.append(asset_permission) asset_permissions.append(asset_permission)
asset_permission_model.objects.bulk_create(asset_permissions, ignore_conflicts=True) asset_permission_model.objects.bulk_create(asset_permissions, ignore_conflicts=True)

@ -9,11 +9,11 @@ def migrate_system_user_to_accounts(apps, schema_editor):
bulk_size = 10000 bulk_size = 10000
while True: while True:
asset_permissions = asset_permission_model.objects \ asset_permissions = asset_permission_model.objects \
.prefetch_related('system_users')[count:bulk_size] .prefetch_related('system_users')[count:bulk_size]
if not asset_permissions: if not asset_permissions:
break break
count += len(asset_permissions)
count += len(asset_permissions)
updated = [] updated = []
for asset_permission in asset_permissions: for asset_permission in asset_permissions:
asset_permission.accounts = [s.username for s in asset_permission.system_users.all()] asset_permission.accounts = [s.username for s in asset_permission.system_users.all()]

Loading…
Cancel
Save