Bug 22084: Plugin upgrade method and database plugin version storage will never be...
authorKyle M Hall <kyle@bywatersolutions.com>
Tue, 8 Jan 2019 18:10:53 +0000 (13:10 -0500)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 1 Mar 2019 11:40:45 +0000 (11:40 +0000)
If an existing plugin begins using the upgrade() method, it will never actually get triggered because the installation has already occurred and no database plugin version was stored at the time. Without that database version, the upgrade method will never be triggered.

In addition, if the plugin has no upgrade method, the installed version is never updated in the database!

Lastly, when an upgrade succeeds it prints a warning that the upgrade failed.

Test Plan:
1) Install an older version of the Kitchen sink plugin:
   * https://github.com/bywatersolutions/koha-plugin-kitchen-sink/releases/download/v2.1.0/koha-plugin-kitchen-sink-v2.1.0.kpz
2) Delete the "__INSTALLED_VERSION__" key, and the "last_upgraded" key from plugin_data for this plugin, to simulate having installed from an earlier version of Koha
3) Upgrade to the latest version of the Kitchen sink plugin:
   * https://github.com/bywatersolutions/koha-plugin-kitchen-sink/releases/download/v2.1.22/koha-plugin-kitchen-sink-v2.1.22.kpz
4) Note "__INSTALLED_VERSION__" still doesn't exist for the plugin, in the plugin_data table
5) Apply this patch
6) Restart all the things
7) Browse to the Kitchen sink configuration page, this should trigger the upgrade method
8) Note the "__INSTALLED_VERSION__" and "last_upgraded" keys exist in plugin_data for the Kitchen Sink plugin

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit 8c2e03020dc02519dec70b9697794a3624c73937)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/Plugins/Base.pm

index ed87253..2cebe63 100644 (file)
@@ -45,7 +45,7 @@ sub new {
     my $self = bless( $args, $class );
 
     my $plugin_version = $self->get_metadata->{version};
-    my $database_version = $self->retrieve_data('__INSTALLED_VERSION__');
+    my $database_version = $self->retrieve_data('__INSTALLED_VERSION__') || 0;
 
     ## Run the installation method if it exists and hasn't been run before
     if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
@@ -57,13 +57,16 @@ sub new {
         } else {
             warn "Plugin $class failed during installation!";
         }
-    } elsif ( $self->can('upgrade') && $plugin_version && $database_version ) {
+    } elsif ( $self->can('upgrade') ) {
         if ( _version_compare( $plugin_version, $database_version ) == 1 ) {
             if ( $self->upgrade() ) {
                 $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
+            } else {
                 warn "Plugin $class failed during upgrade!";
             }
         }
+    } elsif ( $plugin_version ne $database_version ) {
+        $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
     }
 
     return $self;