Skip to content

Commit

Permalink
Python: add FLUSHDB command (#1680)
Browse files Browse the repository at this point in the history
* Python: added FLUSHDB command (#393)

* Updated CHANGELOG.md

* Addressed review comments

* Addressed review comments
  • Loading branch information
yipin-chen authored Jun 27, 2024
1 parent 27705a4 commit 2d499dc
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* Python: Added LOLWUT command ([#1657](https://github.com/aws/glide-for-redis/pull/1657))
* Python: Added XREADGROUP command ([#1679](https://github.com/aws/glide-for-redis/pull/1679))
* Python: Added XACK command ([#1681](https://github.com/aws/glide-for-redis/pull/1681))
* Python: Added FLUSHDB command ([#1680](https://github.com/aws/glide-for-redis/pull/1680))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))
Expand Down
33 changes: 33 additions & 0 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,39 @@ async def flushall(
await self._execute_command(RequestType.FlushAll, args, route),
)

async def flushdb(
self, flush_mode: Optional[FlushMode] = None, route: Optional[Route] = None
) -> TClusterResponse[TOK]:
"""
Deletes all the keys of the currently selected database. This command never fails.
See https://valkey.io/commands/flushdb for more details.
Args:
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
in which case the client will route the command to the nodes defined by `route`.
Returns:
TOK: OK.
Examples:
>>> await client.flushdb()
OK # The keys of the currently selected database were deleted.
>>> await client.flushdb(FlushMode.ASYNC)
OK # The keys of the currently selected database were deleted asynchronously.
>>> await client.flushdb(FlushMode.ASYNC, AllNodes())
OK # The keys of the currently selected database were deleted asynchronously on all nodes.
"""
args = []
if flush_mode is not None:
args.append(flush_mode.value)

return cast(
TClusterResponse[TOK],
await self._execute_command(RequestType.FlushDB, args, route),
)

async def copy(
self,
source: str,
Expand Down
27 changes: 27 additions & 0 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,33 @@ async def flushall(self, flush_mode: Optional[FlushMode] = None) -> TOK:
await self._execute_command(RequestType.FlushAll, args),
)

async def flushdb(self, flush_mode: Optional[FlushMode] = None) -> TOK:
"""
Deletes all the keys of the currently selected database. This command never fails.
See https://valkey.io/commands/flushdb for more details.
Args:
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
Returns:
TOK: OK.
Examples:
>>> await client.flushdb()
OK # The keys of the currently selected database were deleted.
>>> await client.flushdb(FlushMode.ASYNC)
OK # The keys of the currently selected database were deleted asynchronously.
"""
args = []
if flush_mode is not None:
args.append(flush_mode.value)

return cast(
TOK,
await self._execute_command(RequestType.FlushDB, args),
)

async def copy(
self,
source: str,
Expand Down
19 changes: 19 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3667,6 +3667,25 @@ def flushall(
args.append(flush_mode.value)
return self.append_command(RequestType.FlushAll, args)

def flushdb(
self: TTransaction, flush_mode: Optional[FlushMode] = None
) -> TTransaction:
"""
Deletes all the keys of the currently selected database. This command never fails.
See https://valkey.io/commands/flushdb for more details.
Args:
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
Command Response:
TOK: OK.
"""
args = []
if flush_mode is not None:
args.append(flush_mode.value)
return self.append_command(RequestType.FlushDB, args)

def getex(
self: TTransaction, key: str, expiry: Optional[ExpiryGetEx] = None
) -> TTransaction:
Expand Down
58 changes: 58 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6283,6 +6283,41 @@ async def test_flushall(self, redis_client: TRedisClient):
assert await redis_client.flushall(FlushMode.SYNC, AllPrimaries()) is OK
assert await redis_client.dbsize() == 0

@pytest.mark.parametrize("cluster_mode", [False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_standalone_flushdb(self, redis_client: RedisClient):
min_version = "6.2.0"
key1 = f"{{key}}-1{get_random_string(5)}"
key2 = f"{{key}}-2{get_random_string(5)}"
value = get_random_string(5)

# fill DB 0 and check size non-empty
assert await redis_client.select(0) is OK
await redis_client.set(key1, value)
assert await redis_client.dbsize() > 0

# fill DB 1 and check size non-empty
assert await redis_client.select(1) is OK
await redis_client.set(key2, value)
assert await redis_client.dbsize() > 0

# flush DB 1 and check again
assert await redis_client.flushdb() is OK
assert await redis_client.dbsize() == 0

# swith to DB 0, flush, and check
assert await redis_client.select(0) is OK
assert await redis_client.dbsize() > 0
assert await redis_client.flushdb(FlushMode.ASYNC) is OK
assert await redis_client.dbsize() == 0

# verify flush SYNC
if not await check_if_server_version_lt(redis_client, min_version):
await redis_client.set(key2, value)
assert await redis_client.dbsize() > 0
assert await redis_client.flushdb(FlushMode.SYNC) is OK
assert await redis_client.dbsize() == 0

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_getex(self, redis_client: TRedisClient):
Expand Down Expand Up @@ -6789,6 +6824,29 @@ async def test_cluster_fail_routing_by_address_if_no_port_is_provided(
with pytest.raises(RequestError):
await redis_client.info(route=ByAddressRoute("foo"))

@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_cluster_flushdb(self, redis_client: RedisClusterClient):
min_version = "6.2.0"
key = f"{{key}}-1{get_random_string(5)}"
value = get_random_string(5)

await redis_client.set(key, value)
assert await redis_client.dbsize() > 0
assert await redis_client.flushdb(route=AllPrimaries()) is OK
assert await redis_client.dbsize() == 0

await redis_client.set(key, value)
assert await redis_client.dbsize() > 0
assert await redis_client.flushdb(FlushMode.ASYNC, AllPrimaries()) is OK
assert await redis_client.dbsize() == 0

if not await check_if_server_version_lt(redis_client, min_version):
await redis_client.set(key, value)
assert await redis_client.dbsize() > 0
assert await redis_client.flushdb(FlushMode.SYNC, AllPrimaries()) is OK
assert await redis_client.dbsize() == 0


@pytest.mark.asyncio
class TestScripts:
Expand Down
6 changes: 6 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,17 @@ async def transaction_test(
args.append(OK)
transaction.flushall()
args.append(OK)
transaction.flushdb(FlushMode.ASYNC)
args.append(OK)
transaction.flushdb()
args.append(OK)

min_version = "6.2.0"
if not await check_if_server_version_lt(redis_client, min_version):
transaction.flushall(FlushMode.SYNC)
args.append(OK)
transaction.flushdb(FlushMode.SYNC)
args.append(OK)

min_version = "6.2.0"
if not await check_if_server_version_lt(redis_client, min_version):
Expand Down

0 comments on commit 2d499dc

Please sign in to comment.