Skip to content

Commit a3ee0db

Browse files
authored
feat: add wait argument to Model.delete_table (#1270)
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 65e4b5f commit a3ee0db

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

docs/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ v6.1.0
88

99
Features:
1010

11+
* Add a `wait` argument to `Model.delete_table` (:pr:`1270`)
1112
* Add the ability to set or unset the boto retry configuration (:pr:`1271`)
1213

1314
* This adds the ability to directly set the boto retry configuration dictionary, or

pynamodb/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,18 @@ 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+
771+
while wait and cls.exists():
772+
time.sleep(2)
773+
774+
return result
768775

769776
@classmethod
770777
def describe_table(cls) -> Any:

tests/test_model.py

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

0 commit comments

Comments
 (0)