Skip to content

Commit 4412fc9

Browse files
fix(mssql): update driver selection logic to allow enforcing pyodbc in Fabric
1 parent deb9321 commit 4412fc9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

sqlmesh/core/config/connection.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,14 @@ def _mssql_engine_import_validator(cls, data: t.Any) -> t.Any:
15101510
if not isinstance(data, dict):
15111511
return data
15121512

1513-
driver = data.get("driver", "pymssql")
1513+
# Get the default driver for this specific class
1514+
default_driver = "pymssql"
1515+
if hasattr(cls, "model_fields") and "driver" in cls.model_fields:
1516+
field_info = cls.model_fields["driver"]
1517+
if hasattr(field_info, "default") and field_info.default is not None:
1518+
default_driver = field_info.default
1519+
1520+
driver = data.get("driver", default_driver)
15141521

15151522
# Define the mapping of driver to import module and extra name
15161523
driver_configs = {"pymssql": ("pymssql", "mssql"), "pyodbc": ("pyodbc", "mssql-odbc")}

tests/core/test_connection_config.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,3 +1766,50 @@ def test_fabric_pyodbc_connection_string_generation():
17661766

17671767
# Check autocommit parameter, should default to True for Fabric
17681768
assert call_args[1]["autocommit"] is True
1769+
1770+
1771+
def test_mssql_driver_defaults(make_config):
1772+
"""Test driver defaults for MSSQL connection config.
1773+
1774+
Ensures MSSQL defaults to 'pymssql' but can be overridden to 'pyodbc'.
1775+
"""
1776+
1777+
# Test 1: MSSQL with no driver specified - should default to pymssql
1778+
config_no_driver = make_config(type="mssql", host="localhost", check_import=False)
1779+
assert isinstance(config_no_driver, MSSQLConnectionConfig)
1780+
assert config_no_driver.driver == "pymssql"
1781+
1782+
# Test 2: MSSQL with explicit pymssql driver
1783+
config_pymssql = make_config(
1784+
type="mssql", host="localhost", driver="pymssql", check_import=False
1785+
)
1786+
assert isinstance(config_pymssql, MSSQLConnectionConfig)
1787+
assert config_pymssql.driver == "pymssql"
1788+
1789+
# Test 3: MSSQL with explicit pyodbc driver
1790+
config_pyodbc = make_config(type="mssql", host="localhost", driver="pyodbc", check_import=False)
1791+
assert isinstance(config_pyodbc, MSSQLConnectionConfig)
1792+
assert config_pyodbc.driver == "pyodbc"
1793+
1794+
1795+
def test_fabric_driver_defaults(make_config):
1796+
"""Test driver defaults for Fabric connection config.
1797+
1798+
Ensures Fabric defaults to 'pyodbc' and cannot be changed to 'pymssql'.
1799+
"""
1800+
1801+
# Test 1: Fabric with no driver specified - should default to pyodbc
1802+
config_no_driver = make_config(type="fabric", host="localhost", check_import=False)
1803+
assert isinstance(config_no_driver, FabricConnectionConfig)
1804+
assert config_no_driver.driver == "pyodbc"
1805+
1806+
# Test 2: Fabric with explicit pyodbc driver
1807+
config_pyodbc = make_config(
1808+
type="fabric", host="localhost", driver="pyodbc", check_import=False
1809+
)
1810+
assert isinstance(config_pyodbc, FabricConnectionConfig)
1811+
assert config_pyodbc.driver == "pyodbc"
1812+
1813+
# Test 3: Fabric with pymssql driver should fail (not allowed)
1814+
with pytest.raises(ConfigError, match=r"Input should be 'pyodbc'"):
1815+
make_config(type="fabric", host="localhost", driver="pymssql", check_import=False)

0 commit comments

Comments
 (0)