mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
chore(dao): Replace save/overwrite with create/update respectively (#24467)
This commit is contained in:
@@ -25,7 +25,6 @@ from sqlalchemy.exc import SQLAlchemyError, StatementError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from superset.daos.exceptions import (
|
||||
DAOConfigError,
|
||||
DAOCreateFailedError,
|
||||
DAODeleteFailedError,
|
||||
DAOUpdateFailedError,
|
||||
@@ -130,57 +129,72 @@ class BaseDAO(Generic[T]):
|
||||
return query.filter_by(**filter_by).one_or_none()
|
||||
|
||||
@classmethod
|
||||
def create(cls, properties: dict[str, Any], commit: bool = True) -> T:
|
||||
def create(
|
||||
cls,
|
||||
item: T | None = None,
|
||||
attributes: dict[str, Any] | None = None,
|
||||
commit: bool = True,
|
||||
) -> T:
|
||||
"""
|
||||
Generic for creating models
|
||||
:raises: DAOCreateFailedError
|
||||
"""
|
||||
if cls.model_cls is None:
|
||||
raise DAOConfigError()
|
||||
model = cls.model_cls() # pylint: disable=not-callable
|
||||
for key, value in properties.items():
|
||||
setattr(model, key, value)
|
||||
try:
|
||||
db.session.add(model)
|
||||
if commit:
|
||||
db.session.commit()
|
||||
except SQLAlchemyError as ex: # pragma: no cover
|
||||
db.session.rollback()
|
||||
raise DAOCreateFailedError(exception=ex) from ex
|
||||
return model
|
||||
Create an object from the specified item and/or attributes.
|
||||
|
||||
@classmethod
|
||||
def save(cls, instance_model: T, commit: bool = True) -> None:
|
||||
:param item: The object to create
|
||||
:param attributes: The attributes associated with the object to create
|
||||
:param commit: Whether to commit the transaction
|
||||
:raises DAOCreateFailedError: If the creation failed
|
||||
"""
|
||||
Generic for saving models
|
||||
:raises: DAOCreateFailedError
|
||||
"""
|
||||
if cls.model_cls is None:
|
||||
raise DAOConfigError()
|
||||
|
||||
if not item:
|
||||
item = cls.model_cls() # type: ignore # pylint: disable=not-callable
|
||||
|
||||
if attributes:
|
||||
for key, value in attributes.items():
|
||||
setattr(item, key, value)
|
||||
|
||||
try:
|
||||
db.session.add(instance_model)
|
||||
db.session.add(item)
|
||||
|
||||
if commit:
|
||||
db.session.commit()
|
||||
except SQLAlchemyError as ex: # pragma: no cover
|
||||
db.session.rollback()
|
||||
raise DAOCreateFailedError(exception=ex) from ex
|
||||
|
||||
return item # type: ignore
|
||||
|
||||
@classmethod
|
||||
def update(cls, model: T, properties: dict[str, Any], commit: bool = True) -> T:
|
||||
def update(
|
||||
cls,
|
||||
item: T | None = None,
|
||||
attributes: dict[str, Any] | None = None,
|
||||
commit: bool = True,
|
||||
) -> T:
|
||||
"""
|
||||
Generic update a model
|
||||
:raises: DAOCreateFailedError
|
||||
Update an object from the specified item and/or attributes.
|
||||
|
||||
:param item: The object to update
|
||||
:param attributes: The attributes associated with the object to update
|
||||
:param commit: Whether to commit the transaction
|
||||
:raises DAOUpdateFailedError: If the updating failed
|
||||
"""
|
||||
for key, value in properties.items():
|
||||
setattr(model, key, value)
|
||||
|
||||
if not item:
|
||||
item = cls.model_cls() # type: ignore # pylint: disable=not-callable
|
||||
|
||||
if attributes:
|
||||
for key, value in attributes.items():
|
||||
setattr(item, key, value)
|
||||
|
||||
try:
|
||||
db.session.merge(model)
|
||||
db.session.merge(item)
|
||||
|
||||
if commit:
|
||||
db.session.commit()
|
||||
except SQLAlchemyError as ex: # pragma: no cover
|
||||
db.session.rollback()
|
||||
raise DAOUpdateFailedError(exception=ex) from ex
|
||||
return model
|
||||
|
||||
return item # type: ignore
|
||||
|
||||
@classmethod
|
||||
def delete(cls, items: T | list[T], commit: bool = True) -> None:
|
||||
|
||||
Reference in New Issue
Block a user