Merge pull request #1234 from tcely/patch-13
Add `remove_duplicated_rows` function before `task_id` unique change
This commit is contained in:
1 file changed
+37
@@ -1,6 +1,39 @@
|
||||
# Generated by Django 5.2.4 on 2025-07-15 21:40
|
||||
|
||||
from django.db import migrations, models
|
||||
from common.logger import log
|
||||
|
||||
|
||||
def remove_duplicated_rows(apps, schema_editor):
|
||||
def keep_which(task_id):
|
||||
th_qs = TaskHistory.objects.all().order_by('-id')
|
||||
tqs = th_qs.filter(task_id=task_id)
|
||||
return tqs[0].id
|
||||
|
||||
TaskHistory = apps.get_model("common", 'TaskHistory')
|
||||
duplicates = set(
|
||||
TaskHistory.objects.values(
|
||||
'task_id',
|
||||
).alias(
|
||||
count=models.Count('id'),
|
||||
).filter(
|
||||
count__gt=1,
|
||||
).values_list(
|
||||
'task_id',
|
||||
flat=True,
|
||||
),
|
||||
)
|
||||
|
||||
log.info(f'TaskHistory rows: {len(duplicates)=}')
|
||||
for task_id, n in enumerate(duplicates, start=1):
|
||||
keeping = keep_which(task_id)
|
||||
log.debug(f'{n=}: {task_id=}: {keeping=}')
|
||||
TaskHistory.objects.filter(
|
||||
task_id=task_id,
|
||||
).exclude(
|
||||
id=keeping,
|
||||
).delete()
|
||||
log.info('TaskHistory rows: finished removing duplicates.')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -10,6 +43,10 @@ class Migration(migrations.Migration):
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
remove_duplicated_rows,
|
||||
migrations.RunPython.noop,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='taskhistory',
|
||||
name='task_id',
|
||||
|
||||
Reference in new issue
Block a user