Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow certain exceptions to commit #30067

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions superset/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def on_error(

def transaction( # pylint: disable=redefined-outer-name
on_error: Callable[..., Any] | None = on_error,
allowed: tuple[type[Exception], ...] = (),
) -> Callable[..., Any]:
"""
Perform a "unit of work".
Expand All @@ -246,7 +247,12 @@ def transaction( # pylint: disable=redefined-outer-name
proved rather complicated, likely due to many architectural facets, and thus has
been left for a follow up exercise.

In certain cases it might be desirable to commit even though an exception was
raised. For OAuth2, for example, we use exceptions as a way to signal the client to
redirect to the login page. In this case, we re-raise the exception and commit.

:param on_error: Callback invoked when an exception is caught
:param allowed: Exception types to re-raise after committing
:see: https://github.com/apache/superset/issues/25108
"""

Expand All @@ -259,6 +265,9 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
result = func(*args, **kwargs)
db.session.commit() # pylint: disable=consider-using-transaction
return result
except allowed:
db.session.commit() # pylint: disable=consider-using-transaction
raise
except Exception as ex:
db.session.rollback() # pylint: disable=consider-using-transaction

Expand Down
72 changes: 72 additions & 0 deletions tests/unit_tests/utils/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from unittest.mock import call, Mock, patch

import pytest
from pytest_mock import MockerFixture

from superset import app
from superset.utils import decorators
Expand Down Expand Up @@ -294,3 +295,74 @@ def func() -> None:
decorated = decorators.suppress_logging("test-logger", logging.CRITICAL + 1)(func)
decorated()
assert len(handler.log_records) == 0


def test_transaction_no_error(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function works as expected.
"""
db = mocker.patch("superset.db")

@decorators.transaction()
def f() -> int:
return 42

assert f() == 42
db.session.commit.assert_called_once()
db.session.rollback.assert_not_called()


def test_transaction_with_on_error(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function captures an exception.
"""
db = mocker.patch("superset.db")

def on_error(ex: Exception) -> Exception:
return ex

ex = ValueError("error")

@decorators.transaction(on_error)
def f() -> None:
raise ex

assert f() == ex
db.session.commit.assert_not_called()
db.session.rollback.assert_called_once()


def test_transaction_without_on_error(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator when the function raises an exception.
"""
db = mocker.patch("superset.db")

@decorators.transaction()
def f() -> None:
raise ValueError("error")

with pytest.raises(ValueError) as excinfo:
f()
assert str(excinfo.value) == "error"
db.session.commit.assert_not_called()
db.session.rollback.assert_called_once()


def test_transaction_with_allowed(mocker: MockerFixture) -> None:
"""
Test the `transaction` decorator with allowed exceptions.

In this case the decorator will commit before re-raising the exception.
"""
db = mocker.patch("superset.db")

@decorators.transaction(allowed=(ValueError,))
def f() -> None:
raise ValueError("error")

with pytest.raises(ValueError) as excinfo:
f()
assert str(excinfo.value) == "error"
db.session.commit.assert_called_once()
db.session.rollback.assert_not_called()
Loading