@@ -171,12 +171,12 @@ def store(self, report, report_file=None):
171171
172172 @time_function
173173 def _fetch_testcases_raw (self , condition ):
174- getprofiler ().enter_region ('sqlite query' )
174+ # Retrieve relevant session info and index it in Python
175+ getprofiler ().enter_region ('sqlite session query' )
175176 with self ._db_connect (self ._db_file ()) as conn :
176- query = ('SELECT session_uuid, testcases.uuid as uuid, json_blob '
177- 'FROM testcases '
178- 'JOIN sessions ON session_uuid == sessions.uuid '
179- f'WHERE { condition } ' )
177+ query = ('SELECT uuid, json_blob FROM sessions WHERE uuid IN '
178+ '(SELECT DISTINCT session_uuid FROM testcases '
179+ f'WHERE { condition } )' )
180180 getlogger ().debug (query )
181181
182182 # Create SQLite function for filtering using name patterns
@@ -185,10 +185,9 @@ def _fetch_testcases_raw(self, condition):
185185
186186 getprofiler ().exit_region ()
187187
188- # Retrieve session info
189188 sessions = {}
190- for session_uuid , uuid , json_blob in results :
191- sessions .setdefault (session_uuid , json_blob )
189+ for uuid , json_blob in results :
190+ sessions .setdefault (uuid , json_blob )
192191
193192 # Join all sessions and decode them at once
194193 reports_blob = '[' + ',' .join (sessions .values ()) + ']'
@@ -200,10 +199,20 @@ def _fetch_testcases_raw(self, condition):
200199 for rpt in reports :
201200 sessions [rpt ['session_info' ]['uuid' ]] = rpt
202201
203- # Extract the test case data
202+ # Extract the test case data by extracting their UUIDs
203+ getprofiler ().enter_region ('sqlite testcase query' )
204+ with self ._db_connect (self ._db_file ()) as conn :
205+ query = f'SELECT uuid FROM testcases WHERE { condition } '
206+ getlogger ().debug (query )
207+ conn .create_function ('REGEXP' , 2 , self ._db_matches )
208+ results = conn .execute (query ).fetchall ()
209+
210+ getprofiler ().exit_region ()
204211 testcases = []
205- for session_uuid , uuid , json_blob in results :
206- run_index , test_index = [int (x ) for x in uuid .split (':' )[1 :]]
212+ for uuid , * _ in results :
213+ session_uuid , run_index , test_index = uuid .split (':' )
214+ run_index = int (run_index )
215+ test_index = int (test_index )
207216 report = sessions [session_uuid ]
208217 testcases .append (
209218 report ['runs' ][run_index ]['testcases' ][test_index ],
@@ -286,6 +295,9 @@ def _do_remove(self, uuid):
286295 prefix = os .path .dirname (self .__db_file )
287296 with FileLock (os .path .join (prefix , '.db.lock' )):
288297 with self ._db_connect (self ._db_file ()) as conn :
298+ # Enable foreign keys for delete action to have cascade effect
299+ conn .execute ('PRAGMA foreign_keys = ON' )
300+
289301 # Check first if the uuid exists
290302 query = f'SELECT * FROM sessions WHERE uuid == "{ uuid } "'
291303 getlogger ().debug (query )
@@ -301,6 +313,8 @@ def _do_remove2(self, uuid):
301313 prefix = os .path .dirname (self .__db_file )
302314 with FileLock (os .path .join (prefix , '.db.lock' )):
303315 with self ._db_connect (self ._db_file ()) as conn :
316+ # Enable foreign keys for delete action to have cascade effect
317+ conn .execute ('PRAGMA foreign_keys = ON' )
304318 query = (f'DELETE FROM sessions WHERE uuid == "{ uuid } " '
305319 'RETURNING *' )
306320 getlogger ().debug (query )
0 commit comments