Skip to content

Commit 2d499dc

Browse files
authored
Python: add FLUSHDB command (#1680)
* Python: added FLUSHDB command (#393) * Updated CHANGELOG.md * Addressed review comments * Addressed review comments
1 parent 27705a4 commit 2d499dc

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* Python: Added LOLWUT command ([#1657](https://github.com/aws/glide-for-redis/pull/1657))
5858
* Python: Added XREADGROUP command ([#1679](https://github.com/aws/glide-for-redis/pull/1679))
5959
* Python: Added XACK command ([#1681](https://github.com/aws/glide-for-redis/pull/1681))
60+
* Python: Added FLUSHDB command ([#1680](https://github.com/aws/glide-for-redis/pull/1680))
6061

6162
### Breaking Changes
6263
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))

python/python/glide/async_commands/cluster_commands.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,39 @@ async def flushall(
515515
await self._execute_command(RequestType.FlushAll, args, route),
516516
)
517517

518+
async def flushdb(
519+
self, flush_mode: Optional[FlushMode] = None, route: Optional[Route] = None
520+
) -> TClusterResponse[TOK]:
521+
"""
522+
Deletes all the keys of the currently selected database. This command never fails.
523+
524+
See https://valkey.io/commands/flushdb for more details.
525+
526+
Args:
527+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
528+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
529+
in which case the client will route the command to the nodes defined by `route`.
530+
531+
Returns:
532+
TOK: OK.
533+
534+
Examples:
535+
>>> await client.flushdb()
536+
OK # The keys of the currently selected database were deleted.
537+
>>> await client.flushdb(FlushMode.ASYNC)
538+
OK # The keys of the currently selected database were deleted asynchronously.
539+
>>> await client.flushdb(FlushMode.ASYNC, AllNodes())
540+
OK # The keys of the currently selected database were deleted asynchronously on all nodes.
541+
"""
542+
args = []
543+
if flush_mode is not None:
544+
args.append(flush_mode.value)
545+
546+
return cast(
547+
TClusterResponse[TOK],
548+
await self._execute_command(RequestType.FlushDB, args, route),
549+
)
550+
518551
async def copy(
519552
self,
520553
source: str,

python/python/glide/async_commands/standalone_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,33 @@ async def flushall(self, flush_mode: Optional[FlushMode] = None) -> TOK:
460460
await self._execute_command(RequestType.FlushAll, args),
461461
)
462462

463+
async def flushdb(self, flush_mode: Optional[FlushMode] = None) -> TOK:
464+
"""
465+
Deletes all the keys of the currently selected database. This command never fails.
466+
467+
See https://valkey.io/commands/flushdb for more details.
468+
469+
Args:
470+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
471+
472+
Returns:
473+
TOK: OK.
474+
475+
Examples:
476+
>>> await client.flushdb()
477+
OK # The keys of the currently selected database were deleted.
478+
>>> await client.flushdb(FlushMode.ASYNC)
479+
OK # The keys of the currently selected database were deleted asynchronously.
480+
"""
481+
args = []
482+
if flush_mode is not None:
483+
args.append(flush_mode.value)
484+
485+
return cast(
486+
TOK,
487+
await self._execute_command(RequestType.FlushDB, args),
488+
)
489+
463490
async def copy(
464491
self,
465492
source: str,

python/python/glide/async_commands/transaction.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,6 +3667,25 @@ def flushall(
36673667
args.append(flush_mode.value)
36683668
return self.append_command(RequestType.FlushAll, args)
36693669

3670+
def flushdb(
3671+
self: TTransaction, flush_mode: Optional[FlushMode] = None
3672+
) -> TTransaction:
3673+
"""
3674+
Deletes all the keys of the currently selected database. This command never fails.
3675+
3676+
See https://valkey.io/commands/flushdb for more details.
3677+
3678+
Args:
3679+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
3680+
3681+
Command Response:
3682+
TOK: OK.
3683+
"""
3684+
args = []
3685+
if flush_mode is not None:
3686+
args.append(flush_mode.value)
3687+
return self.append_command(RequestType.FlushDB, args)
3688+
36703689
def getex(
36713690
self: TTransaction, key: str, expiry: Optional[ExpiryGetEx] = None
36723691
) -> TTransaction:

python/python/tests/test_async_client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6283,6 +6283,41 @@ async def test_flushall(self, redis_client: TRedisClient):
62836283
assert await redis_client.flushall(FlushMode.SYNC, AllPrimaries()) is OK
62846284
assert await redis_client.dbsize() == 0
62856285

6286+
@pytest.mark.parametrize("cluster_mode", [False])
6287+
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
6288+
async def test_standalone_flushdb(self, redis_client: RedisClient):
6289+
min_version = "6.2.0"
6290+
key1 = f"{{key}}-1{get_random_string(5)}"
6291+
key2 = f"{{key}}-2{get_random_string(5)}"
6292+
value = get_random_string(5)
6293+
6294+
# fill DB 0 and check size non-empty
6295+
assert await redis_client.select(0) is OK
6296+
await redis_client.set(key1, value)
6297+
assert await redis_client.dbsize() > 0
6298+
6299+
# fill DB 1 and check size non-empty
6300+
assert await redis_client.select(1) is OK
6301+
await redis_client.set(key2, value)
6302+
assert await redis_client.dbsize() > 0
6303+
6304+
# flush DB 1 and check again
6305+
assert await redis_client.flushdb() is OK
6306+
assert await redis_client.dbsize() == 0
6307+
6308+
# swith to DB 0, flush, and check
6309+
assert await redis_client.select(0) is OK
6310+
assert await redis_client.dbsize() > 0
6311+
assert await redis_client.flushdb(FlushMode.ASYNC) is OK
6312+
assert await redis_client.dbsize() == 0
6313+
6314+
# verify flush SYNC
6315+
if not await check_if_server_version_lt(redis_client, min_version):
6316+
await redis_client.set(key2, value)
6317+
assert await redis_client.dbsize() > 0
6318+
assert await redis_client.flushdb(FlushMode.SYNC) is OK
6319+
assert await redis_client.dbsize() == 0
6320+
62866321
@pytest.mark.parametrize("cluster_mode", [True, False])
62876322
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
62886323
async def test_getex(self, redis_client: TRedisClient):
@@ -6789,6 +6824,29 @@ async def test_cluster_fail_routing_by_address_if_no_port_is_provided(
67896824
with pytest.raises(RequestError):
67906825
await redis_client.info(route=ByAddressRoute("foo"))
67916826

6827+
@pytest.mark.parametrize("cluster_mode", [True])
6828+
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
6829+
async def test_cluster_flushdb(self, redis_client: RedisClusterClient):
6830+
min_version = "6.2.0"
6831+
key = f"{{key}}-1{get_random_string(5)}"
6832+
value = get_random_string(5)
6833+
6834+
await redis_client.set(key, value)
6835+
assert await redis_client.dbsize() > 0
6836+
assert await redis_client.flushdb(route=AllPrimaries()) is OK
6837+
assert await redis_client.dbsize() == 0
6838+
6839+
await redis_client.set(key, value)
6840+
assert await redis_client.dbsize() > 0
6841+
assert await redis_client.flushdb(FlushMode.ASYNC, AllPrimaries()) is OK
6842+
assert await redis_client.dbsize() == 0
6843+
6844+
if not await check_if_server_version_lt(redis_client, min_version):
6845+
await redis_client.set(key, value)
6846+
assert await redis_client.dbsize() > 0
6847+
assert await redis_client.flushdb(FlushMode.SYNC, AllPrimaries()) is OK
6848+
assert await redis_client.dbsize() == 0
6849+
67926850

67936851
@pytest.mark.asyncio
67946852
class TestScripts:

python/python/tests/test_transaction.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,17 @@ async def transaction_test(
543543
args.append(OK)
544544
transaction.flushall()
545545
args.append(OK)
546+
transaction.flushdb(FlushMode.ASYNC)
547+
args.append(OK)
548+
transaction.flushdb()
549+
args.append(OK)
546550

547551
min_version = "6.2.0"
548552
if not await check_if_server_version_lt(redis_client, min_version):
549553
transaction.flushall(FlushMode.SYNC)
550554
args.append(OK)
555+
transaction.flushdb(FlushMode.SYNC)
556+
args.append(OK)
551557

552558
min_version = "6.2.0"
553559
if not await check_if_server_version_lt(redis_client, min_version):

0 commit comments

Comments
 (0)