Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest-server-fixtures: Don't use context manager for CREATE DATABASE #186

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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