Skip to content

Commit

Permalink
Updating pyodbc to work on windows OS
Browse files Browse the repository at this point in the history
  • Loading branch information
tim.reichard committed Feb 11, 2021
1 parent 24640ad commit 22b2406
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ History
=======


v0.10.4 (2021-02-011)
-----------------------

* Add pyodbc driver string for windows OS.


v0.10.3 (2021-02-08)
-----------------------

Expand Down
24 changes: 18 additions & 6 deletions aioradio/pyodbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
# pylint: disable=too-many-arguments

import os
import platform
from typing import Any, List, Union

import pyodbc

OPERATING_SYSTEM = platform.system()

# driver location varies based on OS. add to this list if necessary...
UNIXODBC_DRIVER_PATHS = [
'/usr/lib/libtdsodbc.so',
Expand Down Expand Up @@ -39,7 +42,7 @@ async def establish_pyodbc_connection(
host: str,
user: str,
pwd: str,
port: int=1433,
port: int=None,
database: str='',
driver: str='',
autocommit: bool=False
Expand All @@ -62,11 +65,20 @@ async def establish_pyodbc_connection(
pyodbc.Connection: database connection object
"""

verified_driver = await get_unixodbc_driver_path([driver]) if driver else await get_unixodbc_driver_path(UNIXODBC_DRIVER_PATHS)
if verified_driver is None:
raise FileNotFoundError('Unable to locate unixodbc driver file: libtdsodbc.so')

conn_string = f'DRIVER={verified_driver};SERVER={host};PORT={port};UID={user};PWD={pwd};TDS_Version=8.0'
verified_driver = None
if OPERATING_SYSTEM == 'Windows':
verified_driver = driver
else:
if driver and not driver.startswith('{'):
verified_driver = await get_unixodbc_driver_path([driver])
else:
verified_driver = await get_unixodbc_driver_path(UNIXODBC_DRIVER_PATHS)
if verified_driver is None:
raise FileNotFoundError('Unable to locate unixodbc driver file: libtdsodbc.so')

conn_string = f'DRIVER={verified_driver};SERVER={host};UID={user};PWD={pwd};TDS_Version=8.0'
if port is not None:
conn_string += f';PORT={port}'
if database:
conn_string += f';DATABASE={database}'

Expand Down
6 changes: 3 additions & 3 deletions aioradio/tests/file_ingestion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import pytest

from aioradio.file_ingestion import (async_db_wrapper, async_wrapper, delete_ftp_file,
establish_ftp_connection,
from aioradio.file_ingestion import (async_db_wrapper, async_wrapper,
delete_ftp_file, establish_ftp_connection,
get_current_datetime_from_timestamp,
list_ftp_objects,
send_emails_via_mandrill,
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_async_wrapper(user):

if user != 'tim.reichard':
pytest.skip('Skip test_async_wrapper since user is not Tim Reichard')

@async_wrapper
async def func():
return 'Hello World'
Expand Down
12 changes: 6 additions & 6 deletions aioradio/tests/pyodbc_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""pytest pyodbc script."""

import os
import json

import pytest

from aioradio.aws.secrets import get_secret
from aioradio.pyodbc import (establish_pyodbc_connection,
pyodbc_query_fetchall, pyodbc_query_fetchone)

pytestmark = pytest.mark.asyncio

CREDS = {'mssql_host': os.getenv('MSSQL_HOST'), 'mssql_user': os.getenv('MSSQL_USER'), 'mssql_pwd': os.getenv('MSSQL_PW')}


@pytest.mark.xfail
async def test_bad_unixodbc_driver(github_action):
Expand All @@ -19,8 +18,8 @@ async def test_bad_unixodbc_driver(github_action):
if github_action:
pytest.skip('Skip test_bad_unixodbc_driver when running via Github Action')

driver = '/usr/lib/bogus.so'
await establish_pyodbc_connection(host=CREDS['mssql_host'], user=CREDS['mssql_user'], pwd=CREDS['mssql_pwd'], driver=driver)
creds = json.loads(await get_secret('production/airflowCluster/sqloltp', 'us-east-1'))
await establish_pyodbc_connection(**creds, driver='/usr/lib/bogus.so')


async def test_pyodbc_query_fetchone_and_fetchall(github_action):
Expand All @@ -33,7 +32,8 @@ async def test_pyodbc_query_fetchone_and_fetchall(github_action):
if github_action:
pytest.skip('Skip test_pyodbc_query_fetchone_and_fetchall when running via Github Action')

conn = await establish_pyodbc_connection(host=CREDS['mssql_host'], user=CREDS['mssql_user'], pwd=CREDS['mssql_pwd'])
creds = json.loads(await get_secret('production/airflowCluster/sqloltp', 'us-east-1'))
conn = await establish_pyodbc_connection(**creds)
query = "SELECT EFIemails FROM DataStage.dbo.EESFileuploadAssignments WHERE FICE = '003800' AND FileCategory = 'EnrollmentLens'"

row = await pyodbc_query_fetchone(conn=conn, query=query)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
long_description = fileobj.read()

setup(name='aioradio',
version='0.10.3',
version='0.10.4',
description='Generic asynchronous i/o python utilities for AWS services (SQS, S3, DynamoDB, Secrets Manager), Redis, MSSQL (pyodbc), JIRA and more',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 22b2406

Please sign in to comment.