~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
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
3691.170.2 by Stuart Bishop
Add simple script to list empty tables
5
6
"""List empty database tables."""
7
8
__metaclass__ = type
9
10
import _pythonpath
14606.3.4 by William Grant
Replace canonical.database usage everywhere, and format-imports.
11
3691.170.2 by Stuart Bishop
Add simple script to list empty tables
12
from optparse import OptionParser
13
14606.3.4 by William Grant
Replace canonical.database usage everywhere, and format-imports.
14
from fti import quote_identifier
15
from lp.services.database.sqlbase import connect
14565.2.15 by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts.
16
from lp.services.scripts import db_options
3691.170.2 by Stuart Bishop
Add simple script to list empty tables
17
13879.1.3 by William Grant
Drop now-obsolete connect(user) args.
18
3691.170.2 by Stuart Bishop
Add simple script to list empty tables
19
def main(options):
13879.1.3 by William Grant
Drop now-obsolete connect(user) args.
20
    con = connect()
3691.170.2 by Stuart Bishop
Add simple script to list empty tables
21
    cur = con.cursor()
22
    cur.execute("""
23
        SELECT relname FROM pg_class,pg_namespace
24
        WHERE pg_class.relnamespace = pg_namespace.oid
25
            AND pg_namespace.nspname='public'
26
            AND pg_class.relkind = 'r'
27
        ORDER BY relname
28
        """)
29
    for table in (row[0] for row in cur.fetchall()):
30
        cur.execute(
31
                "SELECT TRUE FROM public.%s LIMIT 1" % quote_identifier(table)
32
                )
33
        if cur.fetchone() is None:
34
            print table
35
36
37
if __name__ == '__main__':
38
    parser = OptionParser()
39
    db_options(parser)
40
    (options, args) = parser.parse_args()
41
42
    main(options)