~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/lp/sql.py

Undo rename. Again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Sanity checks for the PostgreSQL database"""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
 
 
9
from canonical.config import config
 
10
from canonical.database.sqlbase import connect
 
11
 
 
12
 
 
13
def confirmEncoding(*args, **kw):
 
14
    '''Raise an exception, explaining what went wrong, if the PostgreSQL
 
15
    database encoding is not UNICODE
 
16
 
 
17
    subsribed to zope.app.appsetup.IProcessStartingEvent
 
18
 
 
19
    '''
 
20
    con = connect(config.launchpad.dbuser)
 
21
    try:
 
22
        cur = con.cursor()
 
23
        dbname = config.database.dbname
 
24
        cur.execute(
 
25
            'select encoding from pg_catalog.pg_database where datname=%s',
 
26
            (dbname,)
 
27
            )
 
28
        res = cur.fetchall()
 
29
        if len(res) != 1:
 
30
            raise RuntimeError('Database %r does not exist or is not unique'
 
31
                    % (dbname,)
 
32
                    )
 
33
        if res[0][0] != 6:
 
34
            raise RuntimeError(
 
35
                "Database %r is using the wrong encidong (%r). You need "
 
36
                "to recreate your database using 'createdb -E UNICODE %s'" % (
 
37
                    dbname, res[0][0], dbname
 
38
                    )
 
39
                )
 
40
    finally:
 
41
        con.close()
 
42
 
 
43
def confirmNoAddMissingFrom(*args, **kw):
 
44
    '''Raise a warning if add_missing_from is turned on (dangerous default).
 
45
 
 
46
    This will become an error in the future. Subscribed to
 
47
    zope.app.appsetup.IProcessStartingEvent
 
48
 
 
49
    '''
 
50
    con = connect(config.launchpad.dbuser)
 
51
    try:
 
52
        cur = con.cursor()
 
53
        cur.execute('show add_missing_from')
 
54
        res = cur.fetchall()
 
55
        if res[0][0] != 'off':
 
56
            raise RuntimeError(
 
57
                    "Need to set add_missing_from=false in "
 
58
                    "/etc/postgresql/postgresql.conf"
 
59
                    )
 
60
    finally:
 
61
        con.close()