Bug 25078: Re-introduce NewVersion
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Jun 2021 09:38:51 +0000 (11:38 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 16 Aug 2021 09:55:56 +0000 (11:55 +0200)
This is ugly, we re-add the code we removed in the previous patch.

We need to continue supporting "old" versions.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
C4/Installer.pm
installer/install.pl
koha-tmpl/intranet-tmpl/prog/en/modules/installer/step3.tt

index 4b62d5d..04e7e9f 100644 (file)
@@ -32,7 +32,7 @@ use vars qw(@ISA @EXPORT);
 BEGIN {
     require Exporter;
     @ISA = qw( Exporter );
-    push @EXPORT, qw( primary_key_exists unique_key_exists foreign_key_exists index_exists column_exists TableExists marc_framework_sql_list TransformToNum CheckVersion sanitize_zero_date update get_db_entries );
+    push @EXPORT, qw( primary_key_exists unique_key_exists foreign_key_exists index_exists column_exists TableExists marc_framework_sql_list TransformToNum CheckVersion NewVersion sanitize_zero_date update get_db_entries );
 };
 
 =head1 NAME
@@ -770,7 +770,11 @@ sub output_version {
     my $DBversion = $db_entry->{version};
     my $bug_number = $db_entry->{bug_number};
     my $time = $db_entry->{time};
-    my $done = $db_entry->{done} ? "done" : "failed";
+    my $done = defined $db_entry->{done}
+                ? $db_entry->{done}
+                    ? " done"
+                    : " failed"
+                : ""; # For old versions, we don't know if we succeed or failed
 
     unless ( ref($descriptions) ) {
         $descriptions = [ $descriptions ];
@@ -782,17 +786,17 @@ sub output_version {
         if ( @$descriptions > 1 ) {
             if ( $first ) {
                 unless ( $bug_number ) {
-                    push @output, sprintf "Upgrade to %s %s [%s]:", $DBversion, $done, $time;
+                    push @output, sprintf "Upgrade to %s%s [%s]:", $DBversion, $done, $time;
                 } else {
-                    push @output, sprintf "Upgrade to %s %s [%s]: Bug %5s", $DBversion, $done, $time, $bug_number;
+                    push @output, sprintf "Upgrade to %s%s [%s]: Bug %5s", $DBversion, $done, $time, $bug_number;
                 }
             }
             push @output, sprintf "\t\t\t\t\t\t   - %s", $description;
         } else {
             unless ( $bug_number ) {
-                push @output, sprintf "Upgrade to %s %s [%s]: %s", $DBversion, $done, $time, $description;
+                push @output, sprintf "Upgrade to %s%s [%s]: %s", $DBversion, $done, $time, $description;
             } else {
-                push @output, sprintf "Upgrade to %s %s [%s]: Bug %5s - %s", $DBversion, $done, $time, $bug_number, $description;
+                push @output, sprintf "Upgrade to %s%s [%s]: Bug %5s - %s", $DBversion, $done, $time, $bug_number, $description;
             }
         }
         $first = 0;
@@ -868,8 +872,26 @@ sub SetVersion {
     C4::Context::clear_syspref_cache(); # invalidate cached preferences
 }
 
+# DEPRECATED Don't use it!
+# Used for compatibility with older versions (from updatedatabase.pl)
 sub NewVersion {
-    # void FIXME replace me
+    my ( $DBversion, $bug_number, $descriptions ) = @_;
+
+    SetVersion($DBversion);
+
+    unless ( ref($descriptions) ) {
+        $descriptions = [ $descriptions ];
+    }
+
+    my $output = output_version( {
+            bug_number  => $bug_number,
+            description => $descriptions,
+            version     => $DBversion,
+            time        => POSIX::strftime( "%H:%M:%S", localtime ),
+    });
+
+    say join "\n", @$output;
+
 }
 
 =head2 CheckVersion
index 3547d6a..3d410f3 100755 (executable)
@@ -393,14 +393,45 @@ elsif ( $step && $step == 3 ) {
             chk_log( $logdir, "updatedatabase-error_$filename_suffix" )
         );
 
-        my $updatedatabase_path = C4::Context->config("intranetdir")
-          . "/installer/data/$info{dbms}/updatedatabase.pl";
+        my $cmd = C4::Context->config("intranetdir")
+          . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
+
+        system( $cmd );
+
+        my $fh;
+        open( $fh, "<:encoding(utf-8)", $logfilepath )
+          or die "Cannot open log file $logfilepath: $!";
+        my @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_report =>
+                  [ map { { line => $_ =~ s/\t/&emsp;&emsp;/gr } } split( /\n/, join( '', @report ) ) ]
+            );
+            $template->param( has_update_succeeds => 1 );
+        }
+        else {
+            eval { `rm $logfilepath` };
+        }
+        open( $fh, "<:encoding(utf-8)", $logfilepath_errors )
+          or die "Cannot open log file $logfilepath_errors: $!";
+        @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_errors =>
+                  [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ]
+            );
+            $template->param( has_update_errors => 1 );
+            warn
+"The following errors were returned while attempting to run the updatedatabase.pl script:\n";
+            foreach my $line (@report) { warn "$line\n"; }
+        }
+        else {
+            eval { `rm $logfilepath_errors` };
+        }
 
         my $db_entries = get_db_entries();
         my $report = update( $db_entries );
 
-        # FIXME restore log to logfilepath and logfilepath_errors
-
         $template->param( success => $report->{success}, error => $report->{error} );
 
         #warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n"; #FIXME restore this
index 04f9976..2fca0c4 100644 (file)
 
                 [% IF ( updatestructure ) %]
                     <h2>Updating database structure</h2>
+                    [% IF has_update_succeeds || success %]
+                        <p>Update report :</p>
+                    [% END %]
+
+                    [%# Success for old versions %]
+                    [% IF has_update_succeeds %]
+                        <ul>
+                            [% FOREACH l IN update_report %]
+                                [% SET line = l.line %]
+                                [% IF line.match('^Upgrade to') %]
+                                    <li>[% line | $raw %]</li>
+                                [% ELSE %]
+                                    [% line | $raw %]<br/>
+                                [% END %]
+                            [% END %]
+                        </ul>
+                    [% END %]
+
+                    [%# Success for new versions %]
                     [% IF success %]
-                        <p>Update report:</p>
                         <ul>
                             [% FOR s IN success %]
                                 [% FOR o IN s.output %]
                             [% END %]
                         </ul>
                     [% END %]
+
+                    [%# Errors for old versions %]
+                    [% IF has_update_errors %]
+                        <p>Update errors :</p>
+                        <ul>
+                            [% FOREACH update_error IN update_errors %]
+                                <li class="update_error">[% update_error.line | html %]</li>
+                            [% END %]
+                        </ul>
+                    [% END %]
+
+                    [%# Errors for new versions %]
                     [% IF error %]
                         <p>Update error :</p>
                         <ul>
                             [% END %]
                         </ul>
                     [% END %]
-                    [% UNLESS error %]
+                    [% UNLESS error OR has_update_errors %]
                         <p>Everything went okay. Update done.</p>
                         <p><a href="install.pl?step=3&amp;op=finished" class="btn btn-primary">Continue to log in to Koha</a></p>
                     [% ELSE %]