Skip to content

Commit d565154

Browse files
authored
Chore!: Make default_connection optional (#4522)
1 parent 9012790 commit d565154

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

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/conftest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

3+
34
import datetime
45
import logging
56
import typing as t
67
import uuid
8+
from contextlib import nullcontext
79
from pathlib import Path
810
from shutil import copytree, rmtree
911
from tempfile import TemporaryDirectory
@@ -21,7 +23,8 @@
2123
from sqlglot.helper import ensure_list
2224
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
2325

24-
from sqlmesh.core.config import BaseDuckDBConnectionConfig
26+
from sqlmesh.core.config import Config, BaseDuckDBConnectionConfig, DuckDBConnectionConfig
27+
from sqlmesh.core.config.connection import ConnectionConfig
2528
from sqlmesh.core.context import Context
2629
from sqlmesh.core.engine_adapter import MSSQLEngineAdapter, SparkEngineAdapter
2730
from sqlmesh.core.engine_adapter.base import EngineAdapter
@@ -526,3 +529,26 @@ def _make_function(table_name: str, random_id: str) -> exp.Table:
526529
return temp_table
527530

528531
return _make_function
532+
533+
534+
@pytest.fixture(scope="function", autouse=True)
535+
def set_default_connection(request):
536+
request = request.node.get_closest_marker("set_default_connection")
537+
disable = request and request.kwargs.get("disable")
538+
539+
if disable:
540+
ctx = nullcontext()
541+
else:
542+
original_get_connection = Config.get_connection
543+
544+
def _lax_get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
545+
try:
546+
connection = original_get_connection(self, gateway_name)
547+
except:
548+
connection = DuckDBConnectionConfig()
549+
return connection
550+
551+
ctx = mock.patch("sqlmesh.core.config.Config.get_connection", _lax_get_connection)
552+
553+
with ctx:
554+
yield

tests/core/test_integration.py

Lines changed: 20 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,22 @@ 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+
6134+
@pytest.mark.set_default_connection(disable=True)
6135+
def test_missing_connection_config():
6136+
# This is testing the actual implementation of Config.get_connection
6137+
# To make writing tests easier, it's patched by the autouse fixture provide_sqlmesh_default_connection
6138+
# Case 1: No default_connection or gateways specified should raise a ConfigError
6139+
with pytest.raises(ConfigError):
6140+
ctx = Context(config=Config())
6141+
6142+
# Case 2: No connection specified in the gateway should raise a ConfigError
6143+
with pytest.raises(ConfigError):
6144+
ctx = Context(config=Config(gateways={"incorrect": GatewayConfig()}))
6145+
6146+
# Case 3: Specifying a default_connection or connection in the gateway should work
6147+
ctx = Context(config=Config(default_connection=DuckDBConnectionConfig()))
6148+
ctx = Context(
6149+
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
6150+
)

0 commit comments

Comments
 (0)