Skip to content

Commit

Permalink
feat: added aio_delete_instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
kalombos committed May 28, 2024
1 parent 3089e5f commit c7707f3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
12 changes: 12 additions & 0 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,18 @@ def raw(cls, sql, *params):
def delete(cls):
return AioModelDelete(cls)

async def aio_delete_instance(self, recursive=False, delete_nullable=False):
if recursive:
dependencies = self.dependencies(delete_nullable)
for query, fk in reversed(list(dependencies)):
print(query, fk)
model = fk.model
if fk.null and not delete_nullable:
await model.update(**{fk.name: None}).where(query).aio_execute()
else:
await model.delete().where(query).aio_execute()
return await type(self).delete().where(self._pk_expr()).aio_execute()

@classmethod
async def aio_get(cls, *query, **filters):
"""Async version of **peewee.Model.get**"""
Expand Down
4 changes: 4 additions & 0 deletions peewee_async_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ async def update(self, obj, only=None):

async def delete(self, obj, recursive=False, delete_nullable=False):
"""Delete object from database."""
warnings.warn(
"`delete` method is deprecated, use `AioModel.aio_delete_instance` instead.",
DeprecationWarning
)
if recursive:
dependencies = obj.dependencies(delete_nullable)
for cond, fk in reversed(list(dependencies)):
Expand Down
27 changes: 26 additions & 1 deletion tests/aio_model/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import uuid

import pytest
from peewee import fn

from tests.conftest import dbs_all
from tests.models import TestModel, IntegerTestModel
from tests.models import TestModel, IntegerTestModel, TestModelAlpha, TestModelBeta


@dbs_all
Expand Down Expand Up @@ -61,3 +63,26 @@ async def test_count_query_clear_limit(db):
await IntegerTestModel.aio_create(num=num)
count = await IntegerTestModel.select().limit(3).aio_count(clear_limit=True)
assert count == 5


@dbs_all
async def test_aio_delete_instance(db):
text = "Test %s" % uuid.uuid4()
obj1 = await TestModel.aio_create(text=text)
obj2 = await TestModel.aio_get(id=obj1.id)

await obj2.aio_delete_instance()

obj3 = await TestModel.aio_get_or_none(id=obj1.id)
assert obj3 is None


@dbs_all
async def test_aio_delete_instance_with_fk(db):
alpha = await TestModelAlpha.aio_create(text="test")
beta = await TestModelBeta.aio_create(alpha=alpha, text="test")

await alpha.aio_delete_instance(recursive=True)

assert await TestModelAlpha.aio_get_or_none(id=alpha.id) is None
assert await TestModelBeta.aio_get_or_none(id=beta.id) is None
12 changes: 12 additions & 0 deletions tests/compat/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ async def test_create_obj(manager):
obj = await manager.create(CompatTestModel, text=text)
assert obj is not None
assert obj.text == text


@manager_for_all_dbs
async def test_delete_obj(manager):
text = "Test %s" % uuid.uuid4()
obj1 = await manager.create(CompatTestModel, text=text)
obj2 = await manager.get(CompatTestModel, id=obj1.id)

await manager.delete(obj2)

obj3 = await manager.get_or_none(CompatTestModel, id=obj1.id)
assert obj3 is None
12 changes: 0 additions & 12 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@ async def test_prefetch(manager, prefetch_type):
assert tuple(result[0].betas[0].gammas) == (gamma_111, gamma_112)


@manager_for_all_dbs
async def test_delete_obj(manager):
text = "Test %s" % uuid.uuid4()
obj1 = await manager.create(TestModel, text=text)
obj2 = await manager.get(TestModel, id=obj1.id)

await manager.delete(obj2)

obj3 = await manager.get_or_none(TestModel, id=obj1.id)
assert obj3 is None


@manager_for_all_dbs
async def test_update_obj(manager):

Expand Down

0 comments on commit c7707f3

Please sign in to comment.