Skip to content

Commit 52a9795

Browse files
fix(tests): Restored output converter.
- Added a TODO re its removal as it should already be supported natively by `mssql-python`.
1 parent f2328b8 commit 52a9795

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

sqlmesh/core/config/connection.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,8 @@ def _connection_factory(self) -> t.Callable:
16271627
if not SUPPORTS_MSSQL_PYTHON_DRIVER:
16281628
raise ConfigError("The `mssql-python` driver requires Python 3.10 or higher.")
16291629

1630+
import mssql_python
1631+
16301632
def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16311633
# Extract parameters for connection string
16321634
host = kwargs.pop("host")
@@ -1689,10 +1691,32 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16891691
# Create the connection
16901692
conn_str = ";".join(conn_str_parts)
16911693

1692-
import mssql_python
1693-
16941694
conn = mssql_python.connect(conn_str, autocommit=kwargs.get("autocommit", False))
16951695

1696+
# TODO: Remove this output converter as DATETIMEOFFSET
1697+
# should be handled natively by `mssql-python`.
1698+
# see "https://github.com/microsoft/mssql-python/issues/213"
1699+
1700+
def handle_datetimeoffset_mssql_python(dto_value: t.Any) -> t.Any:
1701+
import struct
1702+
from datetime import datetime, timedelta, timezone
1703+
1704+
# Unpack the DATETIMEOFFSET binary format:
1705+
# Format: <6hI2h = (year, month, day, hour, minute, second, nanoseconds, tz_hour_offset, tz_minute_offset)
1706+
tup = struct.unpack("<6hI2h", dto_value)
1707+
return datetime(
1708+
tup[0],
1709+
tup[1],
1710+
tup[2],
1711+
tup[3],
1712+
tup[4],
1713+
tup[5],
1714+
tup[6] // 1000,
1715+
timezone(timedelta(hours=tup[7], minutes=tup[8])),
1716+
)
1717+
1718+
conn.add_output_converter(-155, handle_datetimeoffset_mssql_python)
1719+
16961720
return conn
16971721

16981722
return connect_mssql_python

0 commit comments

Comments
 (0)