From 199fc5ddf97b354055268f4b48f818964d71c521 Mon Sep 17 00:00:00 2001 From: Daniel Bernstein Date: Wed, 20 Nov 2024 13:48:25 -0800 Subject: [PATCH] [PP-1956] Ensure the context transaction manager is being used for creating identifiers in SweepMonitor runs. --- 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 ba031821d..0e5263487 100644 --- a/src/palace/manager/api/axis.py +++ b/src/palace/manager/api/axis.py @@ -629,7 +629,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 653245992..93ec1f95c 100644 --- a/src/palace/manager/api/overdrive.py +++ b/src/palace/manager/api/overdrive.py @@ -1725,7 +1725,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 ab530ce0a..a29450d83 100644 --- a/src/palace/manager/sqlalchemy/model/identifier.py +++ b/src/palace/manager/sqlalchemy/model/identifier.py @@ -756,6 +756,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. @@ -769,7 +770,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 cc04b4912..dc23aafb4 100644 --- a/src/palace/manager/sqlalchemy/model/licensing.py +++ b/src/palace/manager/sqlalchemy/model/licensing.py @@ -659,6 +659,7 @@ def add_link( rights_explanation=None, original_resource=None, transformation_settings=None, + db=None, ): """Add a link between this LicensePool and a Resource. @@ -689,6 +690,7 @@ def add_link( rights_explanation, original_resource, transformation_settings, + db, ) def needs_update(self):