~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
14612.2.6 by William Grant
utilities
15
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
16
import psycopg2
3691.57.44 by Stuart Bishop
Make database creation more robust
17
14612.2.6 by William Grant
utilities
18
3691.57.44 by Stuart Bishop
Make database creation more robust
19
def main():
20
    if len(sys.argv) != 3:
21
        print >> sys.stderr, 'Usage: %s [template] [dbname]' % sys.argv[0]
22
        return 1
23
24
    template, dbname = sys.argv[1:]
25
26
    for attempt in range(0, 10):
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
27
        con = psycopg2.connect('dbname=template1')
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
28
        con.set_isolation_level(0)
3691.57.44 by Stuart Bishop
Make database creation more robust
29
        try:
30
            cur = con.cursor()
31
            cur.execute(
32
                    "CREATE DATABASE %s TEMPLATE = %s ENCODING = 'UTF8'" % (
33
                        dbname, template
34
                        )
35
                    )
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
36
        except psycopg2.Error:
3691.57.44 by Stuart Bishop
Make database creation more robust
37
            if attempt == 9:
38
                raise
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
39
            con.close()
3691.57.44 by Stuart Bishop
Make database creation more robust
40
            time.sleep(1)
41
        else:
42
            return 0
43
    return 1
44
45
if __name__ == '__main__':
46
    sys.exit(main())