From 221dea4bac00a656e36a638ea965e91beb308392 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 15 Nov 2022 16:04:05 -0500 Subject: [PATCH] Assure changes to process ids are updated in the cache on a file save, and remove old references that no longer exist. Still some work to do here. --- .../services/spec_file_service.py | 11 ++++++ .../unit/test_spec_file_service.py | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/spec_file_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/spec_file_service.py index 4f160b12e..59241de79 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/spec_file_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/spec_file_service.py @@ -160,6 +160,8 @@ class SpecFileService(FileSystemService): (ref for ref in references if ref.is_primary and ref.is_executable), None ) + SpecFileService.clear_caches_for_file(file_name, process_model_info) + for ref in references: # If no valid primary process is defined, default to the first process in the # updated file. @@ -235,6 +237,14 @@ class SpecFileService(FileSystemService): SpecFileService.update_message_trigger_cache(ref) SpecFileService.update_correlation_cache(ref) + @staticmethod + def clear_caches_for_file(file_name: str, process_model_info: ProcessModelInfo) -> None: + """Clear all caches related to a file""" + db.session.query(SpecReferenceCache).\ + filter(SpecReferenceCache.file_name == file_name).\ + filter(SpecReferenceCache.process_model_id == process_model_info.id).delete() + # fixme: likely the other caches should be cleared as well, but we don't have a clean way to do so yet. + @staticmethod def clear_caches() -> None: """Clear_caches.""" @@ -254,6 +264,7 @@ class SpecFileService(FileSystemService): if process_id_lookup is None: process_id_lookup = SpecReferenceCache.from_spec_reference(ref) db.session.add(process_id_lookup) + db.session.commit() else: if ref.relative_path != process_id_lookup.relative_path: full_bpmn_file_path = SpecFileService.full_path_from_relative_path( diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_spec_file_service.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_spec_file_service.py index d9a7adcb8..b5e5aa3cc 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_spec_file_service.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_spec_file_service.py @@ -119,6 +119,45 @@ class TestSpecFileService(BaseTest): == self.call_activity_nested_relative_file_path ) + def test_change_the_identifier_cleans_up_cache ( + self, + app: Flask, + client: FlaskClient, + with_db_and_bpmn_file_cleanup: None, + with_super_admin_user: UserModel, + ) -> None: + """ When a BPMN processes identifier is changed in a file, the old id + is removed from the cache""" + old_identifier = "ye_old_identifier" + process_id_lookup = SpecReferenceCache( + identifier=old_identifier, + relative_path=self.call_activity_nested_relative_file_path, + file_name=self.bpmn_file_name, + process_model_id=f"{self.process_group_id}/{self.process_model_id}", + type='process' + ) + db.session.add(process_id_lookup) + db.session.commit() + + self.create_group_and_model_with_bpmn( + client=client, + user=with_super_admin_user, + process_group_id=self.process_group_id, + process_model_id=self.process_model_id, + bpmn_file_name=self.bpmn_file_name, + bpmn_file_location=self.process_model_id, + ) + + bpmn_process_id_lookups = SpecReferenceCache.query.all() + assert len(bpmn_process_id_lookups) == 1 + assert bpmn_process_id_lookups[0].identifier != old_identifier + assert bpmn_process_id_lookups[0].identifier == "Level1" + assert ( + bpmn_process_id_lookups[0].relative_path + == self.call_activity_nested_relative_file_path + ) + + def test_load_reference_information( self, app: Flask,