Skip to content

Commit c664382

Browse files
authored
Fix migration (#196)
1 parent 86274b2 commit c664382

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

src/backend/apps/clusters/migrations/0015_auto_20251020_1008.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Generated by Django 4.2.17 on 2025-10-20 10:08
22

33
from django.db import migrations, transaction
4-
from django.db.models import Count, Q
4+
from django.db.models import Count
5+
56

67
def 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

7158
def 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+
8776
def 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
]

src/backend/apps/clusters/migrations/0016_alter_aapuser_index_together_and_more.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
# Generated by Django 4.2.17 on 2025-10-21 06:04
22

3-
from django.db import migrations
3+
from django.db import migrations, transaction
4+
from django.db.models import Min
45

56

6-
class Migration(migrations.Migration):
7+
def unique_negative_external_ids(apps, schema_editor):
8+
models = [
9+
{
10+
"app_model": apps.get_model('clusters', 'Organization'),
11+
},
12+
{
13+
"app_model": apps.get_model('clusters', 'JobTemplate'),
14+
},
15+
{
16+
"app_model": apps.get_model('clusters', 'AAPUser'),
17+
},
18+
{
19+
"app_model": apps.get_model('clusters', 'Inventory'),
20+
},
21+
{
22+
"app_model": apps.get_model('clusters', 'ExecutionEnvironment'),
23+
},
24+
{
25+
"app_model": apps.get_model('clusters', 'InstanceGroup'),
26+
},
27+
{
28+
"app_model": apps.get_model('clusters', 'Project'),
29+
},
30+
{
31+
"app_model": apps.get_model('clusters', 'Label'),
32+
},
33+
{
34+
"app_model": apps.get_model('clusters', 'Host'),
35+
},
36+
]
37+
with transaction.atomic():
38+
for model in models:
39+
app_model = model['app_model']
40+
negative_ids = app_model.objects.filter(external_id__lt=0).order_by('internal_created')
41+
_min = app_model.objects.all().aggregate(Min('external_id'))
42+
min_id = _min['external_id__min'] if _min['external_id__min'] is not None else 0
43+
for index, obj in enumerate(negative_ids):
44+
min_id = min_id - 1
45+
obj.external_id = min_id
46+
obj.save()
747

48+
49+
class Migration(migrations.Migration):
850
dependencies = [
951
('clusters', '0015_auto_20251020_1008'),
1052
]
1153

1254
operations = [
55+
migrations.RunPython(unique_negative_external_ids),
1356
migrations.AlterIndexTogether(
1457
name='aapuser',
1558
index_together=set(),

0 commit comments

Comments
 (0)