jumpserver/apps/assets/migrations/0106_auto_20220916_1556.py

72 lines
2.4 KiB
Python

# Generated by Django 3.2.13 on 2022-09-16 07:56
from functools import reduce
from django.db import migrations, models
from assets.const import AllTypes, HostTypes
def migrate_backup_types(apps, schema_editor):
all_types = list(reduce(
lambda x, y: x + y,
[
[j['value'] for j in i['children']]
for i in AllTypes.grouped_choices_to_objs()
]
))
asset_types = [i[0] for i in HostTypes.choices]
app_types = list(set(all_types) - set(asset_types))
backup_model = apps.get_model("assets", "AccountBackupPlan")
backup_objs = []
for instance in backup_model.objects.all():
types = instance.types
if types == 1:
instance.categories = asset_types
elif types == 2:
instance.categories = app_types
elif types == 255:
instance.categories = all_types
else:
instance.categories = []
backup_objs.append(instance)
backup_model.objects.bulk_update(backup_objs, ['categories'])
backup_execution_model = apps.get_model("assets", "AccountBackupPlanExecution")
backup_execution_objs = []
for instance in backup_execution_model.objects.all():
types = instance.plan_snapshot.get('types', [])
if 'all' in types:
instance.plan_snapshot['categories'] = all_types
elif 'asset' in types:
instance.plan_snapshot['categories'] = asset_types
elif 'application' in types:
instance.plan_snapshot['categories'] = app_types
else:
instance.categories = []
instance.plan_snapshot.pop('types', None)
backup_execution_objs.append(instance)
backup_execution_model.objects.bulk_update(backup_execution_objs, ['plan_snapshot'])
class Migration(migrations.Migration):
dependencies = [
('assets', '0105_auto_20220817_1544'),
]
operations = [
migrations.AlterField(
model_name='accountbackupplan',
name='types',
field=models.BigIntegerField(),
),
migrations.AddField(
model_name='accountbackupplan',
name='categories',
field=models.JSONField(default=list),
),
migrations.RunPython(migrate_backup_types),
migrations.RemoveField(
model_name='accountbackupplan',
name='types',
),
]