From d52bd563f889236bc929ccc499e172cb109fbc58 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 2 May 2025 13:28:46 +0100 Subject: [PATCH] Bug 39322: (follow-up) Further improvements This patch updates the query logic of create_from_statistic for borrower attributes. We replace the count of attributes types with a query to fetch all attributes type codes where keep_for_pseudonymization is set. We then check for content in that array before using it to perform a filtered search on the patrons extended_attributes to only return attributes set for pseudonymization. This should result in the same skip of extended_attributes fetch should no attribute types be set to pseudonymization, but it should also reduce the 1 call to attribute types per attribute the patron has to just a single filtered call for patron attributes attributes followed by the store call for each pseudonymizated attribute. Test plan: As before, except there should be even fewer sql calls to the database than before for the case where there are attributes. Signed-off-by: Nick Clemens --- Koha/PseudonymizedTransaction.pm | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Koha/PseudonymizedTransaction.pm b/Koha/PseudonymizedTransaction.pm index cff6ecfe070..b29e1b0a5cc 100644 --- a/Koha/PseudonymizedTransaction.pm +++ b/Koha/PseudonymizedTransaction.pm @@ -98,15 +98,22 @@ sub create_from_statistic { $self->store(); - if ( $patron && Koha::Patron::Attribute::Types->search( { keep_for_pseudonymization => 1 } )->count > 0 ) { - my $extended_attributes = $patron->extended_attributes->unblessed; - for my $attribute (@$extended_attributes) { - next unless Koha::Patron::Attribute::Types->find( $attribute->{code} )->keep_for_pseudonymization; + if ($patron) { + my @kept_types = Koha::Patron::Attribute::Types->search( + { keep_for_pseudonymization => 1 }, + { columns => ['code'] } + )->get_column('code'); + + if (@kept_types) { + my $extended_attributes = + $patron->extended_attributes->search( { code => { -in => \@kept_types } } )->unblessed; - delete $attribute->{id}; - delete $attribute->{borrowernumber}; + for my $attribute (@$extended_attributes) { + delete $attribute->{id}; + delete $attribute->{borrowernumber}; - $self->_result->create_related( 'pseudonymized_borrower_attributes', $attribute ); + $self->_result->create_related( 'pseudonymized_borrower_attributes', $attribute ); + } } } -- 2.39.5