Skip to content

Commit

Permalink
fix duplicate agent customfields
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Jul 18, 2023
1 parent f4fc6ee commit 9118162
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand

from agents.models import Agent
from tacticalrmm.constants import AGENT_DEFER


class Command(BaseCommand):
def find_duplicates(self, lst):
return list(set([item for item in lst if lst.count(item) > 1]))

def handle(self, *args, **kwargs):
for agent in Agent.objects.defer(*AGENT_DEFER).prefetch_related(
"custom_fields__field"
):
if dupes := self.find_duplicates(
[i.field.name for i in agent.custom_fields.all()]
):
for dupe in dupes:
cf = list(
agent.custom_fields.filter(field__name=dupe).order_by("id")
)
to_delete = cf[:-1]
for i in to_delete:
i.delete()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.3 on 2023-07-18 01:15

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("core", "0037_coresettings_open_ai_model_and_more"),
("agents", "0056_alter_agent_time_zone"),
]

operations = [
migrations.AlterUniqueTogether(
name="agentcustomfield",
unique_together={("agent", "field")},
),
]
3 changes: 3 additions & 0 deletions api/tacticalrmm/agents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,9 @@ class AgentCustomField(models.Model):
default=list,
)

class Meta:
unique_together = (("agent", "field"),)

def __str__(self) -> str:
return self.field.name

Expand Down
2 changes: 2 additions & 0 deletions api/tacticalrmm/core/management/commands/pre_update_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand

from core.utils import clear_entire_cache
Expand All @@ -10,3 +11,4 @@ def handle(self, *args, **kwargs):
self.stdout.write(self.style.WARNING("Cleaning the cache"))
clear_entire_cache()
self.stdout.write(self.style.SUCCESS("Cache was cleared!"))
call_command("fix_dupe_agent_customfields")

0 comments on commit 9118162

Please sign in to comment.