1414
1515def get_worker_id ():
1616 """Get pytest-xdist worker ID for test isolation."""
17- return os .environ .get (' PYTEST_XDIST_WORKER' , ' main' )
17+ return os .environ .get (" PYTEST_XDIST_WORKER" , " main" )
1818
1919
2020def get_worker_prefix ():
@@ -30,11 +30,11 @@ def get_worker_prefix():
3030async def clean_redis (redis ):
3131 """Provide a clean Redis instance for schema migration tests."""
3232 worker_prefix = get_worker_prefix ()
33-
33+
3434 # Worker-specific Redis keys
3535 applied_migrations_key = f"redis_om:schema_applied_migrations:{ worker_prefix } "
3636 schema_key_pattern = f"redis_om:schema:*:{ worker_prefix } "
37-
37+
3838 # Cleanup before test
3939 await redis .delete (applied_migrations_key )
4040 keys = await redis .keys (schema_key_pattern )
@@ -79,7 +79,9 @@ async def test_create_migration_file_when_no_ops(redis, monkeypatch):
7979
8080 try :
8181 with tempfile .TemporaryDirectory () as tmp :
82- migrator = _WorkerAwareSchemaMigrator (redis_client = redis , migrations_dir = tmp )
82+ migrator = _WorkerAwareSchemaMigrator (
83+ redis_client = redis , migrations_dir = tmp
84+ )
8385 fp = await migrator .create_migration_file ("noop" )
8486 assert fp is None
8587 finally :
@@ -90,7 +92,9 @@ async def test_create_migration_file_when_no_ops(redis, monkeypatch):
9092
9193async def test_create_and_status_empty (clean_redis ):
9294 with tempfile .TemporaryDirectory () as tmp :
93- migrator = _WorkerAwareSchemaMigrator (redis_client = clean_redis , migrations_dir = tmp )
95+ migrator = _WorkerAwareSchemaMigrator (
96+ redis_client = clean_redis , migrations_dir = tmp
97+ )
9498 status = await migrator .status ()
9599 assert status ["total_migrations" ] == 0
96100 assert status ["applied_count" ] == 0
@@ -107,13 +111,15 @@ async def test_rollback_noop(redis):
107111
108112class _WorkerAwareSchemaMigrator (SchemaMigrator ):
109113 """SchemaMigrator that uses worker-specific Redis keys for test isolation."""
110-
114+
111115 def __init__ (self , redis_client , migrations_dir ):
112116 super ().__init__ (redis_client , migrations_dir )
113117 self .worker_prefix = get_worker_prefix ()
114118 # Override the class constant with worker-specific key
115- self .APPLIED_MIGRATIONS_KEY = f"redis_om:schema_applied_migrations:{ self .worker_prefix } "
116-
119+ self .APPLIED_MIGRATIONS_KEY = (
120+ f"redis_om:schema_applied_migrations:{ self .worker_prefix } "
121+ )
122+
117123 async def mark_unapplied (self , migration_id : str ):
118124 """Mark migration as unapplied using worker-specific key."""
119125 await self .redis .srem (self .APPLIED_MIGRATIONS_KEY , migration_id )
@@ -138,8 +144,12 @@ async def up(self) -> None:
138144 await self .redis .execute_command (f"FT.CREATE { index_name } { new_schema } " )
139145 # Update tracking keys with worker isolation
140146 new_hash = hashlib .sha1 (new_schema .encode ("utf-8" )).hexdigest ()
141- await self .redis .set (f"{ schema_hash_key (index_name )} :{ worker_prefix } " , new_hash )
142- await self .redis .set (f"{ schema_text_key (index_name )} :{ worker_prefix } " , new_schema )
147+ await self .redis .set (
148+ f"{ schema_hash_key (index_name )} :{ worker_prefix } " , new_hash
149+ )
150+ await self .redis .set (
151+ f"{ schema_text_key (index_name )} :{ worker_prefix } " , new_schema
152+ )
143153
144154 async def down (self ) -> None :
145155 """Rollback the migration operations."""
@@ -156,8 +166,12 @@ async def down(self) -> None:
156166 f"FT.CREATE { index_name } { prev_schema } "
157167 )
158168 prev_hash = hashlib .sha1 (prev_schema .encode ("utf-8" )).hexdigest ()
159- await self .redis .set (f"{ schema_hash_key (index_name )} :{ worker_prefix } " , prev_hash )
160- await self .redis .set (f"{ schema_text_key (index_name )} :{ worker_prefix } " , prev_schema )
169+ await self .redis .set (
170+ f"{ schema_hash_key (index_name )} :{ worker_prefix } " , prev_hash
171+ )
172+ await self .redis .set (
173+ f"{ schema_text_key (index_name )} :{ worker_prefix } " , prev_schema
174+ )
161175
162176
163177class _TestSchemaMigrationNoRollback (BaseSchemaMigration ):
@@ -176,7 +190,9 @@ async def up(self) -> None:
176190async def test_rollback_successful_single_operation (clean_redis ):
177191 """Test successful rollback of migration with single operation."""
178192 with tempfile .TemporaryDirectory () as tmp :
179- migrator = _WorkerAwareSchemaMigrator (redis_client = clean_redis , migrations_dir = tmp )
193+ migrator = _WorkerAwareSchemaMigrator (
194+ redis_client = clean_redis , migrations_dir = tmp
195+ )
180196 redis = clean_redis
181197 worker_prefix = get_worker_prefix ()
182198
@@ -189,7 +205,9 @@ async def test_rollback_successful_single_operation(clean_redis):
189205 await redis .execute_command (f"FT.CREATE { index_name } { original_schema } " )
190206 original_hash = hashlib .sha1 (original_schema .encode ("utf-8" )).hexdigest ()
191207 await redis .set (f"{ schema_hash_key (index_name )} :{ worker_prefix } " , original_hash )
192- await redis .set (f"{ schema_text_key (index_name )} :{ worker_prefix } " , original_schema )
208+ await redis .set (
209+ f"{ schema_text_key (index_name )} :{ worker_prefix } " , original_schema
210+ )
193211
194212 # Create and apply migration
195213 migration = _TestSchemaMigration (
@@ -226,8 +244,12 @@ async def mock_discover():
226244 assert success is True
227245
228246 # Verify rollback restored original schema
229- restored_hash = await redis .get (f"{ schema_hash_key (index_name )} :{ worker_prefix } " )
230- restored_text = await redis .get (f"{ schema_text_key (index_name )} :{ worker_prefix } " )
247+ restored_hash = await redis .get (
248+ f"{ schema_hash_key (index_name )} :{ worker_prefix } "
249+ )
250+ restored_text = await redis .get (
251+ f"{ schema_text_key (index_name )} :{ worker_prefix } "
252+ )
231253 assert restored_hash == original_hash
232254 assert restored_text == original_schema
233255
@@ -314,9 +336,13 @@ async def test_rollback_multiple_operations(redis):
314336 hash1 = hashlib .sha1 (original_schema1 .encode ("utf-8" )).hexdigest ()
315337 hash2 = hashlib .sha1 (original_schema2 .encode ("utf-8" )).hexdigest ()
316338 await redis .set (f"{ schema_hash_key (index1_name )} :{ worker_prefix } " , hash1 )
317- await redis .set (f"{ schema_text_key (index1_name )} :{ worker_prefix } " , original_schema1 )
339+ await redis .set (
340+ f"{ schema_text_key (index1_name )} :{ worker_prefix } " , original_schema1
341+ )
318342 await redis .set (f"{ schema_hash_key (index2_name )} :{ worker_prefix } " , hash2 )
319- await redis .set (f"{ schema_text_key (index2_name )} :{ worker_prefix } " , original_schema2 )
343+ await redis .set (
344+ f"{ schema_text_key (index2_name )} :{ worker_prefix } " , original_schema2
345+ )
320346
321347 # Create migration with multiple operations
322348 migration = _TestSchemaMigration (
@@ -353,10 +379,18 @@ async def mock_discover():
353379 assert success is True
354380
355381 # Verify both indices were rolled back to original schemas
356- restored_hash1 = await redis .get (f"{ schema_hash_key (index1_name )} :{ worker_prefix } " )
357- restored_text1 = await redis .get (f"{ schema_text_key (index1_name )} :{ worker_prefix } " )
358- restored_hash2 = await redis .get (f"{ schema_hash_key (index2_name )} :{ worker_prefix } " )
359- restored_text2 = await redis .get (f"{ schema_text_key (index2_name )} :{ worker_prefix } " )
382+ restored_hash1 = await redis .get (
383+ f"{ schema_hash_key (index1_name )} :{ worker_prefix } "
384+ )
385+ restored_text1 = await redis .get (
386+ f"{ schema_text_key (index1_name )} :{ worker_prefix } "
387+ )
388+ restored_hash2 = await redis .get (
389+ f"{ schema_hash_key (index2_name )} :{ worker_prefix } "
390+ )
391+ restored_text2 = await redis .get (
392+ f"{ schema_text_key (index2_name )} :{ worker_prefix } "
393+ )
360394
361395 assert restored_hash1 == hash1
362396 assert restored_text1 == original_schema1
@@ -556,7 +590,9 @@ async def test_rollback_state_consistency(redis):
556590 await redis .execute_command (f"FT.CREATE { index_name } { original_schema } " )
557591 original_hash = hashlib .sha1 (original_schema .encode ("utf-8" )).hexdigest ()
558592 await redis .set (f"{ schema_hash_key (index_name )} :{ worker_prefix } " , original_hash )
559- await redis .set (f"{ schema_text_key (index_name )} :{ worker_prefix } " , original_schema )
593+ await redis .set (
594+ f"{ schema_text_key (index_name )} :{ worker_prefix } " , original_schema
595+ )
560596
561597 migration = _TestSchemaMigration (
562598 migration_id = "008_consistency_test" ,
@@ -593,8 +629,12 @@ async def mock_discover():
593629 assert success is True
594630
595631 # Verify complete state consistency after rollback
596- restored_hash = await redis .get (f"{ schema_hash_key (index_name )} :{ worker_prefix } " )
597- restored_text = await redis .get (f"{ schema_text_key (index_name )} :{ worker_prefix } " )
632+ restored_hash = await redis .get (
633+ f"{ schema_hash_key (index_name )} :{ worker_prefix } "
634+ )
635+ restored_text = await redis .get (
636+ f"{ schema_text_key (index_name )} :{ worker_prefix } "
637+ )
598638
599639 # Hash and text should match original exactly
600640 assert restored_hash == original_hash
0 commit comments