Fix series.project migration logic

Andy Doan andy.doan at linaro.org
Thu Jan 26 07:17:29 AEDT 2017


The migration logic seems slightly off for 2 reasons:

1) If you already have series objects in your database, then adding a
non-nullable foreign key to projects will fail.

2) This may just be a remnant of my development database, but I had
series entries with no patches.

I think part #1 is *required*. I'm not totally sure about #2, but it
seems safe.

Signed-off-by: Andy Doan <andy.doan at linaro.org>
---
 patchwork/migrations/0016_series_project.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/patchwork/migrations/0016_series_project.py b/patchwork/migrations/0016_series_project.py
index befd695..c4874f9 100644
--- a/patchwork/migrations/0016_series_project.py
+++ b/patchwork/migrations/0016_series_project.py
@@ -16,8 +16,10 @@ def forward(apps, schema_editor):
             series.project = series.cover_letter.project
             series.save()
         elif series.patches:
-            series.project = series.patches.first().project
-            series.save()
+            patch = series.patches.first()
+            if patch:
+                series.project = patch.project
+                series.save()
         else:
             # a series without patches or cover letters should not exist.
             # Delete it.
@@ -38,7 +40,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='series',
             name='project',
-            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='series', to='patchwork.Project'),
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='series', to='patchwork.Project'),
         ),
         migrations.RunPython(forward, reverse),
         migrations.AlterField(
--
2.7.4



More information about the Patchwork mailing list