From 2b16065914bf96e9f60e94d0aab6aeee985c59cc Mon Sep 17 00:00:00 2001 From: Jesus Lara Date: Thu, 19 Oct 2023 02:26:58 +0200 Subject: [PATCH] changes --- asyncdb/drivers/pg.py | 12 ++++++++++-- asyncdb/models/model.py | 3 ++- asyncdb/version.py | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/asyncdb/drivers/pg.py b/asyncdb/drivers/pg.py index b32541da..39d00ea7 100644 --- a/asyncdb/drivers/pg.py +++ b/asyncdb/drivers/pg.py @@ -1332,7 +1332,7 @@ async def _insert_(self, _model: Model, **kwargs): # pylint: disable=W0613 message=f"Error on Insert over table {_model.Meta.name}: {err!s}" ) from err - async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613 + async def _delete_(self, _model: Model, _filter: dict = None, **kwargs): # pylint: disable=W0613 """ delete a row from model. """ @@ -1345,7 +1345,8 @@ async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613 except AttributeError: table = _model.__name__ source = [] - _filter = {} + if not _filter: + _filter = {} n = 1 fields = _model.columns() for _, field in fields.items(): @@ -1361,9 +1362,16 @@ async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613 ) n += 1 if pk := self._get_attribute(field, value, attr='primary_key'): + if column in _filter: + # already this value on delete: + continue _filter[column] = pk try: condition = self._where(fields, **_filter) + if not condition: + raise DriverError( + f"Avoid DELETE without WHERE conditions: {_filter}" + ) _delete = f"DELETE FROM {table} {condition};" self._logger.debug(f'DELETE: {_delete}') result = await self._connection.execute(_delete) diff --git a/asyncdb/models/model.py b/asyncdb/models/model.py index 747c4ea4..4991798c 100644 --- a/asyncdb/models/model.py +++ b/asyncdb/models/model.py @@ -157,7 +157,7 @@ async def update(self, **kwargs): f"Error on UPDATE {self.Meta.name}: {err}" ) from err - async def delete(self, **kwargs): + async def delete(self, _filter: dict = None, **kwargs): """ Deleting a row Model based on Primary Key """ @@ -169,6 +169,7 @@ async def delete(self, **kwargs): try: result = await self.Meta.connection._delete_( _model=self, + _filter=_filter, **kwargs ) return result diff --git a/asyncdb/version.py b/asyncdb/version.py index 20a7d7a2..cec92062 100644 --- a/asyncdb/version.py +++ b/asyncdb/version.py @@ -3,7 +3,7 @@ __title__ = 'asyncdb' __description__ = ('Library for Asynchronous data source connections ' 'Collection of asyncio drivers.') -__version__ = '2.5.2' +__version__ = '2.5.3' __author__ = 'Jesus Lara' __author_email__ = 'jesuslarag@gmail.com' __license__ = 'BSD'