~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/database/testing/layers.py

  • Committer: Marc Tardif
  • Date: 2011-11-22 20:30:35 UTC
  • Revision ID: marc.tardif@canonical.com-20111122203035-gf3yiv7iqwgwbss0
Fixed login for user created from submission and incorrect CAPTURE devices.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    "DatabaseLayer",
8
8
    ]
9
9
 
10
 
import os
11
 
 
 
10
import atexit
12
11
import transaction
13
12
 
 
13
import psycopg2
 
14
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
 
15
 
14
16
from zope.component import (
15
17
    queryUtility,
16
18
    provideUtility,
41
43
            "schema-package": self._get_schema_package(store),
42
44
            })
43
45
 
44
 
    def setupDatabases(self, drop=False):
 
46
    def setupDatabases(self, drop=True):
45
47
        for data in self._databases:
46
48
            name = data["database"]
47
49
            if drop:
53
55
            name = data["database"]
54
56
            self._dropDatabase(name)
55
57
 
 
58
    def _getConnection(self):
 
59
        return psycopg2.connect("dbname=template1")
 
60
 
56
61
    def _dropDatabase(self, name):
57
 
        os.system("dropdb %s > /dev/null 2>&1" % name)
 
62
        try:
 
63
            connection = self._getConnection()
 
64
        except psycopg2.OperationalError, error:
 
65
            if "does not exist" in str(error):
 
66
                return
 
67
            raise
 
68
 
 
69
        try:
 
70
            connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
 
71
 
 
72
            # Kill all backend connections if this helper happens to be
 
73
            # available. We could create it if it doesn't exist if not
 
74
            # always having this is a problem.
 
75
            try:
 
76
                cursor = connection.cursor()
 
77
                cursor.execute("""
 
78
                    SELECT pg_terminate_backend(procpid)
 
79
                    FROM pg_stat_activity
 
80
                    WHERE procpid <> pg_backend_pid() AND datname=%s
 
81
                    """, (name,))
 
82
            except psycopg2.DatabaseError:
 
83
                pass
 
84
 
 
85
            # Drop the database, trying for a number of seconds in case
 
86
            # connections are slow in dropping off.
 
87
            try:
 
88
                cursor = connection.cursor()
 
89
                cursor.execute("""
 
90
                    DROP DATABASE "%s"
 
91
                    """ % name)
 
92
            except psycopg2.DatabaseError, error:
 
93
                if "does not exist" in str(error):
 
94
                    return
 
95
                raise
 
96
        finally:
 
97
            connection.close()
58
98
 
59
99
    def _createDatabase(self, name):
60
 
        os.system("createdb %s > /dev/null 2>&1" % name)
 
100
        connection = self._getConnection()
 
101
        try:
 
102
            connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
 
103
            cursor = connection.cursor()
 
104
            try:
 
105
                cursor.execute("""
 
106
                    CREATE DATABASE "%s" TEMPLATE=template0 ENCODING='UNICODE'
 
107
                    """ % name)
 
108
                # Try to ensure our cleanup gets invoked, even in
 
109
                # the face of adversity such as the test suite
 
110
                # aborting badly.
 
111
                atexit.register(lambda: self._dropDatabase(name))
 
112
            except psycopg2.DatabaseError, error:
 
113
                if "already exists" in str(error):
 
114
                    return
 
115
                raise
 
116
            finally:
 
117
                cursor.close()
 
118
        finally:
 
119
            connection.close()
61
120
 
62
121
    def setupPatchStores(self, layer):
63
122
        layer._old_patch_zstorm = queryUtility(IZStorm)