~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/pgmassacre.py

  • Committer: Stuart Bishop
  • Date: 2011-10-05 07:02:01 UTC
  • mto: (9893.9.18 distinct-db-users)
  • mto: This revision was merged to the branch mainline in revision 14108.
  • Revision ID: stuart.bishop@canonical.com-20111005070201-m3kejbltg0g71gzo
delint

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import time
18
18
import psycopg2
19
19
import psycopg2.extensions
20
 
from signal import SIGTERM, SIGQUIT, SIGKILL, SIGINT
21
20
from optparse import OptionParser
22
21
 
23
22
 
36
35
    transactions.
37
36
    """
38
37
    con = connect(database)
39
 
    con.set_isolation_level(0) # Autocommit so we can ROLLBACK PREPARED.
 
38
    con.set_isolation_level(0)  # Autocommit so we can ROLLBACK PREPARED.
40
39
    cur = con.cursor()
41
40
 
42
41
    # Get a list of outstanding prepared transactions.
57
56
    rollback.
58
57
    """
59
58
    con = connect()
60
 
    con.set_isolation_level(0) # Autocommit.
 
59
    con.set_isolation_level(0)  # Autocommit.
61
60
    cur = con.cursor()
62
61
    # Keep checking until the timeout is reached, returning True if all
63
62
    # of the backends are gone.
72
71
            """, vars())
73
72
        if cur.fetchone() is None:
74
73
            return False
75
 
        time.sleep(0.6) # Stats only updated every 500ms.
 
74
        time.sleep(0.6)  # Stats only updated every 500ms.
76
75
    con.close()
77
76
    return True
78
77
 
79
78
 
80
79
def massacre(database):
81
80
    con = connect()
82
 
    con.set_isolation_level(0) # Autocommit
 
81
    con.set_isolation_level(0)  # Autocommit
83
82
    cur = con.cursor()
84
83
 
85
84
    # Allow connections to the doomed database if something turned this off,
109
108
            FROM pg_stat_activity
110
109
            WHERE datname=%s AND procpid <> pg_backend_pid()
111
110
            """, [database])
112
 
        for procpid,success in cur.fetchall():
 
111
        for procpid, success in cur.fetchall():
113
112
            if not success:
114
113
                print >> sys.stderr, (
115
114
                    "pg_terminate_backend(%s) failed" % procpid)
125
124
        # AUTOCOMMIT required to execute commands like DROP DATABASE.
126
125
        con.set_isolation_level(0)
127
126
        cur = con.cursor()
128
 
        cur.execute("DROP DATABASE %s" % database) # Not quoted.
 
127
        cur.execute("DROP DATABASE %s" % database)  # Not quoted.
129
128
        con.close()
130
129
        return 0
131
130
    finally:
151
150
    now = start
152
151
    error_msg = None
153
152
    con = connect()
154
 
    con.set_isolation_level(0) # Autocommit required for CREATE DATABASE.
 
153
    con.set_isolation_level(0)  # Autocommit required for CREATE DATABASE.
155
154
    create_db_cmd = """
156
155
        CREATE DATABASE %s WITH ENCODING='UTF8' TEMPLATE=%s
157
156
        """ % (database, template)
170
169
            return 0
171
170
        except psycopg2.Error, exception:
172
171
            error_msg = str(exception)
173
 
        time.sleep(0.6) # Stats only updated every 500ms.
 
172
        time.sleep(0.6)  # Stats only updated every 500ms.
174
173
        now = time.time()
175
174
    con.close()
176
175
 
196
195
 
197
196
options = None
198
197
 
 
198
 
199
199
def main():
200
200
    parser = OptionParser("Usage: %prog [options] DBNAME")
201
201
    parser.add_option("-U", "--user", dest="user", default=None,