Skip to content

Commit cba6fee

Browse files
committed
explicit_default_catalog
1 parent de6583e commit cba6fee

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
@@ -164,9 +164,15 @@ def set_current_catalog(self, catalog_name: t.Optional[str]) -> None:
164164
https://learn.microsoft.com/en-us/fabric/data-warehouse/sql-query-editor#limitations
165165
"""
166166
target_catalog = self._normalize_catalog(catalog_name)
167+
explicit_default_catalog = catalog_name is not None and target_catalog is None
168+
connected_catalog = self._normalize_catalog(self._connected_catalog)
167169

168-
# No-op: the logical catalog state already matches.
169-
if self.get_current_catalog() == target_catalog:
170+
# An explicit request for the default catalog must also match the catalog
171+
# used by the open connection. A lazy restore with None only updates the
172+
# logical target and intentionally leaves that connection in place.
173+
if self.get_current_catalog() == target_catalog and (
174+
not explicit_default_catalog or connected_catalog is None
175+
):
170176
logger.debug("Already using requested Fabric catalog state, no action needed")
171177
return
172178

@@ -186,8 +192,9 @@ def set_current_catalog(self, catalog_name: t.Optional[str]) -> None:
186192
# open connection is already on a different catalog. If a previous
187193
# restore-to-neutral left the connection on the right catalog, we
188194
# skip the close entirely.
189-
connected_catalog = self._normalize_catalog(self._connected_catalog)
190-
needs_reconnect = target_catalog is not None and connected_catalog != target_catalog
195+
needs_reconnect = (target_catalog is not None or explicit_default_catalog) and (
196+
connected_catalog != target_catalog
197+
)
191198

192199
if needs_reconnect:
193200
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)