Skip to content

Commit 93cd467

Browse files
committed
revert: restore db2.py and connection.py to 003593b state (37 passing baseline)
1 parent d4d492b commit 93cd467

2 files changed

Lines changed: 7 additions & 40 deletions

File tree

sqlmesh/core/config/connection.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,15 +2650,8 @@ def _engine_adapter(self) -> t.Type[EngineAdapter]:
26502650
)
26512651

26522652
def get_catalog(self) -> t.Optional[str]:
2653-
"""
2654-
Return the catalog (database) name uppercased. Db2 stores all unquoted
2655-
identifiers in uppercase and CURRENT SERVER returns an uppercase string.
2656-
get_current_catalog() also returns uppercase, so _default_catalog and the
2657-
live catalog value are always in the same case. The set_catalog() decorator's
2658-
REQUIRES_SET_CATALOG path compares catalog_name != get_current_catalog(); a
2659-
case mismatch (e.g. duckdb-dialect lowercase "testdb" vs "TESTDB") triggers
2660-
set_current_catalog() which is a no-op — so both cases are handled safely.
2661-
"""
2653+
"""Db2 stores catalog names in uppercase; normalise here so the default_catalog
2654+
passed to the adapter matches what get_current_catalog() returns at runtime."""
26622655
catalog = super().get_catalog()
26632656
return catalog.upper() if catalog else None
26642657

sqlmesh/core/engine_adapter/db2.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ class Db2EngineAdapter(
8585
def get_current_catalog(self) -> t.Optional[str]:
8686
"""
8787
Db2 requires FROM SYSIBM.SYSDUMMY1 to read the CURRENT SERVER special register.
88-
Returns the value uppercased to match the convention used by _default_catalog
89-
(which comes from get_catalog() → database name as supplied in config, uppercased).
90-
The set_catalog() decorator's REQUIRES_SET_CATALOG path compares
91-
catalog_name != get_current_catalog() — normalising both to uppercase ensures
92-
the comparison is consistent regardless of which dialect (db2 UPPERCASE vs
93-
duckdb LOWERCASE) produced the catalog token in the model expression.
88+
Returns uppercase to match the Db2 dialect's identifier normalisation.
9489
"""
9590
result = self.fetchone("SELECT CURRENT SERVER FROM SYSIBM.SYSDUMMY1")
9691
if result:
@@ -331,19 +326,7 @@ def _db2_type_to_sqlglot(self, db2_type: str, length: int, scale: int) -> exp.Da
331326

332327
@property
333328
def catalog_support(self) -> CatalogSupport:
334-
# REQUIRES_SET_CATALOG is used instead of SINGLE_CATALOG_ONLY because the
335-
# SINGLE_CATALOG_ONLY path in set_catalog() (shared.py:346) does a raw ==
336-
# comparison between catalog_name and _default_catalog. catalog_name comes
337-
# from the model expression, whose case depends on the dialect that built it:
338-
# db2 dialect → UPPERCASE, duckdb dialect → lowercase. Either case can appear
339-
# at runtime and we cannot control which, so a raw == would fail for one case.
340-
#
341-
# The REQUIRES_SET_CATALOG path instead calls get_current_catalog() for the
342-
# right-hand side. By returning uppercase from both get_catalog() (connection.py)
343-
# and get_current_catalog(), and by making set_current_catalog() a no-op (Db2 has
344-
# only one catalog — CONNECT TO cannot change it meaningfully), the mismatch is
345-
# tolerated: lowercase "testdb" != "TESTDB" → set_current_catalog (no-op) → proceed.
346-
return CatalogSupport.REQUIRES_SET_CATALOG
329+
return CatalogSupport.SINGLE_CATALOG_ONLY
347330

348331
def table_exists(self, table_name: TableName) -> bool:
349332
"""
@@ -851,18 +834,9 @@ def _df_to_source_queries(
851834
)
852835

853836
def set_current_catalog(self, catalog: str) -> None:
854-
"""
855-
No-op for Db2 — there is only one catalog (the database name) and CONNECT TO
856-
cannot switch to a different one in the middle of a session. The set_catalog()
857-
decorator calls this when catalog_name != get_current_catalog(), which happens
858-
because model expressions built through a duckdb-dialect context carry lowercase
859-
catalog names while get_current_catalog() returns uppercase. The mismatch is
860-
case-only and harmless, so we log it and return without executing any SQL.
861-
"""
862-
logger.debug(
863-
"set_current_catalog called with %r — no-op for Db2 (single-catalog engine)",
864-
catalog,
865-
)
837+
"""Switches the active catalog using Db2's CONNECT TO statement."""
838+
self.execute(f"CONNECT TO {catalog}")
839+
logger.debug("Switched to catalog: %s", catalog)
866840

867841
@cached_property
868842
def server_version(self) -> t.Tuple[int, int]:

0 commit comments

Comments
 (0)