fix migrating task/check results

This commit is contained in:
sadnub 2022-04-15 14:17:57 -04:00
parent e26349f2fc
commit fa836d88c7
2 changed files with 73 additions and 29 deletions

View File

@ -1,27 +1,46 @@
# Generated by Django 3.2.12 on 2022-04-01 22:49
from django.db import migrations
from django.db import migrations, transaction
from django.db.utils import IntegrityError
def migrate_task_results(apps, schema_editor):
AutomatedTask = apps.get_model("autotasks", "AutomatedTask")
TaskResult = apps.get_model("autotasks", "TaskResult")
for task in AutomatedTask.objects.exclude(agent=None):
TaskResult(
task=task,
agent=task.agent,
retcode=task.retcode,
stdout=task.stdout,
stderr=task.stderr,
execution_time=task.execution_time,
last_run=task.last_run,
status=task.status,
sync_status=task.sync_status,
).save()
try:
with transaction.atomic():
if task.managed_by_policy:
TaskResult.objects.create(
task_id=task.parent_task,
agent_id=task.agent_id,
retcode=task.retcode,
stdout=task.stdout,
stderr=task.stderr,
execution_time=task.execution_time,
last_run=task.last_run,
status=task.status,
sync_status=task.sync_status,
)
else:
TaskResult.objects.create(
task_id=task.id,
agent_id=task.agent.id,
retcode=task.retcode,
stdout=task.stdout,
stderr=task.stderr,
execution_time=task.execution_time,
last_run=task.last_run,
status=task.status,
sync_status=task.sync_status,
)
except IntegrityError:
continue
class Migration(migrations.Migration):
atomic = False
dependencies = [
("autotasks", "0030_auto_20220401_2244"),
]

View File

@ -1,30 +1,55 @@
# Generated by Django 3.2.12 on 2022-04-01 22:48
from django.db import migrations
from django.db import migrations, transaction
from django.db.utils import IntegrityError
def migrate_check_results(apps, schema_editor):
Check = apps.get_model("checks", "Check")
CheckResult = apps.get_model("checks", "CheckResult")
for check in Check.objects.exclude(agent=None):
CheckResult(
assigned_check=check,
agent=check.agent,
status=check.status,
more_info=check.more_info,
last_run=check.last_run,
fail_count=check.fail_count,
outage_history=check.outage_history,
extra_details=check.extra_details,
stdout=check.stdout,
stderr=check.stderr,
retcode=check.retcode,
execution_time=check.execution_time,
history=check.history,
).save()
try:
with transaction.atomic():
if check.managed_by_policy:
CheckResult.objects.create(
assigned_check_id=check.parent_check,
agent_id=check.agent.id,
status=check.status,
more_info=check.more_info,
last_run=check.last_run,
fail_count=check.fail_count,
outage_history=check.outage_history,
extra_details=check.extra_details,
stdout=check.stdout,
stderr=check.stderr,
retcode=check.retcode,
execution_time=check.execution_time,
history=check.history,
)
else:
CheckResult.objects.create(
assigned_check=check,
agent=check.agent,
status=check.status,
more_info=check.more_info,
last_run=check.last_run,
fail_count=check.fail_count,
outage_history=check.outage_history,
extra_details=check.extra_details,
stdout=check.stdout,
stderr=check.stderr,
retcode=check.retcode,
execution_time=check.execution_time,
history=check.history,
)
except IntegrityError:
pass
class Migration(migrations.Migration):
atomic = False
dependencies = [
("checks", "0026_auto_20220401_2244"),