[Patchew-devel] [PATCH 1/4] models: add denormalized Project link to Result

Paolo Bonzini posted 4 patches 1 year, 9 months ago
[Patchew-devel] [PATCH 1/4] models: add denormalized Project link to Result
Posted by Paolo Bonzini 1 year, 9 months ago
This is needed to handle the series/unapplied queries efficiently.

The ProjectResult.project field is generalized and moved to Result.
Because the ProjectResult.project field will be removed, make it
temporarily NULL so that the migration can work backwards and
populate it from Result.project.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/migrations/0066_auto_20220919_1004.py | 24 +++++++++++++++++++++++
 api/models.py                             | 10 +++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 api/migrations/0066_auto_20220919_1004.py

diff --git a/api/migrations/0066_auto_20220919_1004.py b/api/migrations/0066_auto_20220919_1004.py
new file mode 100644
index 0000000..548481b
--- /dev/null
+++ b/api/migrations/0066_auto_20220919_1004.py
@@ -0,0 +1,24 @@
+# Generated by Django 3.1.14 on 2022-09-19 10:04
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('api', '0065_auto_20220411_1153'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='result',
+            name='project_denorm',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='api.project'),
+        ),
+        migrations.AlterField(
+            model_name='projectresult',
+            name='project',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='results', to='api.project'),
+        ),
+    ]
diff --git a/api/models.py b/api/models.py
index cd0285d..45c7f70 100644
--- a/api/models.py
+++ b/api/models.py
@@ -63,6 +63,10 @@ class Result(models.Model):
     log_entry = models.OneToOneField(LogEntry, on_delete=models.CASCADE, null=True)
     data = jsonfield.JSONField(default={})
 
+    project_denorm = models.ForeignKey(
+        "Project", related_name="+", on_delete=models.CASCADE, null=True
+    )
+
     class Meta:
         index_together = [("status", "name")]
 
@@ -327,12 +331,12 @@ class Project(models.Model):
         return len(updated_series)
 
     def create_result(self, **kwargs):
-        return ProjectResult(project=self, **kwargs)
+        return ProjectResult(project=self, project_denorm=self, **kwargs)
 
 
 class ProjectResult(Result):
     project = models.ForeignKey(
-        Project, related_name="results", on_delete=models.CASCADE
+        Project, related_name="results", on_delete=models.CASCADE, null=True
     )
 
     @property
@@ -938,7 +942,7 @@ class Message(models.Model):
         emit_event("SeriesMerged", project=self.project, series=self)
 
     def create_result(self, **kwargs):
-        return MessageResult(message=self, **kwargs)
+        return MessageResult(message=self, project_denorm=self.project, **kwargs)
 
     def __str__(self):
         return self.project.name + "/" + self.subject
-- 
2.37.2

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