Skip to content

Commit

Permalink
Don't use context manager for CREATE DATABASE
Browse files Browse the repository at this point in the history
Psycopg 2.9 does uses transaction blocks withing context managers,
which is not allowed for CREATE DATABASE
psycopg/psycopg2#941
  • Loading branch information
bnavigator authored Oct 17, 2021
1 parent 2929621 commit b86de32
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pytest-server-fixtures/pytest_server_fixtures/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ def run_cmd(self):

def check_server_up(self):
from psycopg2 import OperationalError
conn = None
try:
print("Connecting to Postgres at localhost:{}".format(self.port))
with self.connect('postgres') as conn:
conn.set_session(autocommit=True)
with conn.cursor() as cursor:
cursor.execute("CREATE DATABASE " + self.database_name)
conn = self.connect('postgres')
conn.set_session(autocommit=True)
with conn.cursor() as cursor:
cursor.execute("CREATE DATABASE " + self.database_name)
self.connection = self.connect(self.database_name)
with open(self.workspace / 'db' / 'postmaster.pid', 'r') as f:
self.pid = int(f.readline().rstrip())
return True
except OperationalError as e:
print("Could not connect to test postgres: {}".format(e))
finally:
if conn:
conn.close()
return False

def connect(self, database=None):
Expand Down

0 comments on commit b86de32

Please sign in to comment.