blob: ff93e549df3256675cb2b2b23ccd5d5aa974a704 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001from django.core.management.base import BaseCommand, CommandError
Patrick Williamsd7e96312015-09-22 08:09:05 -05002from django.core.exceptions import ObjectDoesNotExist
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003from orm.models import Build
4from django.db import OperationalError
5import os
6
7
8
9class Command(BaseCommand):
Patrick Williamsd7e96312015-09-22 08:09:05 -050010 args = '<buildID1 buildID2 .....>'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011 help = "Deletes selected build(s)"
12
Patrick Williamsd7e96312015-09-22 08:09:05 -050013 def handle(self, *args, **options):
14 for bid in args:
15 try:
16 b = Build.objects.get(pk = bid)
17 except ObjectDoesNotExist:
18 print 'build %s does not exist, skipping...' %(bid)
19 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 # theoretically, just b.delete() would suffice
21 # however SQLite runs into problems when you try to
22 # delete too many rows at once, so we delete some direct
23 # relationships from Build manually.
24 for t in b.target_set.all():
25 t.delete()
26 for t in b.task_build.all():
27 t.delete()
28 for p in b.package_set.all():
29 p.delete()
30 for lv in b.layer_version_build.all():
31 lv.delete()
32 for v in b.variable_build.all():
33 v.delete()
34 for l in b.logmessage_set.all():
35 l.delete()
36
37 # delete the build; some databases might have had problem with migration of the bldcontrol app
38 retry_count = 0
39 need_bldcontrol_migration = False
40 while True:
41 if retry_count >= 5:
42 break
43 retry_count += 1
44 if need_bldcontrol_migration:
45 from django.core import management
46 management.call_command('migrate', 'bldcontrol', interactive=False)
47
48 try:
49 b.delete()
50 break
51 except OperationalError as e:
52 # execute migrations
53 need_bldcontrol_migration = True
54