Skip to content

Commit c5903fc

Browse files
committed
fix(tests): Restored output converter.
- Added a TODO re its removal as it should already be supported natively by `mssql-python`. Signed-off-by: walanguzzi <walanguzzi@outlook.com>
1 parent 734fb2c commit c5903fc

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

sqlmesh/core/config/connection.py

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

1639+
import mssql_python
1640+
16391641
def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16401642
# Extract parameters for connection string
16411643
host = kwargs.pop("host")
@@ -1698,10 +1700,32 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16981700
# Create the connection
16991701
conn_str = ";".join(conn_str_parts)
17001702

1701-
import mssql_python
1702-
17031703
conn = mssql_python.connect(conn_str, autocommit=kwargs.get("autocommit", False))
17041704

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

17071731
return connect_mssql_python

0 commit comments

Comments
 (0)