Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Try using pymssql if the ODBC driver is unavailable #879

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Build the stack
run: docker-compose up -d mysql postgres presto trino clickhouse vertica
run: docker-compose up -d mysql postgres presto trino clickhouse vertica mssql

- name: Install Poetry
run: pip install poetry

- name: Install package
run: "poetry install"

- name: "Install MSSQL SDK for Python"
run: poetry install -E mssql

# BigQuery start
# - id: 'auth'
# uses: 'google-github-actions/auth@v1'
Expand All @@ -68,6 +71,7 @@ jobs:
DATADIFF_CLICKHOUSE_URI: 'clickhouse://clickhouse:Password1@localhost:9000/clickhouse'
DATADIFF_VERTICA_URI: 'vertica://vertica:Password1@localhost:5433/vertica'
DATADIFF_REDSHIFT_URI: '${{ secrets.DATADIFF_REDSHIFT_URI }}'
DATADIFF_MSSQL_URI: 'mssql://SA:Password123mssql@localhost:1433/master/dbo'
MOTHERDUCK_TOKEN: '${{ secrets.MOTHERDUCK_TOKEN }}'
run: |
chmod +x tests/waiting_for_stack_up.sh
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/ci_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Build the stack
run: docker-compose up -d mysql postgres presto trino clickhouse vertica
run: docker-compose up -d mysql postgres presto trino clickhouse vertica mssql

- name: Install Poetry
run: pip install poetry

- name: Install package
run: "poetry install"

- name: "Install MSSQL SDK for Python"
run: poetry install -E mssql

# BigQuery start
# - id: 'auth'
# uses: 'google-github-actions/auth@v1'
Expand All @@ -64,6 +67,7 @@ jobs:
DATADIFF_VERTICA_URI: 'vertica://vertica:Password1@localhost:5433/vertica'
# DATADIFF_BIGQUERY_URI: '${{ secrets.DATADIFF_BIGQUERY_URI }}'
DATADIFF_REDSHIFT_URI: '${{ secrets.DATADIFF_REDSHIFT_URI }}'
DATADIFF_MSSQL_URI: 'mssql://SA:Password123mssql@localhost:1433/master/dbo'
MOTHERDUCK_TOKEN: '${{ secrets.MOTHERDUCK_TOKEN }}'
run: |
chmod +x tests/waiting_for_stack_up.sh
Expand Down
39 changes: 33 additions & 6 deletions data_diff/databases/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,27 @@
)


def _check_odbc_driver_installed():
import pyodbc

if "ODBC Driver 18 for SQL Server" in pyodbc.drivers():
return True
return False


odbc_driver_installed = _check_odbc_driver_installed()


@import_helper("mssql")
def import_mssql():
import pyodbc
if odbc_driver_installed:
import pyodbc

return pyodbc
return pyodbc
else:
import pymssql

return pymssql


@attrs.define(frozen=False)
Expand Down Expand Up @@ -170,23 +186,30 @@ class MsSQL(ThreadedDatabase):
default_database: str
_args: Dict[str, Any]
_mssql: Any
_autocommit: bool

def __init__(self, host, port, user, password, *, database, thread_count, **kw) -> None:
super().__init__(thread_count=thread_count)

args = dict(server=host, port=port, database=database, user=user, password=password, **kw)
self._args = {k: v for k, v in args.items() if v is not None}
self._args["driver"] = "{ODBC Driver 18 for SQL Server}"

# TODO temp dev debug
self._args["TrustServerCertificate"] = "yes"
self._autocommit = False

try:
self.default_database = self._args["database"]
self.default_schema = self._args["schema"]
except KeyError:
raise ValueError("Specify a default database and schema.")

if odbc_driver_installed:
self._args["driver"] = "{ODBC Driver 18 for SQL Server}"

# TODO temp dev debug
self._args["TrustServerCertificate"] = "yes"
else:
self._autocommit = True
self._args.pop("schema")

self._mssql = None

def create_connection(self):
Expand Down Expand Up @@ -227,3 +250,7 @@ def _query_cursor(self, c, sql_code: str):
return super()._query_cursor(c, sql_code)
except self._mssql.DatabaseError as e:
raise QueryError(e)

@property
def is_autocommit(self) -> bool:
return self._autocommit
4 changes: 4 additions & 0 deletions dev/dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ VERTICA_DB_NAME=vertica
# leave VMART_DIR and VMART_ETL_SCRIPT empty.
VMART_DIR=
VMART_ETL_SCRIPT=

# MSSQL credentials
ACCEPT_EULA=Y
MSSQL_SA_PASSWORD=Password123mssql
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,27 @@ services:
networks:
- local

mssql:
container_name: dd-mssql
image: mcr.microsoft.com/mssql/server
restart: always
volumes:
- mssql-data:/var/opt/mssql
ports:
- '1433:1433'
env_file:
- dev/dev.env
tty: true
networks:
- local


volumes:
postgresql-data:
mysql-data:
clickhouse-data:
vertica-data:
mssql-data:

networks:
local:
Expand Down
83 changes: 80 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ vertica-python = {version="*", optional=true}
urllib3 = "<2"
oracledb = {version = "*", optional=true}
pyodbc = {version=">=4.0.39", optional=true}
pymssql = {version=">=2.2.11", optional=true}
typing-extensions = ">=4.0.1"
attrs = ">=23.1.0"
mashumaro = {version = ">=2.9,<3.11.0", extras = ["msgpack"]}
Expand Down Expand Up @@ -74,15 +75,15 @@ redshift = ["psycopg2"]
snowflake = ["snowflake-connector-python", "cryptography"]
presto = ["presto-python-client"]
oracle = ["oracledb"]
mssql = ["pyodbc"]
mssql = ["pyodbc", "pymssql"]
# databricks = ["databricks-sql-connector"]
trino = ["trino"]
clickhouse = ["clickhouse-driver"]
vertica = ["vertica-python"]
duckdb = ["duckdb"]
all-dbs = [
"preql", "mysql-connector-python", "psycopg2", "snowflake-connector-python", "cryptography", "presto-python-client",
"oracledb", "pyodbc", "trino", "clickhouse-driver", "vertica-python", "duckdb"
"oracledb", "pyodbc", "pymssql", "trino", "clickhouse-driver", "vertica-python", "duckdb"
]

[tool.poetry.group.dev.dependencies]
Expand Down
Loading