~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
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.57.44 by Stuart Bishop
Make database creation more robust
5
6
"""Create a database
7
8
Like createdb, except will retry on failure.
9
."""
10
11
__metaclass__ = type
12
13
import sys
14
import time
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
15
import psycopg2
3691.57.44 by Stuart Bishop
Make database creation more robust
16
17
def main():
18
    if len(sys.argv) != 3:
19
        print >> sys.stderr, 'Usage: %s [template] [dbname]' % sys.argv[0]
20
        return 1
21
22
    template, dbname = sys.argv[1:]
23
24
    for attempt in range(0, 10):
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
25
        con = psycopg2.connect('dbname=template1')
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
26
        con.set_isolation_level(0)
3691.57.44 by Stuart Bishop
Make database creation more robust
27
        try:
28
            cur = con.cursor()
29
            cur.execute(
30
                    "CREATE DATABASE %s TEMPLATE = %s ENCODING = 'UTF8'" % (
31
                        dbname, template
32
                        )
33
                    )
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
34
        except psycopg2.Error:
3691.57.44 by Stuart Bishop
Make database creation more robust
35
            if attempt == 9:
36
                raise
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
37
            con.close()
3691.57.44 by Stuart Bishop
Make database creation more robust
38
            time.sleep(1)
39
        else:
40
            return 0
41
    return 1
42
43
if __name__ == '__main__':
44
    sys.exit(main())