Replace execute_command
with specific redis functions when possible (#346)
* replace execute_command * change to root_path
This commit is contained in:
parent
60f102d188
commit
57608d85f4
3 changed files with 8 additions and 7 deletions
|
@ -47,7 +47,7 @@ async def create_index(conn: redis.Redis, index_name, schema, current_hash):
|
||||||
f"You attempted to create an index in database {db_number}"
|
f"You attempted to create an index in database {db_number}"
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
await conn.execute_command(f"ft.info {index_name}")
|
await conn.ft(index_name).info()
|
||||||
except redis.ResponseError:
|
except redis.ResponseError:
|
||||||
await conn.execute_command(f"ft.create {index_name} {schema}")
|
await conn.execute_command(f"ft.create {index_name} {schema}")
|
||||||
# TODO: remove "type: ignore" when type stubs will be fixed
|
# TODO: remove "type: ignore" when type stubs will be fixed
|
||||||
|
@ -85,7 +85,7 @@ class IndexMigration:
|
||||||
|
|
||||||
async def drop(self):
|
async def drop(self):
|
||||||
try:
|
try:
|
||||||
await self.conn.execute_command(f"FT.DROPINDEX {self.index_name}")
|
await self.conn.ft(self.index_name).dropindex()
|
||||||
except redis.ResponseError:
|
except redis.ResponseError:
|
||||||
log.info("Index does not exist: %s", self.index_name)
|
log.info("Index does not exist: %s", self.index_name)
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ class Migrator:
|
||||||
current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest() # nosec
|
current_hash = hashlib.sha1(schema.encode("utf-8")).hexdigest() # nosec
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await conn.execute_command("ft.info", cls.Meta.index_name)
|
await conn.ft(cls.Meta.index_name).info()
|
||||||
except redis.ResponseError:
|
except redis.ResponseError:
|
||||||
self.migrations.append(
|
self.migrations.append(
|
||||||
IndexMigration(
|
IndexMigration(
|
||||||
|
|
|
@ -31,6 +31,7 @@ from pydantic.fields import ModelField, Undefined, UndefinedType
|
||||||
from pydantic.main import ModelMetaclass, validate_model
|
from pydantic.main import ModelMetaclass, validate_model
|
||||||
from pydantic.typing import NoArgAnyCallable
|
from pydantic.typing import NoArgAnyCallable
|
||||||
from pydantic.utils import Representation
|
from pydantic.utils import Representation
|
||||||
|
from redis.commands.json.path import Path
|
||||||
from typing_extensions import Protocol, get_args, get_origin
|
from typing_extensions import Protocol, get_args, get_origin
|
||||||
from ulid import ULID
|
from ulid import ULID
|
||||||
|
|
||||||
|
@ -1490,7 +1491,7 @@ class JsonModel(RedisModel, abc.ABC):
|
||||||
db = self._get_db(pipeline)
|
db = self._get_db(pipeline)
|
||||||
|
|
||||||
# TODO: Wrap response errors in a custom exception?
|
# TODO: Wrap response errors in a custom exception?
|
||||||
await db.execute_command("JSON.SET", self.key(), ".", self.json())
|
await db.json().set(self.key(), Path.root_path(), json.loads(self.json()))
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -1535,8 +1536,8 @@ class JsonModel(RedisModel, abc.ABC):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get(cls, pk: Any) -> "JsonModel":
|
async def get(cls, pk: Any) -> "JsonModel":
|
||||||
document = await cls.db().execute_command("JSON.GET", cls.make_primary_key(pk))
|
document = json.dumps(await cls.db().json().get(cls.make_key(pk)))
|
||||||
if not document:
|
if document == "null":
|
||||||
raise NotFoundError
|
raise NotFoundError
|
||||||
return cls.parse_raw(document)
|
return cls.parse_raw(document)
|
||||||
|
|
||||||
|
|
|
@ -293,7 +293,7 @@ async def test_saves_many_explicit_transaction(address, m):
|
||||||
async with m.Member.db().pipeline(transaction=True) as pipeline:
|
async with m.Member.db().pipeline(transaction=True) as pipeline:
|
||||||
await m.Member.add(members, pipeline=pipeline)
|
await m.Member.add(members, pipeline=pipeline)
|
||||||
assert result == [member1, member2]
|
assert result == [member1, member2]
|
||||||
assert await pipeline.execute() == ["OK", "OK"]
|
assert await pipeline.execute() == [True, True]
|
||||||
|
|
||||||
assert await m.Member.get(pk=member1.pk) == member1
|
assert await m.Member.get(pk=member1.pk) == member1
|
||||||
assert await m.Member.get(pk=member2.pk) == member2
|
assert await m.Member.get(pk=member2.pk) == member2
|
||||||
|
|
Loading…
Reference in a new issue