Skip to content

Commit 185317d

Browse files
feat: Refactor MSSQL connection driver import handling
1 parent e896e09 commit 185317d

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

sqlmesh/core/config/connection.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,40 +1444,26 @@ def _mssql_engine_import_validator(cls, data: t.Any) -> t.Any:
14441444
if not isinstance(data, dict):
14451445
return data
14461446

1447-
check_import = str_to_bool(str(data.pop("check_import", True)))
1448-
if not check_import:
1449-
return data
1450-
14511447
driver = data.get("driver", "pymssql")
14521448

1453-
try:
1454-
if driver == "pymssql":
1455-
importlib.import_module("pymssql")
1456-
elif driver == "pyodbc":
1457-
importlib.import_module("pyodbc")
1458-
else:
1459-
raise ValueError(f"Unsupported driver: {driver}")
1460-
except ImportError:
1461-
if debug_mode_enabled():
1462-
raise
1449+
# Define the mapping of driver to import module and extra name
1450+
driver_configs = {"pymssql": ("pymssql", "mssql"), "pyodbc": ("pyodbc", "mssql-odbc")}
14631451

1464-
logger.exception("Failed to import the MSSQL engine library")
1452+
if driver not in driver_configs:
1453+
raise ValueError(f"Unsupported driver: {driver}")
14651454

1466-
if driver == "pymssql":
1467-
extra_name = "mssql"
1468-
elif driver == "pyodbc":
1469-
extra_name = "mssql-odbc"
1470-
else:
1471-
extra_name = "mssql"
1455+
import_module, extra_name = driver_configs[driver]
14721456

1473-
raise ConfigError(
1474-
f"Failed to import the '{driver}' library for MSSQL connections. This may be due to a missing "
1475-
"or incompatible installation. Please ensure the required dependency is installed by "
1476-
f'running: `pip install "sqlmesh[{extra_name}]"`. For more details, check the logs '
1477-
"in the 'logs/' folder, or rerun the command with the '--debug' flag."
1478-
)
1457+
# Conditionally delegate to the existing _get_engine_import_validator
1458+
# Create a validator for the specific driver and call its inner function
1459+
validator_func = _get_engine_import_validator(import_module, driver, extra_name)
14791460

1480-
return data
1461+
# Extract the inner validate function from the decorated validator
1462+
# The validator_func has a __wrapped__ attribute that contains the original function
1463+
inner_validate = getattr(validator_func, "__wrapped__", validator_func)
1464+
1465+
# Call the inner validation function directly
1466+
return inner_validate(cls, data)
14811467

14821468
@property
14831469
def _connection_kwargs_keys(self) -> t.Set[str]:

tests/core/test_connection_config.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,49 +1133,26 @@ class TestConfigC(PydanticModel):
11331133

11341134
def test_mssql_engine_import_validator():
11351135
"""Test that MSSQL import validator respects driver configuration."""
1136-
with pytest.raises(
1137-
ConfigError,
1138-
match=re.escape(
1139-
"Failed to import the 'pyodbc' library for MSSQL connections. This may be due to a missing "
1140-
"or incompatible installation. Please ensure the required dependency is installed by "
1141-
'running: `pip install "sqlmesh[mssql-odbc]"`. For more details, check the logs '
1142-
"in the 'logs/' folder, or rerun the command with the '--debug' flag."
1143-
),
1144-
):
1145-
# Test PyODBC driver suggests mssql-odbc extra
1136+
1137+
# Test PyODBC driver suggests mssql-odbc extra when import fails
1138+
with pytest.raises(ConfigError, match=r"pip install \"sqlmesh\[mssql-odbc\]\""):
11461139
with patch("importlib.import_module") as mock_import:
11471140
mock_import.side_effect = ImportError("No module named 'pyodbc'")
11481141
MSSQLConnectionConfig(host="localhost", driver="pyodbc")
11491142

1150-
with pytest.raises(
1151-
ConfigError,
1152-
match=re.escape(
1153-
"Failed to import the 'pymssql' library for MSSQL connections. This may be due to a missing "
1154-
"or incompatible installation. Please ensure the required dependency is installed by "
1155-
'running: `pip install "sqlmesh[mssql]"`. For more details, check the logs '
1156-
"in the 'logs/' folder, or rerun the command with the '--debug' flag."
1157-
),
1158-
):
1159-
# Test PyMSSQL driver suggests mssql extra
1143+
# Test PyMSSQL driver suggests mssql extra when import fails
1144+
with pytest.raises(ConfigError, match=r"pip install \"sqlmesh\[mssql\]\""):
11601145
with patch("importlib.import_module") as mock_import:
11611146
mock_import.side_effect = ImportError("No module named 'pymssql'")
11621147
MSSQLConnectionConfig(host="localhost", driver="pymssql")
11631148

1164-
with pytest.raises(
1165-
ConfigError,
1166-
match=re.escape(
1167-
"Failed to import the 'pymssql' library for MSSQL connections. This may be due to a missing "
1168-
"or incompatible installation. Please ensure the required dependency is installed by "
1169-
'running: `pip install "sqlmesh[mssql]"`. For more details, check the logs '
1170-
"in the 'logs/' folder, or rerun the command with the '--debug' flag."
1171-
),
1172-
):
1173-
# Test default driver (pymssql) suggests mssql extra
1149+
# Test default driver (pymssql) suggests mssql extra when import fails
1150+
with pytest.raises(ConfigError, match=r"pip install \"sqlmesh\[mssql\]\""):
11741151
with patch("importlib.import_module") as mock_import:
11751152
mock_import.side_effect = ImportError("No module named 'pymssql'")
11761153
MSSQLConnectionConfig(host="localhost") # No driver specified
11771154

1178-
# Test successful import doesn't raise exception
1155+
# Test successful import works without error
11791156
with patch("importlib.import_module") as mock_import:
11801157
mock_import.return_value = None
11811158
config = MSSQLConnectionConfig(host="localhost", driver="pyodbc")

0 commit comments

Comments
 (0)