xref: /OK3568_Linux_fs/yocto/bitbake/lib/toaster/toastermain/management/commands/builddelete.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun
5*4882a593Smuzhiyunfrom django.core.management.base import BaseCommand
6*4882a593Smuzhiyunfrom django.core.exceptions import ObjectDoesNotExist
7*4882a593Smuzhiyunfrom orm.models import Build
8*4882a593Smuzhiyunfrom django.db import OperationalError
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun
11*4882a593Smuzhiyunclass Command(BaseCommand):
12*4882a593Smuzhiyun    args    = '<buildID1 buildID2 .....>'
13*4882a593Smuzhiyun    help    = "Deletes selected build(s)"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun    def add_arguments(self, parser):
16*4882a593Smuzhiyun        parser.add_argument('buildids', metavar='N', type=int, nargs='+',
17*4882a593Smuzhiyun                    help="Build ID's to delete")
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun    def handle(self, *args, **options):
20*4882a593Smuzhiyun        for bid in options['buildids']:
21*4882a593Smuzhiyun            try:
22*4882a593Smuzhiyun                b = Build.objects.get(pk = bid)
23*4882a593Smuzhiyun            except ObjectDoesNotExist:
24*4882a593Smuzhiyun                print('build %s does not exist, skipping...' %(bid))
25*4882a593Smuzhiyun                continue
26*4882a593Smuzhiyun            # theoretically, just b.delete() would suffice
27*4882a593Smuzhiyun            # however SQLite runs into problems when you try to
28*4882a593Smuzhiyun            # delete too many rows at once, so we delete some direct
29*4882a593Smuzhiyun            # relationships from Build manually.
30*4882a593Smuzhiyun            for t in b.target_set.all():
31*4882a593Smuzhiyun                t.delete()
32*4882a593Smuzhiyun            for t in b.task_build.all():
33*4882a593Smuzhiyun                t.delete()
34*4882a593Smuzhiyun            for p in b.package_set.all():
35*4882a593Smuzhiyun                p.delete()
36*4882a593Smuzhiyun            for lv in b.layer_version_build.all():
37*4882a593Smuzhiyun                lv.delete()
38*4882a593Smuzhiyun            for v in b.variable_build.all():
39*4882a593Smuzhiyun                v.delete()
40*4882a593Smuzhiyun            for l in b.logmessage_set.all():
41*4882a593Smuzhiyun                l.delete()
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun            # delete the build; some databases might have had problem with migration of the bldcontrol app
44*4882a593Smuzhiyun            retry_count = 0
45*4882a593Smuzhiyun            need_bldcontrol_migration = False
46*4882a593Smuzhiyun            while True:
47*4882a593Smuzhiyun                if retry_count >= 5:
48*4882a593Smuzhiyun                    break
49*4882a593Smuzhiyun                retry_count += 1
50*4882a593Smuzhiyun                if need_bldcontrol_migration:
51*4882a593Smuzhiyun                    from django.core import management
52*4882a593Smuzhiyun                    management.call_command('migrate', 'bldcontrol', interactive=False)
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun                try:
55*4882a593Smuzhiyun                    b.delete()
56*4882a593Smuzhiyun                    break
57*4882a593Smuzhiyun                except OperationalError as e:
58*4882a593Smuzhiyun                    # execute migrations
59*4882a593Smuzhiyun                    need_bldcontrol_migration = True
60*4882a593Smuzhiyun
61