From ddf531fc4099509ed0ade8f01699d8a0da923549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Mon, 22 Sep 2025 16:11:03 -0300 Subject: [PATCH] Bug 40855: Replace raw SQL in Standard backend edititem with add_or_update_attributes This patch refactors the edititem() method in the Standard ILL backend to use the modern add_or_update_attributes() method instead of raw SQL delete-all-and-recreate operations. Problems with the old approach: - 40+ lines of raw SQL bypassing ORM validation - Inefficient delete-all-then-insert pattern - Complex conditional SQL based on column existence - Manual transaction handling and error-prone bind parameters - Difficult to maintain and debug Benefits of the new approach: - Single line replaces 40+ lines of raw SQL - Uses ORM validation and relationships - Only updates attributes when values actually change - Automatic backend column handling - Consistent with other modernized Standard backend methods - Better performance and maintainability The refactoring: - Removes all raw SQL queries (DELETE and INSERT statements) - Removes manual transaction handling - Removes conditional column existence checks - Replaces with single add_or_update_attributes() call - Maintains identical functionality and behavior Test plan: 1. Apply patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/ILL/Backend/Standard.t => SUCCESS: Tests pass! Refactoring maintains compatibility 3. Test ILL request editing in Standard backend: - Edit existing ILL request metadata - Add/modify custom fields - Verify attributes updated correctly => SUCCESS: Same functionality, cleaner implementation 4. Sign off :-D Signed-off-by: Martin Renvoize --- Koha/ILL/Backend/Standard.pm | 42 ++---------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/Koha/ILL/Backend/Standard.pm b/Koha/ILL/Backend/Standard.pm index 57529f049f6..4aaff4641db 100644 --- a/Koha/ILL/Backend/Standard.pm +++ b/Koha/ILL/Backend/Standard.pm @@ -479,46 +479,8 @@ sub edititem { # generate $request_details my $request_details = _get_request_details( $params, $other ); - # We do this with a 'dump all and repopulate approach' inside - # a transaction, easier than catering for create, update & delete - my $dbh = C4::Context->dbh; - my $schema = Koha::Database->new->schema; - $schema->txn_do( - sub { - # Delete all existing attributes for this request - $dbh->do( - q| - DELETE FROM illrequestattributes WHERE illrequest_id=? - |, undef, $request->id - ); - - # Insert all current attributes for this request - foreach my $attr ( keys %{$request_details} ) { - my $value = $request_details->{$attr}; - if ( $value && length $value > 0 ) { - if ( column_exists( 'illrequestattributes', 'backend' ) ) { - my @bind = ( $request->id, 'Standard', $attr, $value, 0 ); - $dbh->do( - q| - INSERT INTO illrequestattributes - (illrequest_id, backend, type, value, readonly) VALUES - (?, ?, ?, ?, ?) - |, undef, @bind - ); - } else { - my @bind = ( $request->id, $attr, $value, 0 ); - $dbh->do( - q| - INSERT INTO illrequestattributes - (illrequest_id, type, value, readonly) VALUES - (?, ?, ?, ?) - |, undef, @bind - ); - } - } - } - } - ); + # Update request attributes using modern ORM method + $request->add_or_update_attributes($request_details); ## -> create response. return { -- 2.51.0