Skip to content

Chore!: Make default_connection optional #4522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sqlmesh/core/config/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Config(BaseConfig):
"""

gateways: GatewayDict = {"": GatewayConfig()}
default_connection: SerializableConnectionConfig = DuckDBConnectionConfig()
default_connection: t.Optional[SerializableConnectionConfig] = None
default_test_connection_: t.Optional[SerializableConnectionConfig] = Field(
default=None, alias="default_test_connection"
)
Expand Down Expand Up @@ -280,7 +280,11 @@ def get_gateway(self, name: t.Optional[str] = None) -> GatewayConfig:
return self.gateways

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

def get_state_connection(
self, gateway_name: t.Optional[str] = None
Expand Down
28 changes: 27 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations


import datetime
import logging
import typing as t
import uuid
from contextlib import nullcontext
from pathlib import Path
from shutil import copytree, rmtree
from tempfile import TemporaryDirectory
Expand All @@ -21,7 +23,8 @@
from sqlglot.helper import ensure_list
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers

from sqlmesh.core.config import BaseDuckDBConnectionConfig
from sqlmesh.core.config import Config, BaseDuckDBConnectionConfig, DuckDBConnectionConfig
from sqlmesh.core.config.connection import ConnectionConfig
from sqlmesh.core.context import Context
from sqlmesh.core.engine_adapter import MSSQLEngineAdapter, SparkEngineAdapter
from sqlmesh.core.engine_adapter.base import EngineAdapter
Expand Down Expand Up @@ -526,3 +529,26 @@ def _make_function(table_name: str, random_id: str) -> exp.Table:
return temp_table

return _make_function


@pytest.fixture(scope="function", autouse=True)
def set_default_connection(request):
request = request.node.get_closest_marker("set_default_connection")
disable = request and request.kwargs.get("disable")

if disable:
ctx = nullcontext()
else:
original_get_connection = Config.get_connection

def _lax_get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
try:
connection = original_get_connection(self, gateway_name)
except:
connection = DuckDBConnectionConfig()
return connection

ctx = mock.patch("sqlmesh.core.config.Config.get_connection", _lax_get_connection)

with ctx:
yield
21 changes: 20 additions & 1 deletion tests/core/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
SnapshotTableInfo,
)
from sqlmesh.utils.date import TimeLike, now, to_date, to_datetime, to_timestamp
from sqlmesh.utils.errors import NoChangesPlanError, SQLMeshError, PlanError
from sqlmesh.utils.errors import NoChangesPlanError, SQLMeshError, PlanError, ConfigError
from sqlmesh.utils.pydantic import validate_string
from tests.conftest import DuckDBMetadata, SushiDataValidator
from tests.utils.test_helpers import use_terminal_console
Expand Down Expand Up @@ -6129,3 +6129,22 @@ def setup_senario(model_before: str, model_after: str):
'Binder Error: Referenced column "this_col_does_not_exist" not found in \nFROM clause!'
in output.stdout
)


@pytest.mark.set_default_connection(disable=True)
def test_missing_connection_config():
# This is testing the actual implementation of Config.get_connection
# To make writing tests easier, it's patched by the autouse fixture provide_sqlmesh_default_connection
# Case 1: No default_connection or gateways specified should raise a ConfigError
with pytest.raises(ConfigError):
ctx = Context(config=Config())

# Case 2: No connection specified in the gateway should raise a ConfigError
with pytest.raises(ConfigError):
ctx = Context(config=Config(gateways={"incorrect": GatewayConfig()}))

# Case 3: Specifying a default_connection or connection in the gateway should work
ctx = Context(config=Config(default_connection=DuckDBConnectionConfig()))
ctx = Context(
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
)