From 29d4077cbf84c1e2cf93b1a88df98e70b39d136d Mon Sep 17 00:00:00 2001
From: David Cook <dcook@prosentient.com.au>
Date: Wed, 18 Jan 2023 03:20:29 +0000
Subject: [PATCH] Bug 28267: Add warnings for database row formats

This patch adds a warning on about.pl regarding database row formats
other than "DYNAMIC". It points to the Koha community wiki for more
information on how to resolve the problem.

This patch also stops the installer if there are any database tables
with a row format other than "DYNAMIC". It points to the Koha community
wiki for more information on how to resolve the problem.

Test plan:
0a. Apply the patch
0b. koha-plack --restart kohadev
1a. koha-mysql kohadev
1b. ALTER TABLE tags ROW_FORMAT=COMPACT;
2. Go to http://localhost:8081/cgi-bin/koha/about.pl
3. Note that there is a warning about database row formats
4a. koha-mysql kohadev
4b. ALTER TABLE tags ROW_FORMAT=DYNAMIC;
5. Go to http://localhost:8081/cgi-bin/koha/about.pl
6. Note that there is no warning about database row formats

7a. Manually change the version in Koha.pm to a higher version
7b. koha-plack --restart kohadev
8a. koha-mysql kohadev
8b. ALTER TABLE tags ROW_FORMAT=COMPACT;
9. Go to http://localhost:8081/
10. Log into the web installer and click through until you reach
a warning telling you that you have database row formats other
than 'DYNAMIC'
11. Note that you can't progress with the installer until
this problem is resolved
12a. koha-mysql kohadev
12b. ALTER TABLE tags ROW_FORMAT=DYNAMIC;
13. Refresh the page or redo the web installer process
14. Note that you're no longer blocked from running the installer
due to database row format
15. Undo your change to Koha.pm
16. Profit

Note: Due to bug 32665 you'll see an unrelated warning on
step 3

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
---
 Koha/Installer.pm                             | 60 +++++++++++++++++++
 about.pl                                      |  9 +++
 installer/install.pl                          |  8 +++
 .../intranet-tmpl/prog/en/modules/about.tt    | 11 +++-
 .../prog/en/modules/installer/step2.tt        |  9 +++
 t/db_dependent/Koha/Installer.t               | 48 +++++++++++++++
 6 files changed, 144 insertions(+), 1 deletion(-)
 create mode 100644 Koha/Installer.pm
 create mode 100644 t/db_dependent/Koha/Installer.t

diff --git a/Koha/Installer.pm b/Koha/Installer.pm
new file mode 100644
index 0000000000..4d2f3d12ac
--- /dev/null
+++ b/Koha/Installer.pm
@@ -0,0 +1,60 @@
+package Koha::Installer;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use C4::Context;
+
+=head1 NAME
+
+Koha::Installer - Class that provides installation related methods
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 check_db_row_format
+
+=cut
+
+sub check_db_row_format {
+    my ($class) = @_;
+    my %result = ();
+    my $database = C4::Context->config('database');
+    if ($database){
+        my $dbh = C4::Context->dbh;
+        my $sql = q#
+            SELECT count(table_name)
+            FROM information_schema.tables
+            WHERE
+                table_schema = ?
+                AND row_format != "Dynamic"
+        #;
+        my $sth = $dbh->prepare($sql);
+        $sth->execute($database);
+        my $row = $sth->fetchrow_arrayref;
+        my $count = $row->[0];
+        if ($count){
+            $result{count} = $count;
+        }
+    }
+    return \%result;
+}
+
+1;
diff --git a/about.pl b/about.pl
index f4d1c7a959..1160d60585 100755
--- a/about.pl
+++ b/about.pl
@@ -54,6 +54,7 @@ use Koha::Illrequest::Config;
 use Koha::SearchEngine::Elasticsearch;
 use Koha::Logger;
 use Koha::Filter::MARC::ViewPolicy;
+use Koha::Installer;
 
 use C4::Members::Statistics;
 
@@ -621,6 +622,14 @@ $template->param( 'bad_yaml_prefs' => \@bad_yaml_prefs ) if @bad_yaml_prefs;
     }
 }
 
+#BZ 28267: Warn administrators if there are database rows with a format other than 'DYNAMIC'
+{
+    my $db_row_format_result = Koha::Installer->check_db_row_format();
+    if ( my $count = $db_row_format_result->{count} ){
+        $template->param( warnDbRowFormat => $count );
+    }
+}
+
 my %versions = C4::Context::get_versions();
 
 $template->param(
diff --git a/installer/install.pl b/installer/install.pl
index 063832569c..14697b8461 100755
--- a/installer/install.pl
+++ b/installer/install.pl
@@ -32,6 +32,7 @@ use C4::Installer;
 use C4::Installer::PerlModules;
 
 use Koha;
+use Koha::Installer;
 
 my $query = CGI->new;
 my $step  = $query->param('step');
@@ -185,6 +186,13 @@ elsif ( $step && $step == 2 ) {
                     }
                 }
                 $template->param( "checkgrantaccess" => $grantaccess );
+
+                my $db_row_format_result = Koha::Installer->check_db_row_format();
+                if ( my $count = $db_row_format_result->{count} ){
+                    $template->param( warnDbRowFormat => $count );
+                    $template->param( error => "InnoDB row format" );
+                }
+
             }    # End mysql connect check...
 
             elsif ( $info{dbms} eq "Pg" ) {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
index 5b7834a434..248d1c51a7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
@@ -219,7 +219,16 @@
         </div>
 
         <div role="tabpanel" class="tab-pane" id="sysinfo">
-    [% IF warnRequireChoosingExistingAuthority || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatronOPACPrivacy || warnPrefAnonymousPatronAnonSuggestions || warnPrefAnonymousPatronOPACPrivacy_PatronDoesNotExist || warnPrefAnonymousPatronAnonSuggestions_PatronDoesNotExist || warnPrefKohaAdminEmailAddress || warnPrefOpacHiddenItems || invalid_yesno.count || warnNoActiveCurrency || warnIsRootUser || xml_config_warnings.size || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching || warnILLConfiguration || has_ai_issues || oauth2_missing_deps || bad_yaml_prefs || warnRelationships || log4perl_errors || config_bcrypt_settings_no_set || warnHiddenBiblionumbers.size || warnConnectBroker || elasticsearch_has_missing %]
+    [% IF warnRequireChoosingExistingAuthority || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatronOPACPrivacy || warnPrefAnonymousPatronAnonSuggestions || warnPrefAnonymousPatronOPACPrivacy_PatronDoesNotExist || warnPrefAnonymousPatronAnonSuggestions_PatronDoesNotExist || warnPrefKohaAdminEmailAddress || warnPrefOpacHiddenItems || invalid_yesno.count || warnNoActiveCurrency || warnIsRootUser || xml_config_warnings.size || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching || warnILLConfiguration || has_ai_issues || oauth2_missing_deps || bad_yaml_prefs || warnRelationships || log4perl_errors || config_bcrypt_settings_no_set || warnHiddenBiblionumbers.size || warnConnectBroker || elasticsearch_has_missing || warnDbRowFormat %]
+        [% IF ( warnDbRowFormat ) %]
+            <h2>Database row format incorrect</h2>
+            <p>Database tables with a row format other than 'DYNAMIC': [% warnDbRowFormat | html %]</p>
+            <p>You may experience problems upgrading to newer versions of Koha unless you update the row format for your database tables.</p>
+            <p>To know how to avoid this problem see the related wiki page:
+                <a href="https://wiki.koha-community.org/wiki/Database_row_format">Database row format</a>
+            </p>
+        [% END %]
+
         [% IF (warnIsRootUser) %]
             <h2>Warning regarding current user</h2>
             <p>You are logged in as the database administrative user. This is not recommended because some parts of Koha will not function as expected when using this account.</p>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step2.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step2.tt
index fab5eb4355..d44cc4c2ad 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step2.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step2.tt
@@ -49,6 +49,15 @@
                                     <p>Need help? For help with granting permissions, please search online for "[% dbms | $HtmlTags tag=>'code' %] manual grant permissions"
                                     </p>
                                 [% END %]
+                                [% IF ( warnDbRowFormat ) %]
+                                    <div class="alert alert-danger" role="alert">
+                                        <p>Database tables with a row format other than 'DYNAMIC': [% warnDbRowFormat | html %]</p>
+                                        <p>You may experience problems upgrading to newer versions of Koha unless you update the row format for your database tables.</p>
+                                        <p>To know how to avoid this problem see the related wiki page:
+                                            <a href="https://wiki.koha-community.org/wiki/Database_row_format">Database row format</a>
+                                        </p>
+                                    </div>
+                                [% END %]
                             [% ELSE %]
                                 <div class="alert alert-danger" role="alert"><p>No database named [% dbname | $HtmlTags tag=>'code' %] detected.</p></div>
                                 <p>Please create the database before continuing.</p>
diff --git a/t/db_dependent/Koha/Installer.t b/t/db_dependent/Koha/Installer.t
new file mode 100644
index 0000000000..f6b6878e61
--- /dev/null
+++ b/t/db_dependent/Koha/Installer.t
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# This file is part of Koha
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use C4::Context;
+use Koha::Database;
+use Koha::Installer;
+
+use t::lib::TestBuilder;
+
+my $schema  = Koha::Database->new->schema;
+
+subtest 'check_db_row_format() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $dbh = C4::Context->dbh;
+    $dbh->do("ALTER TABLE tags row_format=COMPACT;");
+
+    my $pos_result = Koha::Installer->check_db_row_format;
+    is($pos_result->{count},1,"Detected problem table");
+
+    $dbh->do("ALTER TABLE tags row_format=DYNAMIC;");
+
+    my $neg_result = Koha::Installer->check_db_row_format;
+    is($neg_result->{count},undef,"Detected no problem tables");
+
+    $schema->storage->txn_rollback;
+};
-- 
2.20.1