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

72 lines
2.4 KiB
Python
Raw Normal View History

2022-09-16 09:24:27 +00:00
# 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 = [
2022-09-22 07:24:32 +00:00
('assets', '0105_auto_20220817_1544'),
2022-09-16 09:24:27 +00:00
]
operations = [
2022-09-22 07:24:32 +00:00
migrations.AlterField(
model_name='accountbackupplan',
name='types',
field=models.BigIntegerField(),
),
2022-09-16 09:24:27 +00:00
migrations.AddField(
model_name='accountbackupplan',
name='categories',
field=models.JSONField(default=list),
),
migrations.RunPython(migrate_backup_types),
migrations.RemoveField(
model_name='accountbackupplan',
name='types',
),
]