Skip to content

Commit abe7d7d

Browse files
committed
Chore: Make default connection optional
1 parent 18911b3 commit abe7d7d

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

sqlmesh/core/config/root.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Config(BaseConfig):
122122
"""
123123

124124
gateways: GatewayDict = {"": GatewayConfig()}
125-
default_connection: SerializableConnectionConfig = DuckDBConnectionConfig()
125+
default_connection: t.Optional[SerializableConnectionConfig] = None
126126
default_test_connection_: t.Optional[SerializableConnectionConfig] = Field(
127127
default=None, alias="default_test_connection"
128128
)
@@ -280,7 +280,11 @@ def get_gateway(self, name: t.Optional[str] = None) -> GatewayConfig:
280280
return self.gateways
281281

282282
def get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
283-
return self.get_gateway(gateway_name).connection or self.default_connection
283+
connection = self.get_gateway(gateway_name).connection or self.default_connection
284+
if connection is None:
285+
msg = f" for gateway '{gateway_name}'" if gateway_name else ""
286+
raise ConfigError(f"No connection configured{msg}.")
287+
return connection
284288

285289
def get_state_connection(
286290
self, gateway_name: t.Optional[str] = None

tests/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
import typing as t
2+
13
from sqlmesh.core import constants as c
24
from sqlmesh.core.analytics import disable_analytics
5+
from sqlmesh.core.config import Config, DuckDBConnectionConfig
6+
from sqlmesh.core.config.connection import ConnectionConfig
37

48
c.MAX_FORK_WORKERS = 1
59
disable_analytics()
10+
11+
12+
# Replace the implementation of Config._get_connection with a new one that always
13+
# returns a ConnectionConfig to simplify testing
14+
original_get_connection = Config.get_connection
15+
16+
17+
def _lax_get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
18+
try:
19+
connection = original_get_connection(self, gateway_name)
20+
except:
21+
connection = DuckDBConnectionConfig()
22+
return connection
23+
24+
25+
setattr(Config, "original_get_connection", original_get_connection)
26+
setattr(Config, "get_connection", _lax_get_connection)

tests/core/test_integration.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
SnapshotTableInfo,
6565
)
6666
from sqlmesh.utils.date import TimeLike, now, to_date, to_datetime, to_timestamp
67-
from sqlmesh.utils.errors import NoChangesPlanError, SQLMeshError, PlanError
67+
from sqlmesh.utils.errors import NoChangesPlanError, SQLMeshError, PlanError, ConfigError
6868
from sqlmesh.utils.pydantic import validate_string
6969
from tests.conftest import DuckDBMetadata, SushiDataValidator
7070
from tests.utils.test_helpers import use_terminal_console
@@ -6129,3 +6129,23 @@ def setup_senario(model_before: str, model_after: str):
61296129
'Binder Error: Referenced column "this_col_does_not_exist" not found in \nFROM clause!'
61306130
in output.stdout
61316131
)
6132+
6133+
@pytest.mark.isolated
6134+
def test_missing_connection_config():
6135+
# This is the actual implementation of Config._get_connection which would be used in a real project
6136+
# To make testing easier, it's mocked at tests/__init__.py
6137+
6138+
with mock.patch.object(Config, "get_connection", Config.original_get_connection):
6139+
# Case 1: No default_connection or gateways specified should raise a ConfigError
6140+
with pytest.raises(ConfigError):
6141+
ctx = Context(config=Config())
6142+
6143+
# Case 2: No connection specified in the gateway should raise a ConfigError
6144+
with pytest.raises(ConfigError):
6145+
ctx = Context(config=Config(gateways={"default": GatewayConfig()}))
6146+
6147+
# Case 3: Specifying a default_connection or connection in the gateway should work
6148+
ctx = Context(config=Config(default_connection=DuckDBConnectionConfig()))
6149+
ctx = Context(
6150+
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
6151+
)

0 commit comments

Comments
 (0)