~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
3691.57.44 by Stuart Bishop
Make database creation more robust
2
# Copyright 2006 Canonical Ltd.  All rights reserved.
3
4
"""Create a database
5
6
Like createdb, except will retry on failure.
7
."""
8
9
__metaclass__ = type
10
11
import sys
12
import time
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
13
import psycopg2
3691.57.44 by Stuart Bishop
Make database creation more robust
14
15
def main():
16
    if len(sys.argv) != 3:
17
        print >> sys.stderr, 'Usage: %s [template] [dbname]' % sys.argv[0]
18
        return 1
19
20
    template, dbname = sys.argv[1:]
21
22
    for attempt in range(0, 10):
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
23
        con = psycopg2.connect('dbname=template1')
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
24
        con.set_isolation_level(0)
3691.57.44 by Stuart Bishop
Make database creation more robust
25
        try:
26
            cur = con.cursor()
27
            cur.execute(
28
                    "CREATE DATABASE %s TEMPLATE = %s ENCODING = 'UTF8'" % (
29
                        dbname, template
30
                        )
31
                    )
5821.2.85 by James Henstridge
Add "make check_launchpad_storm_on_merge" target that runs the tests
32
        except psycopg2.Error:
3691.57.44 by Stuart Bishop
Make database creation more robust
33
            if attempt == 9:
34
                raise
4901.1.7 by Stuart Bishop
Possibly robustify pgcreate.py from template-db-in-use issues
35
            con.close()
3691.57.44 by Stuart Bishop
Make database creation more robust
36
            time.sleep(1)
37
        else:
38
            return 0
39
    return 1
40
41
if __name__ == '__main__':
42
    sys.exit(main())