From 2458eb3960a2d3fad49803d51837c1e806d874a5 Mon Sep 17 00:00:00 2001 From: sadnub Date: Fri, 15 Apr 2022 14:52:37 -0400 Subject: [PATCH] create migration that will fix duplicate win_task_names and make win_task_name unique --- .../migrations/0035_auto_20220415_1818.py | 34 +++++++++++++++++++ .../0036_alter_automatedtask_win_task_name.py | 18 ++++++++++ api/tacticalrmm/autotasks/models.py | 7 +++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 api/tacticalrmm/autotasks/migrations/0035_auto_20220415_1818.py create mode 100644 api/tacticalrmm/autotasks/migrations/0036_alter_automatedtask_win_task_name.py diff --git a/api/tacticalrmm/autotasks/migrations/0035_auto_20220415_1818.py b/api/tacticalrmm/autotasks/migrations/0035_auto_20220415_1818.py new file mode 100644 index 00000000..41717d24 --- /dev/null +++ b/api/tacticalrmm/autotasks/migrations/0035_auto_20220415_1818.py @@ -0,0 +1,34 @@ +# Generated by Django 4.0.3 on 2022-04-15 18:18 + +from django.db import migrations +from django.db.models import Count + + +def check_for_win_task_name_duplicates(apps, schema_editor): + AutomatedTask = apps.get_model("autotasks", "AutomatedTask") + TaskResult = apps.get_model("autotasks", "TaskResult") + + duplicate_tasks = ( + AutomatedTask.objects.values("win_task_name") + .annotate(records=Count("win_task_name")) + .filter(records__gt=1) + ) + for task in duplicate_tasks: + dups = list(AutomatedTask.objects.filter(win_task_name=task["win_task_name"])) + for x in range(task["records"] - 1): + + dups[x].win_task_name = AutomatedTask.generate_task_name() + dups[x].save(update_fields=["win_task_name"]) + # update task_result sync status + TaskResult.objects.filter(task=dups[x]).update(sync_status="notsynced") + + +class Migration(migrations.Migration): + + dependencies = [ + ("autotasks", "0034_auto_20220402_0046"), + ] + + operations = [ + migrations.RunPython(check_for_win_task_name_duplicates), + ] diff --git a/api/tacticalrmm/autotasks/migrations/0036_alter_automatedtask_win_task_name.py b/api/tacticalrmm/autotasks/migrations/0036_alter_automatedtask_win_task_name.py new file mode 100644 index 00000000..9877bcfe --- /dev/null +++ b/api/tacticalrmm/autotasks/migrations/0036_alter_automatedtask_win_task_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-04-15 18:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('autotasks', '0035_auto_20220415_1818'), + ] + + operations = [ + migrations.AlterField( + model_name='automatedtask', + name='win_task_name', + field=models.CharField(blank=True, default='TacticalRMM_vRdxtdnbgoQSEADfuxXuIiegSOSNhMElwgr', max_length=255, unique=True), + ), + ] diff --git a/api/tacticalrmm/autotasks/models.py b/api/tacticalrmm/autotasks/models.py index 9ac4c5c4..5eb192c9 100644 --- a/api/tacticalrmm/autotasks/models.py +++ b/api/tacticalrmm/autotasks/models.py @@ -55,6 +55,11 @@ TASK_STATUS_CHOICES = [ ] +def generate_task_name() -> str: + chars = string.ascii_letters + return "TacticalRMM_" + "".join(random.choice(chars) for i in range(35)) + + class AutomatedTask(BaseAuditModel): objects = PermissionQuerySet.as_manager() @@ -106,7 +111,7 @@ class AutomatedTask(BaseAuditModel): max_length=100, choices=TASK_TYPE_CHOICES, default="manual" ) win_task_name = models.CharField( - max_length=255, null=True, blank=True + max_length=255, unique=True, blank=True, default=generate_task_name() ) # should be changed to unique=True run_time_date = DateTimeField(null=True, blank=True) expire_date = DateTimeField(null=True, blank=True)