From 7ef64d10af9ffa1ff24847bec10108cb88325cf0 Mon Sep 17 00:00:00 2001
From: Fridolin Somers <fridolin.somers@biblibre.com>
Date: Wed, 22 Mar 2023 21:09:35 -1000
Subject: [PATCH] Bug 33297: (follow-up) DBRev check wrong preference exists

Better db upgrade messages

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 .../data/mysql/atomicupdate/bug_33297.pl      | 29 ++++++++++++-------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/installer/data/mysql/atomicupdate/bug_33297.pl b/installer/data/mysql/atomicupdate/bug_33297.pl
index 1f94fcc98bb..3df7ec1478f 100755
--- a/installer/data/mysql/atomicupdate/bug_33297.pl
+++ b/installer/data/mysql/atomicupdate/bug_33297.pl
@@ -6,20 +6,27 @@ return {
     up => sub {
         my ($args) = @_;
         my ($dbh, $out) = @$args{qw(dbh out)};
-        # Correct preference may have been generated via interface
-        my ($correct_syspref_exists) = $dbh->selectrow_array(q{
-            SELECT COUNT(*) FROM systempreferences WHERE variable='RetainPatronsSearchTerms'
+        my ($wrong_syspref_exists) = $dbh->selectrow_array(q{
+            SELECT COUNT(*) FROM systempreferences WHERE variable='RetainPatronSearchTerms'
         });
-        if ($correct_syspref_exists) {
-            $dbh->do(q{
-                DELETE FROM systempreferences WHERE variable='RetainPatronSearchTerms'
+        if ($wrong_syspref_exists) {
+            # Correct preference may have been generated via interface
+            my ($correct_syspref_exists) = $dbh->selectrow_array(q{
+                SELECT COUNT(*) FROM systempreferences WHERE variable='RetainPatronsSearchTerms'
             });
-            say $out "Wrong system preference 'RetainPatronSearchTerms' deleted";
+            if ( $correct_syspref_exists ) {
+                $dbh->do(q{
+                    DELETE FROM systempreferences WHERE variable='RetainPatronSearchTerms'
+                });
+                say $out "Wrong system preference 'RetainPatronSearchTerms' deleted";
+            } else {
+                $dbh->do(q{
+                    UPDATE systempreferences SET variable='RetainPatronsSearchTerms' WHERE variable='RetainPatronSearchTerms'
+                });
+                say $out "Wrong system preference 'RetainPatronSearchTerms' renamed 'RetainPatronsSearchTerms'";
+            }
         } else {
-            $dbh->do(q{
-                UPDATE systempreferences SET variable='RetainPatronsSearchTerms' WHERE variable='RetainPatronSearchTerms'
-            });
-            say $out "Wrong system preference 'RetainPatronSearchTerms' renamed 'RetainPatronsSearchTerms'";
+            say $out "Wrong system preference 'RetainPatronSearchTerms' does not exist";
         }
     },
 };
-- 
2.25.1