From 20b157f7e680ec4ef510bcec7b1e2c58eb769a12 Mon Sep 17 00:00:00 2001 From: dbernstein Date: Mon, 16 Dec 2024 13:59:19 -0800 Subject: [PATCH] [PP-1956] Ensure the context transaction manager is being used for creating identifiers in SweepMonitor runs. (#2181) --- src/palace/manager/api/axis.py | 2 +- src/palace/manager/api/bibliotheca.py | 5 ++++- src/palace/manager/api/overdrive.py | 2 +- src/palace/manager/core/metadata_layer.py | 14 ++++++++++---- src/palace/manager/sqlalchemy/model/identifier.py | 6 +++++- src/palace/manager/sqlalchemy/model/licensing.py | 2 ++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/palace/manager/api/axis.py b/src/palace/manager/api/axis.py index 52dc54ce8..2b2763a38 100644 --- a/src/palace/manager/api/axis.py +++ b/src/palace/manager/api/axis.py @@ -630,7 +630,7 @@ def update_book( # NOTE: availability is bibliographic.circulation, so it's a # little redundant to call availability.apply() -- it's taken # care of inside bibliographic.apply(). - bibliographic.apply(edition, self.collection, replace=policy) + bibliographic.apply(edition, self.collection, replace=policy, db=self._db) availability.apply(self._db, self.collection, replace=policy) return edition, new_edition, license_pool, new_license_pool diff --git a/src/palace/manager/api/bibliotheca.py b/src/palace/manager/api/bibliotheca.py index 52d0d5903..60f17f1df 100644 --- a/src/palace/manager/api/bibliotheca.py +++ b/src/palace/manager/api/bibliotheca.py @@ -1278,7 +1278,10 @@ def _process_metadata( edition, _ = metadata.edition(self._db) metadata.apply( - edition, collection=self.collection, replace=self.replacement_policy + edition, + collection=self.collection, + replace=self.replacement_policy, + db=self._db, ) diff --git a/src/palace/manager/api/overdrive.py b/src/palace/manager/api/overdrive.py index 31c85e7d0..2d0665874 100644 --- a/src/palace/manager/api/overdrive.py +++ b/src/palace/manager/api/overdrive.py @@ -1741,7 +1741,7 @@ def update_formats(self, licensepool): edition, ignore = self._edition(licensepool) replace = ReplacementPolicy.from_license_source(self._db) - metadata.apply(edition, self.collection, replace=replace) + metadata.apply(edition, self.collection, replace=replace, db=self._db) def update_licensepool(self, book_id): """Update availability information for a single book. diff --git a/src/palace/manager/core/metadata_layer.py b/src/palace/manager/core/metadata_layer.py index b42285dfe..074caeaf6 100644 --- a/src/palace/manager/core/metadata_layer.py +++ b/src/palace/manager/core/metadata_layer.py @@ -962,6 +962,7 @@ def apply( data_source=data_source, media_type=link.media_type, content=link.content, + db=_db, ) link_objects[link] = link_obj @@ -1436,6 +1437,7 @@ def apply( replace_formats=False, replace_rights=False, force=False, + db=None, ): """Apply this metadata to the given edition. @@ -1445,8 +1447,10 @@ def apply( New: If contributors changed, this is now considered a core change, so work.simple_opds_feed refresh can be triggered. """ - _db = Session.object_session(edition) - + if not db: + _db = Session.object_session(edition) + else: + _db = db # If summary, subjects, or measurements change, then any Work # associated with this edition will need a full presentation # recalculation. @@ -1637,6 +1641,7 @@ def _key(classification): rights_explanation=link.rights_explanation, original_resource=original_resource, transformation_settings=link.transformation_settings, + db=_db, ) if link.rel in self.REL_REQUIRES_NEW_PRESENTATION_EDITION: work_requires_new_presentation_edition = True @@ -1717,7 +1722,7 @@ def _key(classification): elif link.thumbnail: # We need to make sure that its thumbnail exists locally and # is associated with the original image. - self.make_thumbnail(data_source, link, link_obj) + self.make_thumbnail(_db, data_source, link, link_obj) # Make sure the work we just did shows up. made_changes = edition.calculate_presentation( @@ -1757,7 +1762,7 @@ def _key(classification): return edition, work_requires_new_presentation_edition - def make_thumbnail(self, data_source, link, link_obj): + def make_thumbnail(self, _db, data_source, link, link_obj): """Make sure a Hyperlink representing an image is connected to its thumbnail. """ @@ -1782,6 +1787,7 @@ def make_thumbnail(self, data_source, link, link_obj): data_source=data_source, media_type=thumbnail.media_type, content=thumbnail.content, + db=_db, ) # And make sure the thumbnail knows it's a thumbnail of the main # image. diff --git a/src/palace/manager/sqlalchemy/model/identifier.py b/src/palace/manager/sqlalchemy/model/identifier.py index aef3eb2bc..fbc83cc67 100644 --- a/src/palace/manager/sqlalchemy/model/identifier.py +++ b/src/palace/manager/sqlalchemy/model/identifier.py @@ -760,6 +760,7 @@ def add_link( rights_explanation=None, original_resource=None, transformation_settings=None, + db=None, ): """Create a link between this Identifier and a (potentially new) Resource. @@ -773,7 +774,10 @@ def add_link( Resource, ) - _db = Session.object_session(self) + if not db: + _db = Session.object_session(self) + else: + _db = db # Find or create the Resource. if not href: href = Hyperlink.generic_uri(data_source, self, rel, content) diff --git a/src/palace/manager/sqlalchemy/model/licensing.py b/src/palace/manager/sqlalchemy/model/licensing.py index 970567238..33f13ffe2 100644 --- a/src/palace/manager/sqlalchemy/model/licensing.py +++ b/src/palace/manager/sqlalchemy/model/licensing.py @@ -669,6 +669,7 @@ def add_link( rights_explanation=None, original_resource=None, transformation_settings=None, + db=None, ): """Add a link between this LicensePool and a Resource. @@ -699,6 +700,7 @@ def add_link( rights_explanation, original_resource, transformation_settings, + db, ) def needs_update(self):