Skip to content

Commit 291995b

Browse files
committed
Don't use context manager for CREATE DATABASE
Psycopg 2.9 uses transaction blocks withing context managers, which is not allowed for CREATE DATABASE psycopg/psycopg2#941
1 parent 2929621 commit 291995b

File tree

1 file changed

+8
-4
lines changed
  • pytest-server-fixtures/pytest_server_fixtures

1 file changed

+8
-4
lines changed

pytest-server-fixtures/pytest_server_fixtures/postgres.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,22 @@ def run_cmd(self):
103103

104104
def check_server_up(self):
105105
from psycopg2 import OperationalError
106+
conn = None
106107
try:
107108
print("Connecting to Postgres at localhost:{}".format(self.port))
108-
with self.connect('postgres') as conn:
109-
conn.set_session(autocommit=True)
110-
with conn.cursor() as cursor:
111-
cursor.execute("CREATE DATABASE " + self.database_name)
109+
conn = self.connect('postgres')
110+
conn.set_session(autocommit=True)
111+
with conn.cursor() as cursor:
112+
cursor.execute("CREATE DATABASE " + self.database_name)
112113
self.connection = self.connect(self.database_name)
113114
with open(self.workspace / 'db' / 'postmaster.pid', 'r') as f:
114115
self.pid = int(f.readline().rstrip())
115116
return True
116117
except OperationalError as e:
117118
print("Could not connect to test postgres: {}".format(e))
119+
finally:
120+
if conn:
121+
conn.close()
118122
return False
119123

120124
def connect(self, database=None):

0 commit comments

Comments
 (0)