From 5ea6b379301f05e69a3184da4f030847afe4ae04 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 26 Apr 2018 15:07:07 -0400 Subject: [PATCH] Bug 20669: Add upgrade method to plugins It would be nice if plugins had an upgrade method to handle altering tables and such when a new version of a plugin is installed. Right now it must be done in a completely bespoke manner. --- Koha/Plugins/Base.pm | 42 ++++++++++++++++++++++++++++++++++++++++++ t/Koha/Plugin/Test.pm | 5 +++++ t/db_dependent/Plugins.t | 1 + 3 files changed, 48 insertions(+) diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index c39a8c91bf..ebf12cf315 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -47,9 +47,18 @@ sub new { if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) { if ( $self->install() ) { $self->store_data( { '__INSTALLED__' => 1 } ); + if ( my $version = $self->get_metadata->{version} ) { + $self->store_data({ '__INSTALLED_VERSION__' => $version }); + } } else { warn "Plugin $class failed during installation!"; } + } elsif ( $self->can('upgrade') && $self->get_metadata->{version} && $self->retrieve_data('__INSTALLED_VERSION__') ) { + if ( $self->_version_compare( $self->get_metadata->{version}, $self->retrieve_data('__INSTALLED_VERSION__') ) > 0 ) { + unless ( $self->upgrade() ) { + warn "Plugin $class failed during upgrade!"; + } + } } return $self; @@ -218,6 +227,39 @@ sub output { output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options ); } +=head2 _version_compare + +Utility method to compare two version numbers + +if ( $self->version_compare( '2.6.26', '2.6.0' ) == 1 ) { + print "2.6.26 is greater than 2.6.0\n"; +} + +=cut + +sub _version_compare { + my $ver1 = shift || 0; + my $ver2 = shift || 0; + my @v1 = split /[.+:~-]/, $ver1; + my @v2 = split /[.+:~-]/, $ver2; + + for ( my $i = 0 ; $i < max( scalar(@v1), scalar(@v2) ) ; $i++ ) { + + # Add missing version parts if one string is shorter than the other + # i.e. 0 should be lt 0.2.1 and not equal, so we append .0 + # -> 0.0.0 <=> 0.2.1 -> -1 + push( @v1, 0 ) unless defined( $v1[$i] ); + push( @v2, 0 ) unless defined( $v2[$i] ); + if ( int( $v1[$i] ) > int( $v2[$i] ) ) { + return 1; + } + elsif ( int( $v1[$i] ) < int( $v2[$i] ) ) { + return -1; + } + } + return 0; +} + 1; __END__ diff --git a/t/Koha/Plugin/Test.pm b/t/Koha/Plugin/Test.pm index 3452af9302..ae9bd18627 100644 --- a/t/Koha/Plugin/Test.pm +++ b/t/Koha/Plugin/Test.pm @@ -78,6 +78,11 @@ sub install { return "Koha::Plugin::Test::install"; } +sub upgrade { + my ( $self, $args ) = @_; + return "Koha::Plugin::Test::install"; +} + sub uninstall { my ( $self, $args ) = @_; return "Koha::Plugin::Test::uninstall"; diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t index 1e76ed245f..f68c947b0d 100755 --- a/t/db_dependent/Plugins.t +++ b/t/db_dependent/Plugins.t @@ -49,6 +49,7 @@ ok( $plugin->can('opac_head'), 'Test plugin can opac_head' ); ok( $plugin->can('opac_js'), 'Test plugin can opac_js' ); ok( $plugin->can('configure'), 'Test plugin can configure' ); ok( $plugin->can('install'), 'Test plugin can install' ); +ok( $plugin->can('upgrade'), 'Test plugin can upgrade' ); ok( $plugin->can('uninstall'), 'Test plugin can install' ); is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' ); -- 2.15.1 (Apple Git-101)