Skip to content

Commit aeee536

Browse files
Merge branch 'fix/group-images-into-sessions' of https://github.com/RolnickLab/antenna into fix/group-images-into-sessions
2 parents e0fcadc + 05ce90c commit aeee536

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

ami/main/migrations/0061_remove_event_unique_event_and_more.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
from django.db import migrations, models
44

55

6+
def populate_group_by_on_rollback(apps, schema_editor):
7+
"""Populate group_by field with start date for rollback compatibility."""
8+
Event = apps.get_model("main", "Event")
9+
10+
# Group events by deployment to handle uniqueness
11+
from collections import defaultdict
12+
13+
deployment_counters = defaultdict(lambda: defaultdict(int))
14+
15+
for event in Event.objects.all().order_by("deployment_id", "start"):
16+
if event.start:
17+
date_key = event.start.date().isoformat()
18+
deployment_counters[event.deployment_id][date_key] += 1
19+
counter = deployment_counters[event.deployment_id][date_key]
20+
21+
# Make group_by unique by appending counter if needed
22+
if counter == 1:
23+
event.group_by = date_key
24+
else:
25+
event.group_by = f"{date_key}-{counter}"
26+
27+
event.save(update_fields=["group_by"])
28+
29+
630
class Migration(migrations.Migration):
731
dependencies = [
832
("main", "0060_alter_sourceimagecollection_method"),
@@ -17,6 +41,25 @@ class Migration(migrations.Migration):
1741
model_name="event",
1842
name="main_event_group_b_6ce666_idx",
1943
),
44+
# Step 1: Make the field nullable with a default for rollback safety
45+
migrations.AlterField(
46+
model_name="event",
47+
name="group_by",
48+
field=models.CharField(
49+
db_index=True,
50+
help_text="A unique identifier for this event, used to group images into events.",
51+
max_length=255,
52+
null=True,
53+
blank=True,
54+
default=None,
55+
),
56+
),
57+
# Step 2: Populate data on rollback
58+
migrations.RunPython(
59+
code=migrations.RunPython.noop,
60+
reverse_code=populate_group_by_on_rollback,
61+
),
62+
# Step 3: Remove the field
2063
migrations.RemoveField(
2164
model_name="event",
2265
name="group_by",

0 commit comments

Comments
 (0)