Skip to content

Commit ee5850c

Browse files
committed
fix: add dialect parameter instead of hardcode
1 parent 932ee30 commit ee5850c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/mysql/testcontainers/mysql/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MySqlContainer(DbContainer):
3131
The example will spin up a MySql database to which you can connect with the credentials
3232
passed in the constructor. Alternatively, you may use the :code:`get_connection_url()`
3333
method which returns a sqlalchemy-compatible url in format
34-
:code:`dialect+driver://username:password@host:port/database`.
34+
:code:`mysql+dialect://username:password@host:port/database`.
3535
3636
.. doctest::
3737
@@ -64,6 +64,7 @@ class MySqlContainer(DbContainer):
6464
def __init__(
6565
self,
6666
image: str = "mysql:latest",
67+
dialect: Optional[str] = None,
6768
username: Optional[str] = None,
6869
root_password: Optional[str] = None,
6970
password: Optional[str] = None,
@@ -84,6 +85,7 @@ def __init__(
8485
self.root_password = root_password or environ.get("MYSQL_ROOT_PASSWORD", "test")
8586
self.password = password or environ.get("MYSQL_PASSWORD", "test")
8687
self.dbname = dbname or environ.get("MYSQL_DATABASE", "test")
88+
self.dialect = dialect or environ.get("MYSQL_DIALECT", None)
8789

8890
if self.username == "root":
8991
self.root_password = self.password
@@ -104,8 +106,13 @@ def _connect(self) -> None:
104106
)
105107

106108
def get_connection_url(self) -> str:
109+
dialect = "mysql"
110+
111+
if self.dialect is None:
112+
dialect = f"mysql+{self.dialect}"
113+
107114
return super()._create_connection_url(
108-
dialect="mysql+pymysql", username=self.username, password=self.password, dbname=self.dbname, port=self.port
115+
dialect=dialect, username=self.username, password=self.password, dbname=self.dbname, port=self.port
109116
)
110117

111118
def _transfer_seed(self) -> None:

0 commit comments

Comments
 (0)