Skip to content

Commit

Permalink
chore: Allow only iterables for BaseDAO.delete() (#25844)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley authored Nov 22, 2023
1 parent 260d561 commit 843c7ab
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 14 deletions.
11 changes: 5 additions & 6 deletions superset/daos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.
from __future__ import annotations

from typing import Any, cast, Generic, get_args, TypeVar
from typing import Any, Generic, get_args, TypeVar

from flask_appbuilder.models.filters import BaseFilter
from flask_appbuilder.models.sqla import Model
Expand All @@ -30,7 +30,6 @@
DAOUpdateFailedError,
)
from superset.extensions import db
from superset.utils.core import as_list

T = TypeVar("T", bound=Model)

Expand Down Expand Up @@ -197,9 +196,9 @@ def update(
return item # type: ignore

@classmethod
def delete(cls, item_or_items: T | list[T], commit: bool = True) -> None:
def delete(cls, items: list[T], commit: bool = True) -> None:
"""
Delete the specified item(s) including their associated relationships.
Delete the specified items including their associated relationships.
Note that bulk deletion via `delete` is not invoked in the base class as this
does not dispatch the ORM `after_delete` event which may be required to augment
Expand All @@ -209,12 +208,12 @@ def delete(cls, item_or_items: T | list[T], commit: bool = True) -> None:
Subclasses may invoke bulk deletion but are responsible for instrumenting any
post-deletion logic.
:param items: The item(s) to delete
:param items: The items to delete
:param commit: Whether to commit the transaction
:raises DAODeleteFailedError: If the deletion failed
:see: https://docs.sqlalchemy.org/en/latest/orm/queryguide/dml.html
"""
items = cast(list[T], as_list(item_or_items))

try:
for item in items:
db.session.delete(item)
Expand Down
3 changes: 1 addition & 2 deletions superset/dashboards/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,8 +1349,7 @@ def delete_embedded(self, dashboard: Dashboard) -> Response:
500:
$ref: '#/components/responses/500'
"""
for embedded in dashboard.embedded:
EmbeddedDashboardDAO.delete(embedded)
EmbeddedDashboardDAO.delete(dashboard.embedded)
return self.response(200, message="OK")

@expose("/<id_or_slug>/copy/", methods=("POST",))
Expand Down
2 changes: 1 addition & 1 deletion superset/dashboards/filter_sets/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run(self) -> None:
assert self._filter_set

try:
FilterSetDAO.delete(self._filter_set)
FilterSetDAO.delete([self._filter_set])
except DAODeleteFailedError as err:
raise FilterSetDeleteFailedError(str(self._filter_set_id), "") from err

Expand Down
2 changes: 1 addition & 1 deletion superset/databases/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run(self) -> None:
assert self._model

try:
DatabaseDAO.delete(self._model)
DatabaseDAO.delete([self._model])
except DAODeleteFailedError as ex:
logger.exception(ex.exception)
raise DatabaseDeleteFailedError() from ex
Expand Down
2 changes: 1 addition & 1 deletion superset/databases/ssh_tunnel/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self) -> None:
assert self._model

try:
SSHTunnelDAO.delete(self._model)
SSHTunnelDAO.delete([self._model])
except DAODeleteFailedError as ex:
raise SSHTunnelDeleteFailedError() from ex

Expand Down
2 changes: 1 addition & 1 deletion superset/datasets/columns/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self) -> None:
assert self._model

try:
DatasetColumnDAO.delete(self._model)
DatasetColumnDAO.delete([self._model])
except DAODeleteFailedError as ex:
logger.exception(ex.exception)
raise DatasetColumnDeleteFailedError() from ex
Expand Down
2 changes: 1 addition & 1 deletion superset/datasets/metrics/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self) -> None:
assert self._model

try:
DatasetMetricDAO.delete(self._model)
DatasetMetricDAO.delete([self._model])
except DAODeleteFailedError as ex:
logger.exception(ex.exception)
raise DatasetMetricDeleteFailedError() from ex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ def test_get_dashboards_api_no_data_access(self):
self.assert200(rv)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(0, data["count"])
DashboardDAO.delete(dashboard)
DashboardDAO.delete([dashboard])

0 comments on commit 843c7ab

Please sign in to comment.