Skip to content

Commit a782332

Browse files
committed
explicit_default_catalog
Signed-off-by: Andreas Fredhøi <andreas.fredhoi@fresio.no>
1 parent dd8739e commit a782332

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

sqlmesh/core/engine_adapter/fabric.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,15 @@ def set_current_catalog(self, catalog_name: t.Optional[str]) -> None:
170170
https://learn.microsoft.com/en-us/fabric/data-warehouse/sql-query-editor#limitations
171171
"""
172172
target_catalog = self._normalize_catalog(catalog_name)
173+
explicit_default_catalog = catalog_name is not None and target_catalog is None
174+
connected_catalog = self._normalize_catalog(self._connected_catalog)
173175

174-
# No-op: the logical catalog state already matches.
175-
if self.get_current_catalog() == target_catalog:
176+
# An explicit request for the default catalog must also match the catalog
177+
# used by the open connection. A lazy restore with None only updates the
178+
# logical target and intentionally leaves that connection in place.
179+
if self.get_current_catalog() == target_catalog and (
180+
not explicit_default_catalog or connected_catalog is None
181+
):
176182
logger.debug("Already using requested Fabric catalog state, no action needed")
177183
return
178184

@@ -192,8 +198,9 @@ def set_current_catalog(self, catalog_name: t.Optional[str]) -> None:
192198
# open connection is already on a different catalog. If a previous
193199
# restore-to-neutral left the connection on the right catalog, we
194200
# skip the close entirely.
195-
connected_catalog = self._normalize_catalog(self._connected_catalog)
196-
needs_reconnect = target_catalog is not None and connected_catalog != target_catalog
201+
needs_reconnect = (target_catalog is not None or explicit_default_catalog) and (
202+
connected_catalog != target_catalog
203+
)
197204

198205
if needs_reconnect:
199206
logger.info(

tests/core/engine_adapter/test_fabric.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ def test_catalog_scoped_call_restores_to_neutral_without_close(
101101
assert adapter.get_current_catalog() is None
102102

103103

104+
def test_default_catalog_after_non_default_catalog_reconnects(
105+
make_mocked_engine_adapter: t.Callable,
106+
mocker: MockerFixture,
107+
):
108+
adapter = make_mocked_engine_adapter(
109+
FabricEngineAdapter,
110+
default_catalog="core",
111+
database="core",
112+
)
113+
close_spy = mocker.spy(adapter._connection_pool, "close")
114+
adapter.cursor.fetchone.return_value = (1,)
115+
116+
adapter.table_exists("planning.db.table")
117+
adapter.table_exists("core.db.table")
118+
119+
assert close_spy.call_count == 2
120+
assert adapter._connected_catalog is None
121+
assert adapter.get_current_catalog() is None
122+
123+
104124
def test_repeated_same_catalog_reuses_connection(
105125
make_mocked_engine_adapter: t.Callable,
106126
mocker: MockerFixture,

0 commit comments

Comments
 (0)