From 071da6a66e44de4fb16e3e81a977d470e26b8672 Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Thu, 17 Aug 2023 15:04:36 +0300 Subject: [PATCH] Don't overwrite cache when removing items We call cascade_remove_items() from Toolbox without supplying db cache apparently to ensure that all cascading ids get cached in cascading_ids(). The call to make_cache(), however, overwrote all existing items in the cache destroying pending updates. We now supply a new parameter, keep_existing, to make_cache() to prevent overwriting existing items. Re spine-tools/Spine-Toolbox#2259 --- spinedb_api/db_mapping_base.py | 10 ++++++---- spinedb_api/db_mapping_remove_mixin.py | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/spinedb_api/db_mapping_base.py b/spinedb_api/db_mapping_base.py index a612bd03..c80fb985 100644 --- a/spinedb_api/db_mapping_base.py +++ b/spinedb_api/db_mapping_base.py @@ -2019,7 +2019,9 @@ def _reset_mapping(self): self.connection.execute(table.delete()) self.connection.execute("INSERT INTO alternative VALUES (1, 'Base', 'Base alternative', null)") - def make_cache(self, tablenames, include_descendants=False, include_ancestors=False, force_tablenames=None): + def make_cache( + self, tablenames, include_descendants=False, include_ancestors=False, force_tablenames=None, keep_existing=False + ): if include_descendants: tablenames |= { descendant for tablename in tablenames for descendant in self.descendant_tablenames.get(tablename, ()) @@ -2031,7 +2033,7 @@ def make_cache(self, tablenames, include_descendants=False, include_ancestors=Fa if force_tablenames: tablenames |= force_tablenames for tablename in tablenames & self.cache_sqs.keys(): - self._do_advance_cache_query(tablename) + self._do_advance_cache_query(tablename, keep_existing) return self.cache def _advance_cache_query(self, tablename, callback=None): @@ -2043,10 +2045,10 @@ def _advance_cache_query(self, tablename, callback=None): callback() return advanced - def _do_advance_cache_query(self, tablename): + def _do_advance_cache_query(self, tablename, keep_existing=False): table_cache = self.cache.table_cache(tablename) for x in self.query(getattr(self, self.cache_sqs[tablename])).yield_per(1000).enable_eagerloads(False): - table_cache.add_item(x._asdict()) + table_cache.add_item(x._asdict(), keep_existing) def _items_with_type_id(self, tablename, *items): type_id = { diff --git a/spinedb_api/db_mapping_remove_mixin.py b/spinedb_api/db_mapping_remove_mixin.py index f4718a3f..5c2cf779 100644 --- a/spinedb_api/db_mapping_remove_mixin.py +++ b/spinedb_api/db_mapping_remove_mixin.py @@ -83,6 +83,7 @@ def cascading_ids(self, cache=None, **kwargs): force_tablenames={"entity_metadata", "parameter_value_metadata"} if any(x in kwargs for x in ("entity_metadata", "parameter_value_metadata", "metadata")) else None, + keep_existing=True, ) ids = {} self._merge(ids, self._object_class_cascading_ids(kwargs.get("object_class", set()), cache))