Skip to content

Commit 57608d8

Browse files
authored
Replace execute_command with specific redis functions when possible (#346)
* replace execute_command * change to root_path
1 parent 60f102d commit 57608d8

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

aredis_om/model/migrations/migrator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def create_index(conn: redis.Redis, index_name, schema, current_hash):
4747
f"You attempted to create an index in database {db_number}"
4848
)
4949
try:
50-
await conn.execute_command(f"ft.info {index_name}")
50+
await conn.ft(index_name).info()
5151
except redis.ResponseError:
5252
await conn.execute_command(f"ft.create {index_name} {schema}")
5353
# TODO: remove "type: ignore" when type stubs will be fixed
@@ -85,7 +85,7 @@ async def create(self):
8585

8686
async def drop(self):
8787
try:
88-
await self.conn.execute_command(f"FT.DROPINDEX {self.index_name}")
88+
await self.conn.ft(self.index_name).dropindex()
8989
except redis.ResponseError:
9090
log.info("Index does not exist: %s", self.index_name)
9191

@@ -115,7 +115,7 @@ async def detect_migrations(self):
115115
current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest() # nosec
116116

117117
try:
118-
await conn.execute_command("ft.info", cls.Meta.index_name)
118+
await conn.ft(cls.Meta.index_name).info()
119119
except redis.ResponseError:
120120
self.migrations.append(
121121
IndexMigration(

aredis_om/model/model.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from pydantic.main import ModelMetaclass, validate_model
3232
from pydantic.typing import NoArgAnyCallable
3333
from pydantic.utils import Representation
34+
from redis.commands.json.path import Path
3435
from typing_extensions import Protocol, get_args, get_origin
3536
from ulid import ULID
3637

@@ -1490,7 +1491,7 @@ async def save(
14901491
db = self._get_db(pipeline)
14911492

14921493
# TODO: Wrap response errors in a custom exception?
1493-
await db.execute_command("JSON.SET", self.key(), ".", self.json())
1494+
await db.json().set(self.key(), Path.root_path(), json.loads(self.json()))
14941495
return self
14951496

14961497
@classmethod
@@ -1535,8 +1536,8 @@ async def update(self, **field_values):
15351536

15361537
@classmethod
15371538
async def get(cls, pk: Any) -> "JsonModel":
1538-
document = await cls.db().execute_command("JSON.GET", cls.make_primary_key(pk))
1539-
if not document:
1539+
document = json.dumps(await cls.db().json().get(cls.make_key(pk)))
1540+
if document == "null":
15401541
raise NotFoundError
15411542
return cls.parse_raw(document)
15421543

tests/test_json_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ async def test_saves_many_explicit_transaction(address, m):
293293
async with m.Member.db().pipeline(transaction=True) as pipeline:
294294
await m.Member.add(members, pipeline=pipeline)
295295
assert result == [member1, member2]
296-
assert await pipeline.execute() == ["OK", "OK"]
296+
assert await pipeline.execute() == [True, True]
297297

298298
assert await m.Member.get(pk=member1.pk) == member1
299299
assert await m.Member.get(pk=member2.pk) == member2

0 commit comments

Comments
 (0)