Skip to content

Commit

Permalink
[DCS-195] add sql server integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Subhankar authored and Subhankar committed Jul 3, 2024
1 parent a8bddfa commit e68d12d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ Please visit the [Quick Start Guide](https://docs.datachecks.io/getting_started/
Datachecks supports sql and search data sources. Below are the list of supported data sources.

| Data Source | Type | Supported |
| ----------------------------------------------------------------------- | ---------------------- | ---------- |
|-------------------------------------------------------------------------|------------------------|------------|
| [Postgres](https://docs.datachecks.io/integrations/postgres/) | Transactional Database | :thumbsup: |
| [MySql](https://docs.datachecks.io/integrations/mysql/) | Transactional Database | :thumbsup: |
| MS SQL Server | Transactional Database | :soon: |
| MS SQL Server | Transactional Database | :thumbsup: |
| [OpenSearch](https://docs.datachecks.io/integrations/opensearch/) | Search Engine | :thumbsup: |
| [Elasticsearch](https://docs.datachecks.io/integrations/elasticsearch/) | Search Engine | :thumbsup: |
| [GCP BigQuery](https://docs.datachecks.io/integrations/bigquery/) | Data Warehouse | :thumbsup: |
| [DataBricks](https://docs.datachecks.io/integrations/databricks/) | Data Warehouse | :thumbsup: |
| Snowflake | Data Warehouse | :soon: |
| Snowflake | Data Warehouse | :thumbsup: |
| [AWS RedShift](https://docs.datachecks.io/integrations/redshift/) | Data Warehouse | :thumbsup: |

## Metric Types
Expand Down
2 changes: 2 additions & 0 deletions datachecks/core/common/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class DataSourceConnectionConfiguration:
warehouse: Optional[str] = None # Snowflake specific configuration
role: Optional[str] = None # Snowflake specific configuration

driver: Optional[str] = None # SQL Server specific configuration


@dataclass
class DataSourceConfiguration:
Expand Down
1 change: 1 addition & 0 deletions datachecks/core/datasource/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DataSourceManager:
"databricks": "DatabricksDataSource",
"redshift": "RedShiftDataSource",
"snowflake": "SnowFlakeDataSource",
"mssql": "MssqlDataSource",
}

def __init__(self, config: Dict[str, DataSourceConfiguration]):
Expand Down
59 changes: 59 additions & 0 deletions datachecks/integrations/databases/mssql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2022-present, the Waterdip Labs Pvt. Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict

from sqlalchemy import create_engine
from sqlalchemy.engine import URL

from datachecks.core.common.errors import DataChecksDataSourcesConnectionError
from datachecks.core.datasource.sql_datasource import SQLDataSource


class MssqlDataSource(SQLDataSource):
def __init__(self, data_source_name: str, data_connection: Dict):
super().__init__(data_source_name, data_connection)

def connect(self) -> Any:
"""
Connect to the data source
"""
try:
url = URL.create(
drivername="mssql+pyodbc",
username=self.data_connection.get("username"),
password=self.data_connection.get("password"),
host=self.data_connection.get("host"),
port=self.data_connection.get("port", 1433),
database=self.data_connection.get("database"),
)
schema = self.data_connection.get("schema") or "dbo"
# For osx have to install
# brew install unixodbc
# brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
# brew update
# brew install msodbcsql mssql-tools
driver = self.data_connection.get("driver", "ODBC Driver 17 for SQL Server")
url_updated = f"{url}?driver={driver}"
engine = create_engine(
url=url_updated,
connect_args={"options": f"-csearch_path={schema}"},
isolation_level="AUTOCOMMIT",
)
self.connection = engine.connect()
return self.connection
except Exception as e:
raise DataChecksDataSourcesConnectionError(
message=f"Failed to connect to Mssql data source: [{str(e)}]"
)
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ python-dateutil = "^2.8.2"
loguru = "^0.7.0"
rich = "^13.5.2"
pyparsing = "^3.1.1"
numpy = "1.26.4"
sqlalchemy = "1.4.49"
sqlalchemy-bigquery = { version="^1.8.0", optional=true }
opensearch-py = { version="^2.2.0", optional=true }
Expand All @@ -41,7 +42,7 @@ elasticsearch = { version="^7.17.3", optional=true }
pymysql = { version="^1.1.0", optional=true, extras=["rsa"] }
sqlalchemy-redshift = { version="^0.8.14", optional=true }
snowflake-sqlalchemy = { version="^1.5.3", optional=true }
numpy = "1.26.4"
pyodbc = { version="^5.1.0", optional=true }


[tool.poetry.group.dev.dependencies]
Expand Down Expand Up @@ -78,7 +79,8 @@ all = [
"databricks-sql-connector",
"elasticsearch",
"sqlalchemy-redshift",
"snowflake-sqlalchemy"
"snowflake-sqlalchemy",
"pyodbc"
]

[tool.poetry.scripts]
Expand Down

0 comments on commit e68d12d

Please sign in to comment.