PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/baruwa/messages/management/commands/dbclean.py

https://bitbucket.org/datopdog/baruwa
Python | 50 lines | 14 code | 5 blank | 31 comment | 0 complexity | 5f00560771dd8332f1822ce19cd858b1 MD5 | raw file
Possible License(s): GPL-2.0
 1# 
 2# Baruwa - Web 2.0 MailScanner front-end.
 3# Copyright (C) 2010-2011  Andrew Colin Kissa <andrew@topdog.za.net>
 4# 
 5# This program is free software; you can redistribute it and/or modify
 6# it under the terms of the GNU General Public License as published by
 7# the Free Software Foundation; either version 2 of the License, or
 8# (at your option) any later version.
 9# 
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14# 
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19# vim: ai ts=4 sts=4 et sw=4
20#
21
22from django.core.management.base import NoArgsCommand
23from django.utils.translation import ugettext as _
24
25
26class Command(NoArgsCommand):
27    "Archive messages and delete from messages table"
28    help = _("Deletes records older than 60 days from the messages table")
29
30    def handle_noargs(self, **options):
31        from django.db import connection
32        #import datetime
33        #from baruwa.messages.models import Message
34        #interval = datetime.timedelta(days=60)
35        #last_date = datetime.datetime.now() - interval
36        #Message.objects.filter(timestamp__lt=last_date).delete()
37
38        conn = connection.cursor()
39        conn.execute(
40            """INSERT LOW_PRIORITY INTO archive 
41            SELECT * FROM messages WHERE timestamp < 
42            DATE_SUB(CURDATE(), INTERVAL 60 DAY)"""
43        )
44        conn.execute(
45            """DELETE LOW_PRIORITY FROM messages 
46            WHERE timestamp < DATE_SUB(CURDATE(), 
47            INTERVAL 60 DAY)"""
48        )
49        conn.execute('OPTIMIZE TABLE messages')
50        conn.execute('OPTIMIZE TABLE archive')