1# -*- coding: utf-8 -*- 2from __future__ import unicode_literals 3 4from django.db import migrations, models 5from django.db.models import Q 6 7 8def branch_to_release(apps, schema_editor): 9 Layer_Version = apps.get_model('orm', 'Layer_Version') 10 Release = apps.get_model('orm', 'Release') 11 12 print("Converting all layer version up_branches to releases") 13 # Find all the layer versions which have an upbranch and convert them to 14 # the release that they're for. 15 for layer_version in Layer_Version.objects.filter( 16 Q(release=None) & ~Q(up_branch=None)): 17 try: 18 # HEAD and local are equivalent 19 if "HEAD" in layer_version.up_branch.name: 20 release = Release.objects.get(name="local") 21 layer_version.commit = "HEAD" 22 layer_version.branch = "HEAD" 23 else: 24 release = Release.objects.get( 25 name=layer_version.up_branch.name) 26 27 layer_version.release = release 28 layer_version.save() 29 except Exception as e: 30 print("Couldn't work out an appropriate release for %s " 31 "the up_branch was %s " 32 "user the django admin interface to correct it" % 33 (layer_version.layer.name, layer_version.up_branch.name)) 34 print(e) 35 36 continue 37 38 39class Migration(migrations.Migration): 40 41 dependencies = [ 42 ('orm', '0011_delete_layersource'), 43 ] 44 45 operations = [ 46 migrations.AddField( 47 model_name='layer_version', 48 name='release', 49 field=models.ForeignKey(to='orm.Release', default=None, null=True, on_delete=models.CASCADE), 50 ), 51 migrations.RunPython(branch_to_release, 52 reverse_code=migrations.RunPython.noop), 53 54 migrations.RemoveField( 55 model_name='layer_version', 56 name='up_branch', 57 ), 58 59 migrations.DeleteModel( 60 name='Branch', 61 ), 62 ] 63