Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ v6.1.0

Features:

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

* This adds the ability to directly set the boto retry configuration dictionary, or
Expand Down
11 changes: 9 additions & 2 deletions pynamodb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,18 @@ def exists(cls: Type[_T]) -> bool:
return False

@classmethod
def delete_table(cls) -> Any:
def delete_table(cls, *, wait: bool = False) -> Any:
"""
Delete the table for this model

:param wait: If set, then this call will block until the table is deleted
"""
return cls._get_connection().delete_table()
result = cls._get_connection().delete_table()

while wait and cls.exists():
time.sleep(2)

return result

@classmethod
def describe_table(cls) -> Any:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3438,3 +3438,21 @@ def test_delete(add_version_condition: bool) -> None:
}
args = req.call_args[0][1]
assert args == expected

def test_delete_with_wait(mocker):
"""Test that the wait argument works as expected on the delete."""
mock_exists = mocker.patch.object(target=Model, attribute="exists", autospec=True)
mock__get_connection = mocker.patch.object(
target=Model, attribute="_get_connection", autospec=True
)

# Make the first two exists calls show the table as still existing, then have the
# last call show it has been deleted.
mock_exists.side_effect = (True, True, False)

result = Model.delete_table(wait=True)

# Should have returned the delete value.
assert result == mock__get_connection.return_value.delete_table.return_value
# Should have called exists 3 times.
assert mock_exists.call_count == 3