From bb9beccfd3b60a771ce2113dc56b118ee2581bd1 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Wed, 15 Feb 2023 15:54:37 +0000
Subject: [PATCH] Bug 32334: (QA follow-up) Improve finding schema file

Adds a schema parameter to the cmdline script now too.

Test plan:
Run sync_db_comments.pl with -schema file where file does not exist.
(On dev install) rename kohastructure.sql, try with[out] referring
to it using -schema. You could also use the standard path
intranet/cgi-bin/installer/data/mysql.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: David Nind <david@davidnind.com>
---
 Koha/Database/Commenter.pm           | 17 +++++++++++++++--
 misc/maintenance/sync_db_comments.pl |  7 +++++--
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/Koha/Database/Commenter.pm b/Koha/Database/Commenter.pm
index 9d4959e0ea..9ce562ff94 100644
--- a/Koha/Database/Commenter.pm
+++ b/Koha/Database/Commenter.pm
@@ -20,6 +20,7 @@ package Koha::Database::Commenter;
 use Modern::Perl;
 use File::Slurp qw(read_file);
 
+use C4::Context;
 use Koha::Exceptions;
 
 use constant KOHA_STRUCTURE => 'installer/data/mysql/kohastructure.sql';
@@ -76,7 +77,7 @@ sub new {
         unless ref($self->{dbh}) eq DBI_HANDLE_CLASS;
 
     $self->{database} //= ( $self->{dbh}->selectrow_array('SELECT DATABASE()') )[0];
-    $self->{schema_file} //= KOHA_STRUCTURE;
+    $self->_find_schema unless $self->{schema_file};
     $self->{schema_info} = {};
 
     return $self;
@@ -151,8 +152,20 @@ sub renumber {
 
 =head1 INTERNAL ROUTINES
 
+=head2 _find_schema
+
 =cut
 
+sub _find_schema {
+    my $self = shift;
+    my $rootdir = C4::Context->config('intranetdir');
+    if( -e "$rootdir/". KOHA_STRUCTURE ) {
+        $self->{schema_file} = "$rootdir/". KOHA_STRUCTURE;
+    } elsif( -e "$rootdir/intranet/cgi-bin/". KOHA_STRUCTURE ) {
+        $self->{schema_file} = "$rootdir/intranet/cgi-bin/". KOHA_STRUCTURE;
+    }
+}
+
 =head2 _fetch_schema_comments
 
 =cut
@@ -161,7 +174,7 @@ sub _fetch_schema_comments {
 # Wish we had a DBIC function for this, showing comments too ;) Now using kohastructure as source of truth.
     my ( $self ) = @_;
     my $file = $self->{schema_file};
-    Koha::Exceptions::FileNotFound->throw( filename => $file ) unless -e $file;
+    Koha::Exceptions::FileNotFound->throw( filename => $file ) unless $file && -e $file;
 
     return $self->{schema_info} if keys %{$self->{schema_info}};
 
diff --git a/misc/maintenance/sync_db_comments.pl b/misc/maintenance/sync_db_comments.pl
index 5014c4ac68..07ee001cb6 100755
--- a/misc/maintenance/sync_db_comments.pl
+++ b/misc/maintenance/sync_db_comments.pl
@@ -36,12 +36,15 @@ GetOptions(
   'help|h'     => \$cmd_args->{help},
   'renumber'   => \$cmd_args->{renumber},
   'reset'      => \$cmd_args->{reset},
+  'schema:s'   => \$cmd_args->{schema},
   'table:s'    => \$cmd_args->{table},
   'verbose|v'  => \$cmd_args->{verbose},
 );
 $cmd_args->{dry_run} = !$cmd_args->{commit};
 
-my $commenter = Koha::Database::Commenter->new({ database => delete $cmd_args->{database}, dbh => C4::Context->dbh });
+my $commenter = Koha::Database::Commenter->new({
+    database => delete $cmd_args->{database}, dbh => C4::Context->dbh, schema_file => delete $cmd_args->{schema},
+});
 my $messages = $cmd_args->{verbose} || $cmd_args->{dry_run} ? [] : undef;
 if( $cmd_args->{help} ) {
     pod2usage( -verbose => 2 );
@@ -71,7 +74,7 @@ misc/maintenance/sync_db_comments.pl
 
 =head1 SYNOPSIS
 
-    perl sync_db_comments.pl [-h] [-v] [-database DB_NAME] [-table TABLE_NAME] [-commit] [-clear|-reset|-renumber]
+    perl sync_db_comments.pl [-h] [-v] [-schema FILE ] [-database DB_NAME] [-table TABLE_NAME] [-commit] [-clear|-reset|-renumber]
 
 =head1 DESCRIPTION
 
-- 
2.30.2