Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | from django.core.management.base import BaseCommand, CommandError |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 2 | from django.core.exceptions import ObjectDoesNotExist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | from orm.models import Build |
| 4 | from django.db import OperationalError |
| 5 | import os |
| 6 | |
| 7 | |
| 8 | |
| 9 | class Command(BaseCommand): |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 10 | args = '<buildID1 buildID2 .....>' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | help = "Deletes selected build(s)" |
| 12 | |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 13 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | # 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 | |