11# Generated by Django 4.2.17 on 2025-10-20 10:08
22
33from django .db import migrations , transaction
4- from django .db .models import Count , Q
4+ from django .db .models import Count
5+
56
67def resolve_duplicates (apps , schema_editor ):
78 models = [
@@ -47,26 +48,12 @@ def resolve_duplicates(apps, schema_editor):
4748 .filter (count__gt = 1 )
4849 )
4950 for dup in duplicates :
50- ids = list (app_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' , flat = True ))
51+ ids = list (app_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' ,
52+ flat = True ))
5153 primary_id = ids .pop (0 ) # Keep the first ID as primary
5254 jobs_model .objects .filter (** {f"{ key } __in" : ids }).update (** {key : primary_id })
5355 app_model .objects .filter (id__in = ids ).delete ()
5456
55- def resolve_label_duplicates (apps , schema_editor ):
56- labels_model = apps .get_model ('clusters' , 'Label' )
57- job_label_model = apps .get_model ('clusters' , 'JobLabel' )
58- duplicates = (
59- labels_model .objects
60- .values ('name' , 'cluster_id' )
61- .annotate (count = Count ('id' ))
62- .filter (count__gt = 1 )
63- )
64- with transaction .atomic ():
65- for dup in duplicates :
66- ids = list (labels_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' , flat = True ))
67- primary_id = ids .pop (0 )
68- job_label_model .objects .filter (label_id__in = ids ).update (label_id = primary_id )
69- labels_model .objects .filter (id__in = ids ).delete ()
7057
7158def resolve_label_duplicates (apps , schema_editor ):
7259 labels_model = apps .get_model ('clusters' , 'Label' )
@@ -79,11 +66,13 @@ def resolve_label_duplicates(apps, schema_editor):
7966 )
8067 with transaction .atomic ():
8168 for dup in duplicates :
82- ids = list (labels_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' , flat = True ))
69+ ids = list (labels_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' ,
70+ flat = True ))
8371 primary_id = ids .pop (0 )
8472 job_label_model .objects .filter (label_id__in = ids ).update (label_id = primary_id )
8573 labels_model .objects .filter (id__in = ids ).delete ()
8674
75+
8776def resolve_host_duplicates (apps , schema_editor ):
8877 host_model = apps .get_model ('clusters' , 'Host' )
8978 job_host_summary_model = apps .get_model ('clusters' , 'JobHostSummary' )
@@ -95,7 +84,8 @@ def resolve_host_duplicates(apps, schema_editor):
9584 )
9685 with transaction .atomic ():
9786 for dup in duplicates :
98- ids = list (host_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' , flat = True ))
87+ ids = list (
88+ host_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ]).values_list ('id' , flat = True ))
9989 primary_id = ids .pop (0 )
10090 job_host_summary_model .objects .filter (host_id__in = ids ).update (host_id = primary_id )
10191 host_model .objects .filter (id__in = ids ).delete ()
@@ -112,13 +102,15 @@ def resolve_aap_user_duplicates(apps, schema_editor):
112102 .filter (count__gt = 1 )
113103 )
114104 for dup in duplicates :
115- ids = list (aap_user_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ], type = dup ['type' ]).values_list ('id' , flat = True ))
105+ ids = list (
106+ aap_user_model .objects .filter (name = dup ['name' ], cluster_id = dup ['cluster_id' ], type = dup ['type' ]).values_list (
107+ 'id' , flat = True ))
116108 primary_id = ids .pop (0 ) # Keep the first ID as primary
117109 jobs_model .objects .filter (launched_by_id__in = ids ).update (launched_by_id = primary_id )
118110 aap_user_model .objects .filter (id__in = ids ).delete ()
119111
120- class Migration (migrations .Migration ):
121112
113+ class Migration (migrations .Migration ):
122114 dependencies = [
123115 ('clusters' , '0014_alter_jobtemplate_time_taken_manually_execute_minutes' ),
124116 ]
0 commit comments