Skip to content

Commit ba5bb5b

Browse files
committed
feat: add wait argument to Model.delete_table
This adds a `wait` argument that will make the `delete_table` call block until the DynamoDB table is fully deleted if set to `True`. It defaults to `False` to not break the existing behavior.
1 parent f0bc917 commit ba5bb5b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

pynamodb/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,21 @@ def exists(cls: Type[_T]) -> bool:
760760
return False
761761

762762
@classmethod
763-
def delete_table(cls) -> Any:
763+
def delete_table(cls, wait: bool = False) -> Any:
764764
"""
765765
Delete the table for this model
766+
767+
:param wait: If set, then this call will block until the table is deleted
766768
"""
767-
return cls._get_connection().delete_table()
769+
result = cls._get_connection().delete_table()
770+
if not wait:
771+
return result
772+
773+
while True:
774+
if not cls.exists():
775+
return result
776+
777+
time.sleep(2)
768778

769779
@classmethod
770780
def describe_table(cls) -> Any:

tests/test_model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,3 +3438,18 @@ def test_delete(add_version_condition: bool) -> None:
34383438
}
34393439
args = req.call_args[0][1]
34403440
assert args == expected
3441+
3442+
@patch.object(target=Model, attribute="exists", autospec=True)
3443+
@patch.object(target=Model, attribute="_get_connection", autospec=True)
3444+
def test_delete_with_wait(mock__get_connection, mock_exists):
3445+
"""Test that the wait argument works as expected on the delete."""
3446+
# Make the first two exists calls show the table as still existing, then have the
3447+
# last call show it has been deleted.
3448+
mock_exists.side_effect = (True, True, False)
3449+
3450+
result = Model.delete_table(wait=True)
3451+
3452+
# Should have returned the delete value.
3453+
assert result == mock__get_connection.return_value.delete_table.return_value
3454+
# Should have called exists 3 times.
3455+
assert mock_exists.call_count == 3

0 commit comments

Comments
 (0)