aboutsummaryrefslogtreecommitdiffstats
path: root/Post/management/commands/culldb.py
blob: 698537c43c3abc040b08e8b2c1d9e2703ffec3ad (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
# error-reporting-tool -  culldb
#
# Copyright (C) 2015 Intel Corporation
#
# Licensed under the MIT license, see COPYING.MIT for details

# Create your views here.
# vi: tabstop=8 expandtab shiftwidth=4 softtabstop=4

from django.core.management.base import BaseCommand, CommandError
from Post.models import Build, BuildFailure
from optparse import make_option
import time
import sys

class Command(BaseCommand):
    help = 'Culls the database to size in rows'
    args = '<size>'
    option_list = BaseCommand.option_list + (
        make_option('-i',
                    '--info',
                    dest='info',
                    action="store_true",
                    help='Show the current database size'),
    )

    def handle(self, *args, **options):
        count = Build.objects.count()

        if options['info']:
            print "Current builds table size: %d" % Build.objects.count()
            return

        if len(args) > 0 and args[0]:
            try:
                new_size = int(args[0])
            except ValueError:
                print "Not a valid size"
                return


            num_to_delete = count - new_size
            print "\nReducing the database size to %d which will DELETE %d rows" % (new_size, num_to_delete)
            i = 1
            while i != 0:
                i = i-1
                print 'Ctrl+c TO CANCEL. Executing in... %d \r' % i,
                sys.stdout.flush()
                time.sleep(1)

            q = Build.objects.all()[:num_to_delete].values_list('pk',
                                                                flat=True)
            Build.objects.filter(pk__in=list(q)).delete()