Skip to content

Commit

Permalink
Merge pull request #186 from bnavigator/psycopg-2.9-context
Browse files Browse the repository at this point in the history
pytest-server-fixtures: Don't use context manager for CREATE DATABASE
  • Loading branch information
eeaston authored Nov 19, 2021
2 parents 8c5cdb9 + 291995b commit ef24320
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 ef24320

Please sign in to comment.