Skip to content

Commit 4abf209

Browse files
committed
Restore temporary catalog after failed operations
Signed-off-by: lllleolin-max <244385774+lllleolin-max@users.noreply.github.com>
1 parent 271b855 commit 4abf209

3 files changed

Lines changed: 172 additions & 5 deletions

File tree

‎sqlmesh/core/engine_adapter/shared.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ def internal_wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
352352
current_catalog = engine_adapter.get_current_catalog()
353353
if catalog_name != current_catalog:
354354
engine_adapter.set_current_catalog(catalog_name)
355-
resp = func(*list_args, **kwargs)
356-
engine_adapter.set_current_catalog(current_catalog)
357-
else:
358-
resp = func(*list_args, **kwargs)
359-
return resp
355+
try:
356+
return func(*list_args, **kwargs)
357+
finally:
358+
engine_adapter.set_current_catalog(current_catalog)
359+
return func(*list_args, **kwargs)
360360

361361
return internal_wrapper
362362

‎tests/core/engine_adapter/test_duckdb.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ def test_set_current_catalog(make_mocked_engine_adapter: t.Callable, duck_conn):
7777
]
7878

7979

80+
@pytest.mark.parametrize("as_keyword", [True, False])
81+
def test_metadata_error_restores_catalog(
82+
adapter: EngineAdapter, duck_conn, mocker: MockerFixture, as_keyword: bool
83+
):
84+
duck_conn.execute("ATTACH ':memory:' AS secondary")
85+
original_catalog = adapter.get_current_catalog()
86+
schema = exp.table_("", db="main", catalog="secondary")
87+
original_schema = schema.copy()
88+
error = RuntimeError("metadata query failed")
89+
90+
mocker.patch.object(adapter, "fetchdf", side_effect=error)
91+
with pytest.raises(RuntimeError) as exc_info:
92+
if as_keyword:
93+
adapter._get_data_objects(schema_name=schema)
94+
else:
95+
adapter._get_data_objects(schema)
96+
97+
assert exc_info.value is error
98+
assert schema == original_schema
99+
assert adapter.get_current_catalog() == original_catalog
100+
duck_conn.execute("CREATE TABLE subsequent_table (id INT)")
101+
assert duck_conn.execute(
102+
"SELECT database_name FROM duckdb_tables() WHERE table_name = 'subsequent_table'"
103+
).fetchall() == [(original_catalog,)]
104+
105+
80106
def test_temporary_table(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture):
81107
adapter = make_mocked_engine_adapter(DuckDBEngineAdapter)
82108

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
from __future__ import annotations
3+
4+
import typing as t
5+
from unittest.mock import call
6+
7+
import pytest
8+
from sqlglot import exp
9+
10+
from sqlmesh.core.engine_adapter import EngineAdapter
11+
from sqlmesh.core.engine_adapter.shared import CatalogSupport, set_catalog
12+
13+
if t.TYPE_CHECKING:
14+
from sqlmesh.core._typing import SchemaName, TableName
15+
16+
pytestmark = pytest.mark.engine
17+
18+
19+
@set_catalog()
20+
class CatalogAdapter(EngineAdapter):
21+
@property
22+
def catalog_support(self) -> CatalogSupport:
23+
return CatalogSupport.REQUIRES_SET_CATALOG
24+
25+
def table_operation(self, table_name: TableName, callback: t.Callable) -> t.Any:
26+
return callback(table_name)
27+
28+
def schema_operation(self, schema_name: SchemaName, callback: t.Callable) -> t.Any:
29+
return callback(schema_name)
30+
31+
32+
@pytest.mark.parametrize("is_schema", [True, False])
33+
@pytest.mark.parametrize("as_keyword", [True, False])
34+
@pytest.mark.parametrize("fails", [True, False])
35+
def test_set_catalog_operation(make_mocked_engine_adapter, mocker, is_schema, as_keyword, fails):
36+
adapter = make_mocked_engine_adapter(CatalogAdapter)
37+
mocker.patch.object(adapter, "get_current_catalog", return_value="original")
38+
switch = mocker.patch.object(adapter, "set_current_catalog")
39+
target = (
40+
exp.table_("", db="schema", catalog="temporary")
41+
if is_schema
42+
else exp.to_table("temporary.schema.table")
43+
)
44+
original_target = target.copy()
45+
result = object()
46+
error = RuntimeError("operation failed")
47+
callback = mocker.Mock(side_effect=error if fails else None, return_value=result)
48+
operation = adapter.schema_operation if is_schema else adapter.table_operation
49+
arguments = {"schema_name" if is_schema else "table_name": target, "callback": callback}
50+
51+
def run():
52+
return operation(**arguments) if as_keyword else operation(target, callback)
53+
54+
if fails:
55+
with pytest.raises(RuntimeError) as exc_info:
56+
run()
57+
assert exc_info.value is error
58+
else:
59+
assert run() is result
60+
61+
assert target == original_target
62+
stripped_target = original_target.copy()
63+
stripped_target.set("catalog", None)
64+
callback.assert_called_once_with(stripped_target)
65+
assert switch.call_args_list == [call("temporary"), call("original")]
66+
67+
68+
@pytest.mark.parametrize("target", ["original.schema.table", "schema.table"])
69+
def test_set_catalog_skips_unneeded_switch(make_mocked_engine_adapter, mocker, target):
70+
adapter = make_mocked_engine_adapter(CatalogAdapter)
71+
get_catalog = mocker.patch.object(adapter, "get_current_catalog", return_value="original")
72+
switch = mocker.patch.object(adapter, "set_current_catalog")
73+
error = RuntimeError("operation failed")
74+
callback = mocker.Mock(side_effect=error)
75+
76+
with pytest.raises(RuntimeError) as exc_info:
77+
adapter.table_operation(target, callback)
78+
79+
assert exc_info.value is error
80+
switch.assert_not_called()
81+
assert get_catalog.call_count == (1 if target.startswith("original.") else 0)
82+
83+
84+
@pytest.mark.parametrize("fails", [True, False])
85+
def test_set_catalog_nested_operations(make_mocked_engine_adapter, mocker, fails):
86+
adapter = make_mocked_engine_adapter(CatalogAdapter)
87+
current_catalog = "original"
88+
89+
def set_current_catalog(catalog):
90+
nonlocal current_catalog
91+
current_catalog = catalog
92+
93+
mocker.patch.object(adapter, "get_current_catalog", side_effect=lambda: current_catalog)
94+
switch = mocker.patch.object(adapter, "set_current_catalog", side_effect=set_current_catalog)
95+
result = object()
96+
error = RuntimeError("inner operation failed")
97+
98+
def inner(table):
99+
assert current_catalog == "inner"
100+
if fails:
101+
raise error
102+
return result
103+
104+
def outer(table):
105+
assert current_catalog == "outer"
106+
try:
107+
return adapter.table_operation("inner.schema.table", inner)
108+
finally:
109+
assert current_catalog == "outer"
110+
111+
if fails:
112+
with pytest.raises(RuntimeError) as exc_info:
113+
adapter.table_operation("outer.schema.table", outer)
114+
assert exc_info.value is error
115+
else:
116+
assert adapter.table_operation("outer.schema.table", outer) is result
117+
118+
assert current_catalog == "original"
119+
assert switch.call_args_list == [
120+
call("outer"),
121+
call("inner"),
122+
call("outer"),
123+
call("original"),
124+
]
125+
126+
127+
@pytest.mark.parametrize("operation_fails", [True, False])
128+
def test_set_catalog_restore_failure(make_mocked_engine_adapter, mocker, operation_fails):
129+
adapter = make_mocked_engine_adapter(CatalogAdapter)
130+
mocker.patch.object(adapter, "get_current_catalog", return_value="original")
131+
operation_error = RuntimeError("operation failed")
132+
restore_error = RuntimeError("restore failed")
133+
switch = mocker.patch.object(adapter, "set_current_catalog", side_effect=[None, restore_error])
134+
callback = mocker.Mock(side_effect=operation_error if operation_fails else None)
135+
136+
with pytest.raises(RuntimeError) as exc_info:
137+
adapter.table_operation("temporary.schema.table", callback)
138+
139+
assert exc_info.value is restore_error
140+
assert exc_info.value.__context__ is (operation_error if operation_fails else None)
141+
assert switch.call_args_list == [call("temporary"), call("original")]

0 commit comments

Comments
 (0)