[Patchew-devel] [PATCH 2/4] models: populate denormalized project field

Paolo Bonzini posted 4 patches 1 year, 9 months ago
[Patchew-devel] [PATCH 2/4] models: populate denormalized project field
Posted by Paolo Bonzini 1 year, 9 months ago
The field is now populated and need not be nullable.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/migrations/0067_result_denormalize.py | 32 +++++++++++++++++++++++
 api/migrations/0068_auto_20220919_1007.py | 19 ++++++++++++++
 api/models.py                             |  2 +-
 3 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 api/migrations/0067_result_denormalize.py
 create mode 100644 api/migrations/0068_auto_20220919_1007.py

diff --git a/api/migrations/0067_result_denormalize.py b/api/migrations/0067_result_denormalize.py
new file mode 100644
index 0000000..8394256
--- /dev/null
+++ b/api/migrations/0067_result_denormalize.py
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations
+from django.db.models import F, Subquery, OuterRef
+
+
+def populate_denormalized_project(apps, schema_editor):
+    Message = apps.get_model("api", "Message")
+    MessageResult = apps.get_model("api", "MessageResult")
+    ProjectResult = apps.get_model("api", "ProjectResult")
+    ProjectResult.objects.filter(project_denorm__isnull=True).update(
+        project_denorm=
+            Subquery(ProjectResult.objects.filter(result_ptr_id=OuterRef('pk')).values('project')[:1]))
+    MessageResult.objects.filter(project_denorm__isnull=True).update(
+        project_denorm=
+            Subquery(MessageResult.objects.filter(result_ptr_id=OuterRef('pk')).values('message__project')[:1]))
+
+
+def populate_projectresult_project(apps, schema_editor):
+    ProjectResult = apps.get_model("api", "ProjectResult")
+    ProjectResult.update(project=F('project_denorm'))
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [("api", "0066_auto_20220919_1004")]
+
+    operations = [
+        migrations.RunPython(populate_denormalized_project, populate_projectresult_project)
+    ]
+
diff --git a/api/migrations/0068_auto_20220919_1007.py b/api/migrations/0068_auto_20220919_1007.py
new file mode 100644
index 0000000..6e070a4
--- /dev/null
+++ b/api/migrations/0068_auto_20220919_1007.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.1.14 on 2022-09-19 10:07
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('api', '0067_result_denormalize'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='result',
+            name='project_denorm',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='api.project'),
+        ),
+    ]
diff --git a/api/models.py b/api/models.py
index 45c7f70..0728853 100644
--- a/api/models.py
+++ b/api/models.py
@@ -64,7 +64,7 @@ class Result(models.Model):
     data = jsonfield.JSONField(default={})
 
     project_denorm = models.ForeignKey(
-        "Project", related_name="+", on_delete=models.CASCADE, null=True
+        "Project", related_name="+", on_delete=models.CASCADE
     )
 
     class Meta:
-- 
2.37.2

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel