Skip to content

Commit

Permalink
use pyodbc instead
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBNinetyOne committed Jul 15, 2024
1 parent 94a63d6 commit 9b1797f
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions mssql/testcontainers/mssql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ class SqlServerContainer(DbContainer):
>>> import sqlalchemy
>>> from testcontainers.mssql import SqlServerContainer
>>> with SqlServerContainer() as mssql:
... engine = sqlalchemy.create_engine(mssql.get_connection_url())
... with engine.begin() as connection:
... result = connection.execute(sqlalchemy.text("select @@VERSION"))
>>> with SqlServerContainer() as mssql:
... engine = sqlalchemy.create_engine(mssql.get_connection_url())
... result = engine.execute(sqlalchemy.text("select @@VERSION"))
Notes
-----
Requires `ODBC Driver 17 for SQL Server <https://docs.microsoft.com/en-us/sql/connect/odbc/
linux-mac/installing-the-microsoft-odbc-driver-for-sql-server>`_.
"""

def __init__(self, image: str = "mcr.microsoft.com/mssql/server:2019-latest",
username: str = "SA", password: Optional[str] = None, port: int = 1433,
dbname: str = "tempdb", dialect: str = 'mssql+pymssql', **kwargs) -> None:
dbname: str = "tempdb", dialect: str = 'mssql+pymssql', driver: str = "ODBC Driver 17 for SQL Server", **kwargs) -> None:
raise_for_deprecated_parameter(kwargs, "user", "username")
super(SqlServerContainer, self).__init__(image, **kwargs)

Expand All @@ -34,6 +37,7 @@ def __init__(self, image: str = "mcr.microsoft.com/mssql/server:2019-latest",
self.username = username
self.dbname = dbname
self.dialect = dialect
self.driver = driver

def _configure(self) -> None:
self.with_env("SA_PASSWORD", self.password)
Expand All @@ -42,7 +46,10 @@ def _configure(self) -> None:
self.with_env("ACCEPT_EULA", 'Y')

def get_connection_url(self) -> str:
return super()._create_connection_url(
base_url = super(SqlServerContainer, self)._create_connection_url(
dialect=self.dialect, username=self.username, password=self.password,
dbname=self.dbname, port=self.port
db_name=self.dbname, port=self.port
)
url = base_url + f"?driver={'+'.join(self.driver.split(' '))}"
return url

0 comments on commit 9b1797f

Please sign in to comment.