Skip to content

Commit 4ee6821

Browse files
eakmanrqizeigerman
andauthored
fix: context close on Create/Update/Delete operations (#2038)
* fix: context close on crud operations * Use the existing error handler decorator to close the context * Remaining cleanup --------- Co-authored-by: Iaroslav Zeigerman <zeigerman.ia@gmail.com>
1 parent 27ba320 commit 4ee6821

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

sqlmesh/cli/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import click
66
from sqlglot.errors import SqlglotError
77

8+
from sqlmesh.core.context import Context
89
from sqlmesh.utils import debug_mode_enabled
910
from sqlmesh.utils.concurrency import NodeExecutionFailedError
1011
from sqlmesh.utils.errors import SQLMeshError
@@ -19,15 +20,21 @@ def error_handler(
1920
func: t.Callable[..., DECORATOR_RETURN_TYPE]
2021
) -> t.Callable[..., DECORATOR_RETURN_TYPE]:
2122
@wraps(func)
22-
def wrapper(*args: t.Any, **kwargs: t.Any) -> DECORATOR_RETURN_TYPE:
23+
def wrapper(*args: t.List[t.Any], **kwargs: t.Any) -> DECORATOR_RETURN_TYPE:
24+
context_or_obj = args[0]
25+
sqlmesh_context = (
26+
context_or_obj.obj if isinstance(context_or_obj, click.Context) else context_or_obj
27+
)
28+
if not isinstance(sqlmesh_context, Context):
29+
sqlmesh_context = None
2330
handler = _debug_exception_handler if debug_mode_enabled() else _default_exception_handler
24-
return handler(lambda: func(*args, **kwargs))
31+
return handler(sqlmesh_context, lambda: func(*args, **kwargs))
2532

2633
return wrapper
2734

2835

2936
def _default_exception_handler(
30-
func: t.Callable[[], DECORATOR_RETURN_TYPE]
37+
context: t.Optional[Context], func: t.Callable[[], DECORATOR_RETURN_TYPE]
3138
) -> DECORATOR_RETURN_TYPE:
3239
try:
3340
return func()
@@ -36,11 +43,19 @@ def _default_exception_handler(
3643
raise click.ClickException(f"Failed processing {ex.node}. {cause}")
3744
except (SQLMeshError, SqlglotError, ValueError) as ex:
3845
raise click.ClickException(str(ex))
46+
finally:
47+
if context:
48+
context.close()
3949

4050

41-
def _debug_exception_handler(func: t.Callable[[], DECORATOR_RETURN_TYPE]) -> DECORATOR_RETURN_TYPE:
51+
def _debug_exception_handler(
52+
context: t.Optional[Context], func: t.Callable[[], DECORATOR_RETURN_TYPE]
53+
) -> DECORATOR_RETURN_TYPE:
4254
try:
4355
return func()
4456
except Exception:
4557
logger.exception("Unhandled exception")
4658
raise
59+
finally:
60+
if context:
61+
context.close()

0 commit comments

Comments
 (0)