create migration that will fix duplicate win_task_names and make win_task_name unique
This commit is contained in:
parent
fa836d88c7
commit
2458eb3960
|
@ -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),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue