From 72ed0fd77115e30a98aa02ca96376b6c5f9c48b5 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Thu, 5 Mar 2026 10:38:39 +0000 Subject: [PATCH 1/3] Safe inserts of search maps into database --- .../workflows/spa/flush_spa_preprocess.py | 38 +++++++++++-------- src/murfey/workflows/tomo/tomo_metadata.py | 19 +++++----- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/murfey/workflows/spa/flush_spa_preprocess.py b/src/murfey/workflows/spa/flush_spa_preprocess.py index 88f84c39b..0ac946661 100644 --- a/src/murfey/workflows/spa/flush_spa_preprocess.py +++ b/src/murfey/workflows/spa/flush_spa_preprocess.py @@ -53,13 +53,15 @@ def register_grid_square( if grid_square_params.width is not None: grid_square_params.width_scaled = int(grid_square_params.width / 7.8) - try: - grid_square = murfey_db.exec( - select(GridSquare) - .where(GridSquare.name == gsid) - .where(GridSquare.tag == grid_square_params.tag) - .where(GridSquare.session_id == session_id) - ).one() + grid_square_query = murfey_db.exec( + select(GridSquare) + .where(GridSquare.name == gsid) + .where(GridSquare.tag == grid_square_params.tag) + .where(GridSquare.session_id == session_id) + ).all() + if grid_square_query: + # Grid square already exists in the murfey database + grid_square = grid_square_query[0] grid_square.x_location = grid_square_params.x_location or grid_square.x_location grid_square.y_location = grid_square_params.y_location or grid_square.y_location grid_square.x_stage_position = ( @@ -84,7 +86,8 @@ def register_grid_square( grid_square.image = grid_square_params.image or grid_square.image if _transport_object: _transport_object.do_update_grid_square(grid_square.id, grid_square_params) - except Exception: + else: + # No existing grid square in the murfey database if _transport_object: dcg = murfey_db.exec( select(DataCollectionGroup) @@ -151,13 +154,15 @@ def register_foil_hole( jpeg_size = Image.open(secured_foil_hole_image_path).size else: jpeg_size = (0, 0) - try: - foil_hole = murfey_db.exec( - select(FoilHole) - .where(FoilHole.name == foil_hole_params.name) - .where(FoilHole.grid_square_id == gsid) - .where(FoilHole.session_id == session_id) - ).one() + foil_hole_query = murfey_db.exec( + select(FoilHole) + .where(FoilHole.name == foil_hole_params.name) + .where(FoilHole.grid_square_id == gsid) + .where(FoilHole.session_id == session_id) + ).one() + if foil_hole_query: + # Foil hole already exists in the murfey database + foil_hole = foil_hole_query[0] foil_hole.x_location = foil_hole_params.x_location or foil_hole.x_location foil_hole.y_location = foil_hole_params.y_location or foil_hole.y_location foil_hole.x_stage_position = ( @@ -183,7 +188,8 @@ def register_foil_hole( _transport_object.do_update_foil_hole( foil_hole.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params ) - except Exception: + else: + # No existing foil hole in the murfey database if _transport_object: fh_ispyb_response = _transport_object.do_insert_foil_hole( gs.id, diff --git a/src/murfey/workflows/tomo/tomo_metadata.py b/src/murfey/workflows/tomo/tomo_metadata.py index 23b6490fb..cdaa44746 100644 --- a/src/murfey/workflows/tomo/tomo_metadata.py +++ b/src/murfey/workflows/tomo/tomo_metadata.py @@ -31,14 +31,15 @@ def register_search_map_in_database( .where(DataCollectionGroup.session_id == session_id) .where(DataCollectionGroup.tag == search_map_params.tag) ).one() - try: + search_map_query = murfey_db.exec( + select(SearchMap) + .where(SearchMap.name == search_map_name) + .where(SearchMap.tag == search_map_params.tag) + .where(SearchMap.session_id == session_id) + ).all() + if search_map_query: # See if there is already a search map with this name and update if so - search_map = murfey_db.exec( - select(SearchMap) - .where(SearchMap.name == search_map_name) - .where(SearchMap.tag == search_map_params.tag) - .where(SearchMap.session_id == session_id) - ).one() + search_map = search_map_query[0] search_map.x_stage_position = ( search_map_params.x_stage_position or search_map.x_stage_position ) @@ -100,8 +101,8 @@ def register_search_map_in_database( search_map.width = search_map_params.width or search_map.width if _transport_object: _transport_object.do_update_search_map(search_map.id, search_map_params) - except Exception as e: - logger.info(f"Registering new search map due to {e}", exc_info=True) + else: + logger.info(f"Registering new search map {search_map_name}") if _transport_object: sm_ispyb_response = _transport_object.do_insert_search_map( dcg.atlas_id, search_map_params From 94326e8d58dffe58034e69026d79c35b81925abd Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Thu, 5 Mar 2026 10:50:49 +0000 Subject: [PATCH 2/3] all not one --- src/murfey/workflows/spa/flush_spa_preprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/murfey/workflows/spa/flush_spa_preprocess.py b/src/murfey/workflows/spa/flush_spa_preprocess.py index 0ac946661..69886beaa 100644 --- a/src/murfey/workflows/spa/flush_spa_preprocess.py +++ b/src/murfey/workflows/spa/flush_spa_preprocess.py @@ -159,7 +159,7 @@ def register_foil_hole( .where(FoilHole.name == foil_hole_params.name) .where(FoilHole.grid_square_id == gsid) .where(FoilHole.session_id == session_id) - ).one() + ).all() if foil_hole_query: # Foil hole already exists in the murfey database foil_hole = foil_hole_query[0] From 235b80533288fca43968cc634db90c55d6b09792 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Thu, 5 Mar 2026 10:52:13 +0000 Subject: [PATCH 3/3] Sanitise input for log-injection --- src/murfey/workflows/tomo/tomo_metadata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/murfey/workflows/tomo/tomo_metadata.py b/src/murfey/workflows/tomo/tomo_metadata.py index cdaa44746..da22252ab 100644 --- a/src/murfey/workflows/tomo/tomo_metadata.py +++ b/src/murfey/workflows/tomo/tomo_metadata.py @@ -102,7 +102,7 @@ def register_search_map_in_database( if _transport_object: _transport_object.do_update_search_map(search_map.id, search_map_params) else: - logger.info(f"Registering new search map {search_map_name}") + logger.info(f"Registering new search map {sanitise(search_map_name)}") if _transport_object: sm_ispyb_response = _transport_object.do_insert_search_map( dcg.atlas_id, search_map_params