mirror of https://github.com/jumpserver/jumpserver
				
				
				
			perf: 迁移 app permission
							parent
							
								
									85acd4b2ac
								
							
						
					
					
						commit
						34c8cfc20a
					
				|  | @ -1,11 +1,62 @@ | ||||||
| # Generated by Django 3.2.14 on 2022-07-28 09:10 | from django.db import migrations | ||||||
| 
 |  | ||||||
| from django.db import migrations, models |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def migrate_system_user_to_accounts(apps, schema_editor): | def migrate_app_perms_to_assets(apps, schema_editor): | ||||||
|     # Todo: 迁移 系统用户为账号 |     asset_permission_model = apps.get_model("perms", "AssetPermission") | ||||||
|     pass |     app_permission_model = apps.get_model("perms", "ApplicationPermission") | ||||||
|  | 
 | ||||||
|  |     count = 0 | ||||||
|  |     bulk_size = 1000 | ||||||
|  |     while True: | ||||||
|  |         app_perms = app_permission_model.objects.all()[count:bulk_size] | ||||||
|  |         if not app_perms: | ||||||
|  |             break | ||||||
|  |         count += len(app_perms) | ||||||
|  |         attrs = [ | ||||||
|  |             'id', 'name', 'actions', 'is_active', 'date_start', | ||||||
|  |             'date_expired', 'created_by', 'from_ticket', 'comment', | ||||||
|  |         ] | ||||||
|  |         asset_permissions = [] | ||||||
|  | 
 | ||||||
|  |         for app_perm in app_perms: | ||||||
|  |             asset_permission = asset_permission_model() | ||||||
|  |             for attr in attrs: | ||||||
|  |                 setattr(asset_permission, attr, getattr(app_perm, attr)) | ||||||
|  |             asset_permissions.append(asset_permission) | ||||||
|  |         asset_permission_model.objects.bulk_create(asset_permissions, ignore_conflicts=True) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def migrate_relations(apps, schema_editor): | ||||||
|  |     asset_permission_model = apps.get_model("perms", "AssetPermission") | ||||||
|  |     app_permission_model = apps.get_model("perms", "ApplicationPermission") | ||||||
|  |     m2m_names = [ | ||||||
|  |         ('applications', 'assets', 'application_id', 'asset_id'), | ||||||
|  |         ('users', 'users', 'user_id', 'user_id'), | ||||||
|  |         ('user_groups', 'user_groups', 'usergroup_id', 'usergroup_id'), | ||||||
|  |         ('system_users', 'system_users', 'systemuser_id', 'systemuser_id'), | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  |     for app_name, asset_name, app_attr, asset_attr in m2m_names: | ||||||
|  |         app_through = getattr(app_permission_model, app_name).through | ||||||
|  |         asset_through = getattr(asset_permission_model, asset_name).through | ||||||
|  | 
 | ||||||
|  |         count = 0 | ||||||
|  |         bulk_size = 1000 | ||||||
|  | 
 | ||||||
|  |         while True: | ||||||
|  |             app_permission_relations = app_through.objects.all()[count:bulk_size] | ||||||
|  |             if not app_permission_relations: | ||||||
|  |                 break | ||||||
|  |             count += len(app_permission_relations) | ||||||
|  |             asset_through_relations = [] | ||||||
|  | 
 | ||||||
|  |             for app_relation in app_permission_relations: | ||||||
|  |                 asset_relation = asset_through() | ||||||
|  |                 asset_relation.assetpermission_id = app_relation.applicationpermission_id | ||||||
|  |                 setattr(asset_relation, asset_attr, getattr(app_relation, app_attr)) | ||||||
|  |                 asset_through_relations.append(asset_relation) | ||||||
|  | 
 | ||||||
|  |             asset_through.objects.bulk_create(asset_through_relations, ignore_conflicts=True) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Migration(migrations.Migration): | class Migration(migrations.Migration): | ||||||
|  | @ -15,14 +66,6 @@ class Migration(migrations.Migration): | ||||||
|     ] |     ] | ||||||
| 
 | 
 | ||||||
|     operations = [ |     operations = [ | ||||||
|         migrations.AddField( |         migrations.RunPython(migrate_app_perms_to_assets), | ||||||
|             model_name='assetpermission', |         migrations.RunPython(migrate_relations), | ||||||
|             name='accounts', |  | ||||||
|             field=models.JSONField(default=list, verbose_name='Accounts'), |  | ||||||
|         ), |  | ||||||
|         migrations.RunPython(migrate_system_user_to_accounts), |  | ||||||
|         migrations.RemoveField( |  | ||||||
|             model_name='assetpermission', |  | ||||||
|             name='system_users', |  | ||||||
|         ), |  | ||||||
|     ] |     ] | ||||||
|  |  | ||||||
|  | @ -0,0 +1,45 @@ | ||||||
|  | # Generated by Django 3.2.14 on 2022-08-16 03:32 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | import time | ||||||
|  | from django.db import migrations, models | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def migrate_system_user_to_accounts(apps, schema_editor): | ||||||
|  |     asset_permission_model = apps.get_model("perms", "AssetPermission") | ||||||
|  | 
 | ||||||
|  |     count = 0 | ||||||
|  |     bulk_size = 10000 | ||||||
|  |     while True: | ||||||
|  |         asset_permissions = asset_permission_model.objects \ | ||||||
|  |                                 .prefetch_related('system_users')[count:bulk_size] | ||||||
|  |         if not asset_permissions: | ||||||
|  |             break | ||||||
|  |         count += len(asset_permissions) | ||||||
|  | 
 | ||||||
|  |         updated = [] | ||||||
|  |         for asset_permission in asset_permissions: | ||||||
|  |             asset_permission.accounts = [s.username for s in asset_permission.system_users.all()] | ||||||
|  |             updated.append(asset_permission) | ||||||
|  |         asset_permission_model.objects.bulk_update(updated, ['accounts']) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class Migration(migrations.Migration): | ||||||
|  | 
 | ||||||
|  |     dependencies = [ | ||||||
|  |         ('perms', '0029_auto_20220728_1728'), | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  |     operations = [ | ||||||
|  |         migrations.AddField( | ||||||
|  |             model_name='assetpermission', | ||||||
|  |             name='accounts', | ||||||
|  |             field=models.JSONField(default=list, verbose_name='Accounts'), | ||||||
|  |         ), | ||||||
|  |         migrations.RunPython(migrate_system_user_to_accounts), | ||||||
|  |         migrations.RemoveField( | ||||||
|  |             model_name='assetpermission', | ||||||
|  |             name='system_users', | ||||||
|  |         ), | ||||||
|  |     ] | ||||||
		Loading…
	
		Reference in New Issue
	
	 ibuler
						ibuler