chore(dao/command): Add transaction decorator to try to enforce "unit of work" (#24969)

This commit is contained in:
John Bodley
2024-06-28 12:33:56 -07:00
committed by GitHub
parent a3f0d00714
commit 8fb8199a55
151 changed files with 681 additions and 916 deletions

View File

@@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import logging
from functools import partial
from typing import Any, Optional
from flask_appbuilder.models.sqla import Model
@@ -28,23 +29,19 @@ from superset.commands.dashboard.exceptions import (
)
from superset.commands.utils import populate_roles
from superset.daos.dashboard import DashboardDAO
from superset.daos.exceptions import DAOCreateFailedError
from superset.utils.decorators import on_error, transaction
logger = logging.getLogger(__name__)
class CreateDashboardCommand(CreateMixin, BaseCommand):
def __init__(self, data: dict[str, Any]):
def __init__(self, data: dict[str, Any]) -> None:
self._properties = data.copy()
@transaction(on_error=partial(on_error, reraise=DashboardCreateFailedError))
def run(self) -> Model:
self.validate()
try:
dashboard = DashboardDAO.create(attributes=self._properties, commit=True)
except DAOCreateFailedError as ex:
logger.exception(ex.exception)
raise DashboardCreateFailedError() from ex
return dashboard
return DashboardDAO.create(attributes=self._properties)
def validate(self) -> None:
exceptions: list[ValidationError] = []