Fix series.project migration logic
Stephen Finucane
stephen at that.guru
Fri Jan 27 10:52:48 AEDT 2017
On Wed, 2017-01-25 at 14:17 -0600, Andy Doan wrote:
> 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.
What I was aiming for here was to ensure that the table was left in the
eventual state that the field must require a value (i.e. null=False and
blank=False), but the approach I took didn't do the job. I'll try again
later, but for now this looks fine.
> 2) This may just be a remnant of my development database, but I had
> series entries with no patches.
It doesn't seem necessary to keep empty "containers" around (for that's
all series are, really), but no, there's no harm in it all the same.
> 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>
Reviewed-by: Stephen Finucane <stephen at that.guru>
> ---
> 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.delet
> ion.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
>
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
More information about the Patchwork
mailing list