2222
2323class SchemaMismatchError (Exception ):
2424 """Raised when deployed code expects different field types than what's in Redis."""
25+
2526 pass
2627
2728
@@ -48,7 +49,9 @@ async def check_for_schema_mismatches(self, models: List[Any]) -> Dict[str, Any]
4849 for model in models :
4950 try :
5051 # Get the current index schema from Redis
51- index_name = f"{ model ._meta .global_key_prefix } :{ model ._meta .model_key_prefix } "
52+ index_name = (
53+ f"{ model ._meta .global_key_prefix } :{ model ._meta .model_key_prefix } "
54+ )
5255
5356 try :
5457 # Try to get index info
@@ -62,26 +65,31 @@ async def check_for_schema_mismatches(self, models: List[Any]) -> Dict[str, Any]
6265 datetime_fields = self ._get_datetime_fields (model )
6366
6467 for field_name , field_info in datetime_fields .items ():
65- redis_field_type = current_schema .get (field_name , {}).get ('type' )
66-
67- if redis_field_type == 'TAG' and field_info .get ('expected_type' ) == 'NUMERIC' :
68- mismatches .append ({
69- 'model' : model .__name__ ,
70- 'field' : field_name ,
71- 'current_type' : 'TAG' ,
72- 'expected_type' : 'NUMERIC' ,
73- 'index_name' : index_name
74- })
68+ redis_field_type = current_schema .get (field_name , {}).get ("type" )
69+
70+ if (
71+ redis_field_type == "TAG"
72+ and field_info .get ("expected_type" ) == "NUMERIC"
73+ ):
74+ mismatches .append (
75+ {
76+ "model" : model .__name__ ,
77+ "field" : field_name ,
78+ "current_type" : "TAG" ,
79+ "expected_type" : "NUMERIC" ,
80+ "index_name" : index_name ,
81+ }
82+ )
7583
7684 except Exception as e :
7785 log .warning (f"Could not check schema for model { model .__name__ } : { e } " )
7886 continue
7987
8088 return {
81- ' has_mismatches' : len (mismatches ) > 0 ,
82- ' mismatches' : mismatches ,
83- ' total_affected_models' : len (set (m [' model' ] for m in mismatches )),
84- ' recommendation' : self ._get_recommendation (mismatches )
89+ " has_mismatches" : len (mismatches ) > 0 ,
90+ " mismatches" : mismatches ,
91+ " total_affected_models" : len (set (m [" model" ] for m in mismatches )),
92+ " recommendation" : self ._get_recommendation (mismatches ),
8593 }
8694
8795 def _parse_index_schema (self , index_info : List ) -> Dict [str , Dict [str , Any ]]:
@@ -92,22 +100,27 @@ def _parse_index_schema(self, index_info: List) -> Dict[str, Dict[str, Any]]:
92100 info_dict = {}
93101 for i in range (0 , len (index_info ), 2 ):
94102 if i + 1 < len (index_info ):
95- key = index_info [i ].decode () if isinstance (index_info [i ], bytes ) else str (index_info [i ])
103+ key = (
104+ index_info [i ].decode ()
105+ if isinstance (index_info [i ], bytes )
106+ else str (index_info [i ])
107+ )
96108 value = index_info [i + 1 ]
97109 info_dict [key ] = value
98110
99111 # Extract attributes (field definitions)
100- attributes = info_dict .get (' attributes' , [])
112+ attributes = info_dict .get (" attributes" , [])
101113
102114 for attr in attributes :
103115 if isinstance (attr , list ) and len (attr ) >= 4 :
104- field_name = attr [0 ].decode () if isinstance (attr [0 ], bytes ) else str (attr [0 ])
105- field_type = attr [2 ].decode () if isinstance (attr [2 ], bytes ) else str (attr [2 ])
116+ field_name = (
117+ attr [0 ].decode () if isinstance (attr [0 ], bytes ) else str (attr [0 ])
118+ )
119+ field_type = (
120+ attr [2 ].decode () if isinstance (attr [2 ], bytes ) else str (attr [2 ])
121+ )
106122
107- schema [field_name ] = {
108- 'type' : field_type ,
109- 'raw_attr' : attr
110- }
123+ schema [field_name ] = {"type" : field_type , "raw_attr" : attr }
111124
112125 return schema
113126
@@ -117,20 +130,20 @@ def _get_datetime_fields(self, model) -> Dict[str, Dict[str, Any]]:
117130
118131 try :
119132 # Get model fields in a compatible way
120- if hasattr (model , ' _get_model_fields' ):
133+ if hasattr (model , " _get_model_fields" ):
121134 model_fields = model ._get_model_fields ()
122- elif hasattr (model , ' model_fields' ):
135+ elif hasattr (model , " model_fields" ):
123136 model_fields = model .model_fields
124137 else :
125- model_fields = getattr (model , ' __fields__' , {})
138+ model_fields = getattr (model , " __fields__" , {})
126139
127140 for field_name , field_info in model_fields .items ():
128141 # Check if this is a datetime field
129- field_type = getattr (field_info , ' annotation' , None )
142+ field_type = getattr (field_info , " annotation" , None )
130143 if field_type in (datetime .datetime , datetime .date ):
131144 datetime_fields [field_name ] = {
132- ' expected_type' : ' NUMERIC' , # New code expects NUMERIC
133- ' field_info' : field_info
145+ " expected_type" : " NUMERIC" , # New code expects NUMERIC
146+ " field_info" : field_info ,
134147 }
135148
136149 except Exception as e :
0 commit comments