aboutsummaryrefslogtreecommitdiffstats
path: root/lib/toaster/orm/migrations/0012_use_release_instead_of_up_branch.py
blob: 0e6bb83311509fdbf3eb06b1a4556e0ca9f649b9 (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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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


def branch_to_release(apps, schema_editor):
    Layer_Version = apps.get_model('orm', 'Layer_Version')
    Release = apps.get_model('orm', 'Release')

    print("Converting all layer version up_branches to releases")
    # Find all the layer versions which have an upbranch and convert them to
    # the release that they're for.
    for layer_version in Layer_Version.objects.filter(
            Q(release=None) & ~Q(up_branch=None)):
        try:
            # HEAD and local are equivalent
            if "HEAD" in layer_version.up_branch.name:
                release = Release.objects.get(name="local")
                layer_version.commit = "HEAD"
                layer_version.branch = "HEAD"
            else:
                release = Release.objects.get(
                    name=layer_version.up_branch.name)

            layer_version.release = release
            layer_version.save()
        except Exception as e:
            print("Couldn't work out an appropriate release for %s "
                  "the up_branch was %s "
                  "user the django admin interface to correct it" %
                  (layer_version.layer.name, layer_version.up_branch.name))
            print(e)

            continue


class Migration(migrations.Migration):

    dependencies = [
        ('orm', '0011_delete_layersource'),
    ]

    operations = [
        migrations.AddField(
            model_name='layer_version',
            name='release',
            field=models.ForeignKey(to='orm.Release', default=None, null=True),
        ),
        migrations.RunPython(branch_to_release,
                             reverse_code=migrations.RunPython.noop),

        migrations.RemoveField(
            model_name='layer_version',
            name='up_branch',
        ),

        migrations.DeleteModel(
            name='Branch',
        ),
    ]