Skip to content

Commit 052ebb2

Browse files
committed
Ensure Report.updated_at is the greatest between the current value and the last history.
1 parent a73e721 commit 052ebb2

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

tigaserver_app/migrations/0044_auto_20240124_1533.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import django.contrib.gis.db.models.fields
77
from django.db import migrations, models, transaction
88
from django.db.models import OuterRef, Subquery
9-
from django.db.models.functions import Greatest
109
from django.db.utils import IntegrityError
1110
import django.db.models.deletion
1211

@@ -222,24 +221,6 @@ def populate_report_history_table(apps, schema_editor):
222221

223222
return
224223

225-
# NOTE: this gets blocked...
226-
print("Updating Report.updated_at with last modification date (if necessary)")
227-
Report.objects.annotate(
228-
last_update=Greatest(
229-
models.F('updated_at'),
230-
Subquery(
231-
HistoricalReport.objects.filter(
232-
**{Report._meta.pk.name: OuterRef('pk')}
233-
).order_by().annotate(
234-
# NOTE: Using models.Func to avoid adding GROUP BY
235-
# See: https://stackoverflow.com/questions/42543978/django-1-11-annotating-a-subquery-aggregate/69020732#69020732
236-
last_history_date=models.Func(models.F('history_date'), function='MAX')
237-
).values('last_history_date')
238-
)
239-
)
240-
).update(updated_at=models.F('last_update'))
241-
242-
243224

244225
class Migration(migrations.Migration):
245226

tigaserver_app/migrations/0045_auto_20240124_1543.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
from django.db import migrations, models
44

5+
from django.db.models import OuterRef, Subquery
6+
from django.db.models.functions import Greatest
7+
8+
def update_report_updated_at_field(apps, schema_editor):
9+
HistoricalReport = apps.get_model("tigaserver_app", "HistoricalReport")
10+
Report = apps.get_model("tigaserver_app", "Report")
11+
12+
print("Updating Report.updated_at with last modification date (if necessary)")
13+
Report.objects.annotate(
14+
last_update=Greatest(
15+
models.F('updated_at'),
16+
Subquery(
17+
HistoricalReport.objects.filter(
18+
**{Report._meta.pk.name: OuterRef('pk')}
19+
).order_by('-history_date').values('history_date')[:1]
20+
)
21+
)
22+
).update(updated_at=models.F('last_update'))
523

624
class Migration(migrations.Migration):
725

@@ -10,6 +28,7 @@ class Migration(migrations.Migration):
1028
]
1129

1230
operations = [
31+
migrations.RunPython(update_report_updated_at_field, migrations.RunPython.noop),
1332
migrations.AddConstraint(
1433
model_name='report',
1534
constraint=models.CheckConstraint(check=models.Q(models.Q(('deleted_at__isnull', True), ('version_number', 0)), models.Q(('deleted_at__isnull', False), ('version_number', -1)), _connector='OR'), name='version_number_constraint'),

0 commit comments

Comments
 (0)