~launchpad-pqm/launchpad/devel

10637.3.7 by Guilherme Salgado
merge devel
1
#!/usr/bin/python -S
8687.15.9 by Karl Fogel
Add the copyright header block to more files (everything under database/).
2
#
7675.1239.1 by Jeroen Vermeulen
The database/schema portion of my lint branch; must land in db-devel.
3
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
8687.15.9 by Karl Fogel
Add the copyright header block to more files (everything under database/).
4
# GNU Affero General Public License version 3 (see the file LICENSE).
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
5
6
"""Prune old nonces."""
7
8
__metaclass__ = type
9
__all__ = []
10
7675.1239.1 by Jeroen Vermeulen
The database/schema portion of my lint branch; must land in db-devel.
11
from optparse import OptionParser
12
13980.2.6 by Jeroen Vermeulen
Cut away the lint fixes in database/schema; they must go to db-devel.
13
import _pythonpath
14
7675.1239.1 by Jeroen Vermeulen
The database/schema portion of my lint branch; must land in db-devel.
15
from canonical.database.sqlbase import (
16
    connect,
17
    ISOLATION_LEVEL_AUTOCOMMIT,
18
    )
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
19
from canonical.launchpad.scripts import db_options
14565.2.13 by Curtis Hovey
Moved canonial.launchapd.script tests and modules to lp.services.script.
20
from lp.services.scripts.logger import (
7675.1239.1 by Jeroen Vermeulen
The database/schema portion of my lint branch; must land in db-devel.
21
    log,
22
    logger_options,
23
    )
24
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
25
26
def update_until_done(con, table, query, vacuum_every=100):
27
    log.info("Running %s" % query)
28
    loops = 0
29
    total_rows = 0
30
    cur = con.cursor()
31
    while True:
32
        loops += 1
33
        cur.execute(query)
34
        rowcount = cur.rowcount
35
        total_rows += rowcount
36
        log.debug("Updated %d" % total_rows)
37
        if vacuum_every is not None and loops % vacuum_every == 0:
38
            log.debug("Vacuuming %s" % table)
39
            cur.execute("VACUUM %s" % table)
40
        if rowcount <= 0:
41
            log.info("Done")
42
            return
43
44
parser = OptionParser()
45
logger_options(parser)
46
db_options(parser)
47
options, args = parser.parse_args()
48
13879.1.3 by William Grant
Drop now-obsolete connect(user) args.
49
con = connect(isolation=ISOLATION_LEVEL_AUTOCOMMIT)
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
50
51
update_until_done(con, 'OAuthNonce', """
52
    DELETE FROM OAuthNonce
53
    WHERE id IN (
54
        SELECT id FROM OAuthNonce
55
        WHERE request_timestamp < 'yesterday'
56
        LIMIT 5000)
57
    """, vacuum_every=None)