Skip to content

Commit d8dbc8b

Browse files
abrookinsclaude
andcommitted
Fix sync CLI commands and test failures
- Remove run_async() calls from sync CLI commands to prevent coroutine errors - Add AsyncMock -> Mock transformation in unasync configuration - Fix test_create_and_status_empty to use clean_redis fixture 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 815567e commit d8dbc8b

File tree

6 files changed

+558
-13
lines changed

6 files changed

+558
-13
lines changed

aredis_om/model/migrations/migrator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,19 @@ async def detect_migrations(self):
112112

113113
for name, cls in model_registry.items():
114114
hash_key = schema_hash_key(cls.Meta.index_name)
115-
115+
116116
# Try to get a connection, but handle event loop issues gracefully
117117
try:
118118
conn = self.conn or cls.db()
119119
except RuntimeError as e:
120120
if "Event loop is closed" in str(e):
121121
# Model connection is bound to closed event loop, create fresh one
122122
from ...connections import get_redis_connection
123+
123124
conn = get_redis_connection()
124125
else:
125126
raise
126-
127+
127128
try:
128129
schema = cls.redisearch_schema()
129130
except NotImplementedError:
@@ -137,6 +138,7 @@ async def detect_migrations(self):
137138
if "Event loop is closed" in str(e):
138139
# Connection had event loop issues, try with a fresh connection
139140
from ...connections import get_redis_connection
141+
140142
conn = get_redis_connection()
141143
try:
142144
await conn.ft(cls.Meta.index_name).info()

aredis_om/model/migrations/schema_migrator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ async def create_migration_file(self, name: str) -> Optional[str]:
241241
ops_lines.append("]")
242242
ops_literal = "\n".join(ops_lines)
243243

244-
template = f'''"""
244+
template = '''"""
245245
Schema migration: {name}
246246
247-
Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
247+
Created: {created_time}
248248
"""
249249
250250
import hashlib
@@ -270,7 +270,7 @@ async def up(self) -> None:
270270
await self.redis.ft(index_name).dropindex()
271271
except Exception:
272272
pass
273-
await self.redis.execute_command(f"FT.CREATE {index_name} {{new_schema}}".format(new_schema=new_schema))
273+
await self.redis.execute_command(f"FT.CREATE {{index_name}} {{new_schema}}".format(index_name=index_name, new_schema=new_schema))
274274
new_hash = hashlib.sha1(new_schema.encode('utf-8')).hexdigest()
275275
await self.redis.set(schema_hash_key(index_name), new_hash) # type: ignore[misc]
276276
await self.redis.set(schema_text_key(index_name), new_schema) # type: ignore[misc]
@@ -284,11 +284,18 @@ async def down(self) -> None:
284284
except Exception:
285285
pass
286286
if prev_schema:
287-
await self.redis.execute_command(f"FT.CREATE {index_name} {{prev_schema}}".format(prev_schema=prev_schema))
287+
await self.redis.execute_command(f"FT.CREATE {{index_name}} {{prev_schema}}".format(index_name=index_name, prev_schema=prev_schema))
288288
prev_hash = hashlib.sha1(prev_schema.encode('utf-8')).hexdigest()
289289
await self.redis.set(schema_hash_key(index_name), prev_hash) # type: ignore[misc]
290290
await self.redis.set(schema_text_key(index_name), prev_schema) # type: ignore[misc]
291-
'''
291+
'''.format(
292+
name=name,
293+
created_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
294+
class_name=class_name,
295+
migration_id=migration_id,
296+
description=description,
297+
ops_literal=ops_literal,
298+
)
292299

293300
with open(filepath, "w") as f:
294301
f.write(template)

aredis_om/model/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,7 @@ async def save(
24462446
if "Event loop is closed" in str(e):
24472447
# Connection is bound to closed event loop, refresh it and retry
24482448
from ..connections import get_redis_connection
2449+
24492450
self._meta.database = get_redis_connection()
24502451
db = self._get_db(pipeline)
24512452
await db.hset(self.key(), mapping=document)
@@ -2656,6 +2657,7 @@ async def save(
26562657
if "Event loop is closed" in str(e):
26572658
# Connection is bound to closed event loop, refresh it and retry
26582659
from ..connections import get_redis_connection
2660+
26592661
self._meta.database = get_redis_connection()
26602662
db = self._get_db(pipeline)
26612663
await db.json().set(self.key(), Path.root_path(), data)

make_sync.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
":tests.": ":tests_sync.",
1010
"pytest_asyncio": "pytest",
1111
"py_test_mark_asyncio": "py_test_mark_sync",
12+
"AsyncMock": "Mock",
1213
}
1314

1415

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ async def redis():
2323
# Per-test client bound to current loop; close after each test
2424
# Force a new connection for each test to avoid event loop issues
2525
import os
26+
2627
url = os.environ.get("REDIS_OM_URL", "redis://localhost:6380?decode_responses=True")
2728
from aredis_om import redis as redis_module
28-
29+
2930
client = redis_module.Redis.from_url(url, decode_responses=True)
3031
try:
3132
# Ensure client is working with current event loop

0 commit comments

Comments
 (0)