@@ -, +, @@ defined by skeleton.perl --- Koha/AtomicUpdater.pm | 23 ++++++++++ t/db_dependent/Koha/AtomicUpdater.t | 71 ++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) --- a/Koha/AtomicUpdater.pm +++ a/Koha/AtomicUpdater.pm @@ -260,6 +260,29 @@ sub applyAtomicUpdate { my $installer = C4::Installer->new(); $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1; } elsif ( $filename =~ /\.(perl|pl)$/ ) { + # Koha-community uses .perl files for developer atomic updates. These + # simplified versions of atomicupdates do not instantiate $dbh, and + # subroutines CheckVersion and SetVersion from updatedatabase.pl are + # out of scope when executed by the AtomicUpdater. In order to support + # community dev atomicupdates, we have to re-define these subroutines + # and pass $dbh into the .perl file. + # + # See atomicupdates/skeleton.perl for default community template. + if ($filename =~ /\.perl$/) { + our $dbh = C4::Context->dbh; + sub SetVersion { return 1; } + sub CheckVersion { + unless ($_[0] =~ /^XXX$/) { + Koha::Exceptions::File->throw( + error => 'Atomicupdate is not a dev atomicupdate. Dev' + .' updates are identified by CheckVersion("XXX").' + ." Given version is $_[0]." + ); + } + return 1; + } + } + my $fileAndPath = $self->{scriptDir}.'/'.$filename; $rv = do $fileAndPath; unless ($rv) { --- a/t/db_dependent/Koha/AtomicUpdater.t +++ a/t/db_dependent/Koha/AtomicUpdater.t @@ -30,7 +30,7 @@ use Koha::AtomicUpdater; use t::lib::TestBuilder; -plan tests => 9; +plan tests => 10; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -304,9 +304,76 @@ subtest 'Mark all atomicupdates as installed (for fresh installs), but do not ex $test_file2->remove; }; -$test_file->remove; $schema->storage->txn_rollback; +subtest 'Support community dev atomicupdates (.perl files, see skeleton.perl)' => sub { + + plan tests => 4; + $schema->storage->txn_begin; + + my $dev_update = q{ + $DBversion = 'XXX'; # will be replaced by the RM + if( CheckVersion( $DBversion ) ) { + $dbh->do( "SHOW TABLES" ); + + # Always end with this (adjust the bug info) + SetVersion( $DBversion ); + $ENV{ATOMICUPDATE_TESTS_VAL}++; + print "Upgrade to $DBversion done (Bug XXXXX - description)\n"; + } + }; + + my $dev_update_invalid = q{ + $DBversion = '16.00.00'; # will be replaced by the RM + if( CheckVersion( $DBversion ) ) { + $dbh->do( "SHOW TABLES" ); + + # Always end with this (adjust the bug info) + SetVersion( $DBversion ); + $ENV{ATOMICUPDATE_TESTS_INV}++; + print "Upgrade to $DBversion done (Bug XXXXX - description)\n"; + } + }; + + my $test_file1 = create_file({ + filepath => 'atomicupdate/', + filename => 'Bug_00001-First-update.perl', + content => $dev_update, + }); + + my $test_file2 = create_file({ + filepath => 'atomicupdate/', + filename => 'Bug_00002-Invalid-update.perl', + content => $dev_update_invalid, + }); + + my $atomicUpdater = Koha::AtomicUpdater->new({ + scriptDir => $test_file->dirname(), + }); + + my $atomicUpdater_invalid = Koha::AtomicUpdater->new({ + scriptDir => $test_file->dirname(), + }); + + $atomicUpdater->applyAtomicUpdate( $test_file1->stringify ); + + $atomicUpdater_invalid->applyAtomicUpdate( $test_file2->stringify ); + + my $atomicUpdate = $atomicUpdater->find('Bug-00001'); + my $atomicUpdate_invalid = $atomicUpdater->find('Bug-00002'); + + is($atomicUpdate->filename, "Bug_00001-First-update.perl", "Bug_00001-First-update.perl added to DB"); + is($atomicUpdate_invalid->filename, "Bug_00002-Invalid-update.perl", "Bug_00002-Invalid-update.perl added to DB"); + is($ENV{ATOMICUPDATE_TESTS_VAL}, 1, "First update execution success."); + is($ENV{ATOMICUPDATE_TESTS_INV}, undef, "Invalid update execution failed."); + + $test_file1->remove; + $test_file2->remove; + $schema->storage->txn_rollback; +}; + +$test_file->remove; + sub create_file { my ($file) = @_; my $tmpdir = File::Spec->tmpdir(); --