-
Notifications
You must be signed in to change notification settings - Fork 327
add support async postgres driver #320
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
Changes from 2 commits
716a1ea
4faa31c
0b4df38
03a8f04
00d247e
5d1941b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,14 @@ | |
import os | ||
from typing import Optional | ||
from testcontainers.core.generic import DbContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready | ||
|
||
ADDITIONAL_TRANSIENT_ERRORS = [] | ||
try: | ||
from sqlalchemy.exc import DBAPIError | ||
ADDITIONAL_TRANSIENT_ERRORS.append(DBAPIError) | ||
except ImportError: | ||
pass | ||
|
||
|
||
class PostgresContainer(DbContainer): | ||
|
@@ -41,10 +49,14 @@ class PostgresContainer(DbContainer): | |
POSTGRES_USER = os.environ.get("POSTGRES_USER", "test") | ||
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test") | ||
POSTGRES_DB = os.environ.get("POSTGRES_DB", "test") | ||
DEFAULT_DRIVER = "psycopg2" | ||
|
||
def __init__(self, image: str = "postgres:latest", port: int = 5432, user: Optional[str] = None, | ||
password: Optional[str] = None, dbname: Optional[str] = None, | ||
driver: str = "psycopg2", **kwargs) -> None: | ||
fourteekey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
driver: Optional[str] = None, **kwargs) -> None: | ||
if driver is None: | ||
driver = self.DEFAULT_DRIVER | ||
|
||
super(PostgresContainer, self).__init__(image=image, **kwargs) | ||
self.POSTGRES_USER = user or self.POSTGRES_USER | ||
self.POSTGRES_PASSWORD = password or self.POSTGRES_PASSWORD | ||
|
@@ -54,14 +66,24 @@ def __init__(self, image: str = "postgres:latest", port: int = 5432, user: Optio | |
|
||
self.with_exposed_ports(self.port_to_expose) | ||
|
||
@wait_container_is_ready(*ADDITIONAL_TRANSIENT_ERRORS) | ||
def _connect(self) -> None: | ||
import sqlalchemy | ||
engine = sqlalchemy.create_engine(self.get_connection_url(driver=self.DEFAULT_DRIVER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fourteekey, could you please address this question? |
||
conn = engine.connect() | ||
conn.close() | ||
|
||
def _configure(self) -> None: | ||
self.with_env("POSTGRES_USER", self.POSTGRES_USER) | ||
self.with_env("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD) | ||
self.with_env("POSTGRES_DB", self.POSTGRES_DB) | ||
|
||
def get_connection_url(self, host=None) -> str: | ||
def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] = None) -> str: | ||
if driver is None: | ||
driver = self.driver | ||
|
||
return super()._create_connection_url( | ||
dialect="postgresql+{}".format(self.driver), username=self.POSTGRES_USER, | ||
dialect="postgresql+{}".format(driver), username=self.POSTGRES_USER, | ||
password=self.POSTGRES_PASSWORD, db_name=self.POSTGRES_DB, host=host, | ||
port=self.port_to_expose, | ||
) |
Uh oh!
There was an error while loading. Please reload this page.