From 67d3b63c6d24406d80485df2bf22303c314ead5e Mon Sep 17 00:00:00 2001 From: xinwen Date: Tue, 6 Apr 2021 09:53:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20key=20=E4=B8=BA=200?= =?UTF-8?q?=20=E7=9A=84=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0069_change_node_key0_to_key1.py | 59 +++++++++++++++++++ ...329_1711.py => 0034_auto_20210406_1434.py} | 11 ++-- 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 apps/assets/migrations/0069_change_node_key0_to_key1.py rename apps/terminal/migrations/{0033_auto_20210329_1711.py => 0034_auto_20210406_1434.py} (82%) diff --git a/apps/assets/migrations/0069_change_node_key0_to_key1.py b/apps/assets/migrations/0069_change_node_key0_to_key1.py new file mode 100644 index 000000000..4024386dc --- /dev/null +++ b/apps/assets/migrations/0069_change_node_key0_to_key1.py @@ -0,0 +1,59 @@ +from django.db import migrations +from django.db.transaction import atomic + +default_id = '00000000-0000-0000-0000-000000000002' + + +def change_key0_to_key1(apps, schema_editor): + from orgs.utils import set_current_org + + # https://stackoverflow.com/questions/28777338/django-migrations-runpython-not-able-to-call-model-methods + Organization = apps.get_model('orgs', 'Organization') + Node = apps.get_model('assets', 'Node') + + print() + org = Organization.objects.get(id=default_id) + set_current_org(org) + + exists_0 = Node.objects.filter(key__startswith='0').exists() + if not exists_0: + print(f'--> Not exist key=0 nodes, do nothing.') + return + + key_1_count = Node.objects.filter(key__startswith='1').count() + if key_1_count > 1: + print(f'--> Node key=1 have children, can`t just delete it. Please contact JumpServer team') + return + + root_node = Node.objects.filter(key='1').first() + if root_node and root_node.assets.exists(): + print(f'--> Node key=1 has assets, do nothing.') + return + + with atomic(): + if root_node: + print(f'--> Delete node key=1') + root_node.delete() + + nodes_0 = Node.objects.filter(key__startswith='0') + + for n in nodes_0: + old_key = n.key + key_list = n.key.split(':') + key_list[0] = '1' + new_key = ':'.join(key_list) + n.key = new_key + n.save() + print('--> Modify key ( {} > {} )'.format(old_key, new_key)) + + +class Migration(migrations.Migration): + + dependencies = [ + ('orgs', '0010_auto_20210219_1241'), + ('assets', '0068_auto_20210312_1455'), + ] + + operations = [ + migrations.RunPython(change_key0_to_key1) + ] diff --git a/apps/terminal/migrations/0033_auto_20210329_1711.py b/apps/terminal/migrations/0034_auto_20210406_1434.py similarity index 82% rename from apps/terminal/migrations/0033_auto_20210329_1711.py rename to apps/terminal/migrations/0034_auto_20210406_1434.py index bbc45a8c7..59cc89b9b 100644 --- a/apps/terminal/migrations/0033_auto_20210329_1711.py +++ b/apps/terminal/migrations/0034_auto_20210406_1434.py @@ -1,4 +1,4 @@ -# Generated by Django 3.1 on 2021-03-29 09:11 +# Generated by Django 3.1 on 2021-04-06 06:34 from django.db import migrations, models @@ -6,16 +6,15 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('terminal', '0032_auto_20210302_1853'), + ('terminal', '0033_auto_20210324_1008'), ] operations = [ - migrations.RenameField( + migrations.RemoveField( model_name='status', - old_name='cpu_used', - new_name='cpu_load', + name='cpu_used', ), - migrations.AlterField( + migrations.AddField( model_name='status', name='cpu_load', field=models.FloatField(default=0, verbose_name='CPU Load'),