44from enum import Enum
55from typing import List , Optional
66
7- from aioredis import Redis , ResponseError
7+ from ... import redis
88
99
1010log = logging .getLogger (__name__ )
@@ -39,18 +39,19 @@ def schema_hash_key(index_name):
3939 return f"{ index_name } :hash"
4040
4141
42- async def create_index (redis : Redis , index_name , schema , current_hash ):
43- db_number = redis .connection_pool .connection_kwargs .get ("db" )
42+ async def create_index (conn : redis . Redis , index_name , schema , current_hash ):
43+ db_number = conn .connection_pool .connection_kwargs .get ("db" )
4444 if db_number and db_number > 0 :
4545 raise MigrationError (
4646 "Creating search indexes is only supported in database 0. "
4747 f"You attempted to create an index in database { db_number } "
4848 )
4949 try :
50- await redis .execute_command (f"ft.info { index_name } " )
51- except ResponseError :
52- await redis .execute_command (f"ft.create { index_name } { schema } " )
53- await redis .set (schema_hash_key (index_name ), current_hash )
50+ await conn .execute_command (f"ft.info { index_name } " )
51+ except redis .ResponseError :
52+ await conn .execute_command (f"ft.create { index_name } { schema } " )
53+ # TODO: remove "type: ignore" when type stubs will be fixed
54+ await conn .set (schema_hash_key (index_name ), current_hash ) # type: ignore
5455 else :
5556 log .info ("Index already exists, skipping. Index hash: %s" , index_name )
5657
@@ -67,7 +68,7 @@ class IndexMigration:
6768 schema : str
6869 hash : str
6970 action : MigrationAction
70- redis : Redis
71+ conn : redis . Redis
7172 previous_hash : Optional [str ] = None
7273
7374 async def run (self ):
@@ -78,14 +79,14 @@ async def run(self):
7879
7980 async def create (self ):
8081 try :
81- await create_index (self .redis , self .index_name , self .schema , self .hash )
82- except ResponseError :
82+ await create_index (self .conn , self .index_name , self .schema , self .hash )
83+ except redis . ResponseError :
8384 log .info ("Index already exists: %s" , self .index_name )
8485
8586 async def drop (self ):
8687 try :
87- await self .redis .execute_command (f"FT.DROPINDEX { self .index_name } " )
88- except ResponseError :
88+ await self .conn .execute_command (f"FT.DROPINDEX { self .index_name } " )
89+ except redis . ResponseError :
8990 log .info ("Index does not exist: %s" , self .index_name )
9091
9192
@@ -105,7 +106,7 @@ async def detect_migrations(self):
105106
106107 for name , cls in model_registry .items ():
107108 hash_key = schema_hash_key (cls .Meta .index_name )
108- redis = cls .db ()
109+ conn = cls .db ()
109110 try :
110111 schema = cls .redisearch_schema ()
111112 except NotImplementedError :
@@ -114,21 +115,21 @@ async def detect_migrations(self):
114115 current_hash = hashlib .sha1 (schema .encode ("utf-8" )).hexdigest () # nosec
115116
116117 try :
117- await redis .execute_command ("ft.info" , cls .Meta .index_name )
118- except ResponseError :
118+ await conn .execute_command ("ft.info" , cls .Meta .index_name )
119+ except redis . ResponseError :
119120 self .migrations .append (
120121 IndexMigration (
121122 name ,
122123 cls .Meta .index_name ,
123124 schema ,
124125 current_hash ,
125126 MigrationAction .CREATE ,
126- redis ,
127+ conn ,
127128 )
128129 )
129130 continue
130131
131- stored_hash = await redis .get (hash_key )
132+ stored_hash = await conn .get (hash_key )
132133 schema_out_of_date = current_hash != stored_hash
133134
134135 if schema_out_of_date :
@@ -140,7 +141,7 @@ async def detect_migrations(self):
140141 schema ,
141142 current_hash ,
142143 MigrationAction .DROP ,
143- redis ,
144+ conn ,
144145 stored_hash ,
145146 )
146147 )
@@ -151,7 +152,7 @@ async def detect_migrations(self):
151152 schema ,
152153 current_hash ,
153154 MigrationAction .CREATE ,
154- redis ,
155+ conn ,
155156 stored_hash ,
156157 )
157158 )
0 commit comments