summaryrefslogtreecommitdiff
path: root/patchwork/migrations/0016_series_project.py
blob: 8f28ea7f062cae898093ab55d7c324251a8029fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


def forward(apps, schema_editor):
    """Populate the project field from child cover letter/patches."""
    # TODO(stephenfin): Perhaps we'd like to include an SQL variant of the
    # below though I'd imagine it would be rather tricky
    Series = apps.get_model('patchwork', 'Series')

    for series in Series.objects.all():
        if series.cover_letter:
            series.project = series.cover_letter.project
            series.save()
        elif series.patches:
            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.
            series.delete()


def reverse(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    # This is necessary due to a mistake made when writing the migration.
    # PostgreSQL does not allow mixing of schema and data migrations within the
    # same transaction. Disabling transactions ensures this doesn't happen.
    # Refer to bug #104 for more information.
    atomic = False

    dependencies = [
        ('patchwork', '0015_add_series_models'),
    ]

    operations = [
        migrations.AddField(
            model_name='series',
            name='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, atomic=False),
        migrations.AlterField(
            model_name='seriesreference',
            name='msgid',
            field=models.CharField(max_length=255),
        ),
        migrations.AlterUniqueTogether(
            name='seriesreference', unique_together=set([('series', 'msgid')]),
        ),
    ]