@@ -78,6 +78,11 @@ def _select_model_class(base_cls, py312_cls, py311_cls, py310_cls, model_name: s
7878
7979from .scenarios import get_enabled_scenarios , get_scenario # noqa: E402
8080
81+ from providers .fixtures ._common import (
82+ safe_drop_table , safe_drop_table_async ,
83+ disable_fk_checks , enable_fk_checks ,
84+ disable_fk_checks_async , enable_fk_checks_async ,
85+ )
8186
8287class EventsProviderBase :
8388 def __init__ (self ):
@@ -115,26 +120,7 @@ def _setup_model(self, model_class: Type[ActiveRecord], scenario_name: str, tabl
115120 if backend_instance not in self ._active_backends :
116121 self ._active_backends .append (backend_instance )
117122 options = ExecutionOptions (stmt_type = StatementType .DDL )
118- try :
119- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 0" )
120- try :
121- drop_expr = DropTableExpression (
122- dialect = backend_instance .dialect ,
123- table = TableExpression (backend_instance .dialect , table_name ),
124- if_exists = True ,
125- )
126- backend_instance .execute (* drop_expr .to_sql (), options = options )
127- except Exception :
128- backend_instance .execute (f"DROP TABLE IF EXISTS `{ table_name } `" )
129- except Exception :
130- try :
131- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
132- except Exception :
133- pass
134- try :
135- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
136- except Exception :
137- pass
123+ safe_drop_table (backend_instance , backend_instance .dialect , table_name )
138124 if fn := TABLE_EXPRESSIONS .get (table_name ):
139125 create_expr = fn (backend_instance .dialect , table_name )
140126 create_sql , params = to_sqlserver_ddl_sql (create_expr )
@@ -152,24 +138,15 @@ def setup_event_tracking_model(self, scenario_name: str) -> Type[ActiveRecord]:
152138
153139 def cleanup_after_test (self , scenario_name : str ):
154140 for backend_instance in self ._active_backends :
155- try :
156- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 0" )
157- for table_name in ["event_tests" , "event_tracking_models" ]:
158- try :
159- backend_instance .execute (f"DROP TABLE IF EXISTS `{ table_name } `" )
160- except Exception :
161- pass
162- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
163- except Exception :
164- try :
165- backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
166- except Exception :
167- pass
168- finally :
141+ for table_name in ["event_tests" , "event_tracking_models" ]:
169142 try :
170- backend_instance . disconnect ( )
143+ safe_drop_table ( backend_instance , backend_instance . dialect , table_name )
171144 except Exception :
172145 pass
146+ try :
147+ backend_instance .disconnect ()
148+ except Exception :
149+ pass
173150 self ._active_backends .clear ()
174151
175152
@@ -194,26 +171,7 @@ async def _setup_async_model(
194171 if backend_instance not in self ._active_async_backends :
195172 self ._active_async_backends .append (backend_instance )
196173 options = ExecutionOptions (stmt_type = StatementType .DDL )
197- try :
198- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 0" )
199- try :
200- drop_expr = DropTableExpression (
201- dialect = backend_instance .dialect ,
202- table = TableExpression (backend_instance .dialect , table_name ),
203- if_exists = True ,
204- )
205- await backend_instance .execute (* drop_expr .to_sql (), options = options )
206- except Exception :
207- await backend_instance .execute (f"DROP TABLE IF EXISTS `{ table_name } `" )
208- except Exception :
209- try :
210- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
211- except Exception :
212- pass
213- try :
214- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
215- except Exception :
216- pass
174+ await safe_drop_table_async (backend_instance , backend_instance .dialect , table_name )
217175 if fn := TABLE_EXPRESSIONS .get (table_name ):
218176 create_expr = fn (backend_instance .dialect , table_name )
219177 create_sql , params = to_sqlserver_ddl_sql (create_expr )
@@ -231,23 +189,13 @@ async def setup_event_tracking_model(self, scenario_name: str) -> Type[ActiveRec
231189
232190 async def cleanup_after_test (self , scenario_name : str ):
233191 for backend_instance in self ._active_async_backends :
234- try :
235- try :
236- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 0" )
237- for table_name in ["event_tests" , "event_tracking_models" ]:
238- try :
239- await backend_instance .execute (f"DROP TABLE IF EXISTS `{ table_name } `" )
240- except Exception :
241- pass
242- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
243- except Exception :
244- try :
245- await backend_instance .execute ("SET FOREIGN_KEY_CHECKS = 1" )
246- except Exception :
247- pass
248- finally :
192+ for table_name in ["event_tests" , "event_tracking_models" ]:
249193 try :
250- await backend_instance . disconnect ( )
194+ await safe_drop_table_async ( backend_instance , backend_instance . dialect , table_name )
251195 except Exception :
252196 pass
197+ try :
198+ await backend_instance .disconnect ()
199+ except Exception :
200+ pass
253201 self ._active_async_backends .clear ()
0 commit comments