View | Details | Raw Unified | Return to bug 14698
Collapse All | Expand All

(-)a/Koha/AtomicUpdater.pm (+23 lines)
Lines 260-265 sub applyAtomicUpdate { Link Here
260
            my $installer = C4::Installer->new();
260
            my $installer = C4::Installer->new();
261
            $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1;
261
            $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1;
262
        } elsif ( $filename =~ /\.(perl|pl)$/ ) {
262
        } elsif ( $filename =~ /\.(perl|pl)$/ ) {
263
            # Koha-community uses .perl files for developer atomic updates. These
264
            # simplified versions of atomicupdates do not instantiate $dbh, and
265
            # subroutines CheckVersion and SetVersion from updatedatabase.pl are
266
            # out of scope when executed by the AtomicUpdater. In order to support
267
            # community dev atomicupdates, we have to re-define these subroutines
268
            # and pass $dbh into the .perl file.
269
            #
270
            # See atomicupdates/skeleton.perl for default community template.
271
            if ($filename =~ /\.perl$/) {
272
                our $dbh = C4::Context->dbh;
273
                sub SetVersion   { return 1; }
274
                sub CheckVersion {
275
                    unless ($_[0] =~ /^XXX$/) {
276
                        Koha::Exceptions::File->throw(
277
                            error => 'Atomicupdate is not a dev atomicupdate. Dev'
278
                            .' updates are identified by CheckVersion("XXX").'
279
                            ." Given version is $_[0]."
280
                        );
281
                    }
282
                    return 1;
283
                }
284
            }
285
263
            my $fileAndPath = $self->{scriptDir}.'/'.$filename;
286
            my $fileAndPath = $self->{scriptDir}.'/'.$filename;
264
            $rv = do $fileAndPath;
287
            $rv = do $fileAndPath;
265
            unless ($rv) {
288
            unless ($rv) {
(-)a/t/db_dependent/Koha/AtomicUpdater.t (-3 / +69 lines)
Lines 30-36 use Koha::AtomicUpdater; Link Here
30
30
31
use t::lib::TestBuilder;
31
use t::lib::TestBuilder;
32
32
33
plan tests => 9;
33
plan tests => 10;
34
34
35
my $schema  = Koha::Database->new->schema;
35
my $schema  = Koha::Database->new->schema;
36
$schema->storage->txn_begin;
36
$schema->storage->txn_begin;
Lines 304-312 subtest 'Mark all atomicupdates as installed (for fresh installs), but do not ex Link Here
304
   $test_file2->remove;
304
   $test_file2->remove;
305
};
305
};
306
306
307
$test_file->remove;
308
$schema->storage->txn_rollback;
307
$schema->storage->txn_rollback;
309
308
309
subtest 'Support community dev atomicupdates (.perl files, see skeleton.perl)' => sub {
310
311
   plan tests => 4;
312
   $schema->storage->txn_begin;
313
314
   my $dev_update = q{
315
      $DBversion = 'XXX';  # will be replaced by the RM
316
      if( CheckVersion( $DBversion ) ) {
317
      $dbh->do( "SHOW TABLES" );
318
319
      # Always end with this (adjust the bug info)
320
      SetVersion( $DBversion );
321
      $ENV{ATOMICUPDATE_TESTS_VAL}++;
322
      print "Upgrade to $DBversion done (Bug XXXXX - description)\n";
323
   }
324
   };
325
326
   my $dev_update_invalid = q{
327
      $DBversion = '16.00.00';  # will be replaced by the RM
328
      if( CheckVersion( $DBversion ) ) {
329
      $dbh->do( "SHOW TABLES" );
330
331
      # Always end with this (adjust the bug info)
332
      SetVersion( $DBversion );
333
      $ENV{ATOMICUPDATE_TESTS_INV}++;
334
      print "Upgrade to $DBversion done (Bug XXXXX - description)\n";
335
   }
336
   };
337
338
   my $test_file1 = create_file({
339
      filepath => 'atomicupdate/',
340
      filename => 'Bug_00001-First-update.perl',
341
      content  => $dev_update,
342
   });
343
344
   my $test_file2 = create_file({
345
      filepath => 'atomicupdate/',
346
      filename => 'Bug_00002-Invalid-update.perl',
347
      content  => $dev_update_invalid,
348
   });
349
350
   my $atomicUpdater = Koha::AtomicUpdater->new({
351
      scriptDir => $test_file->dirname(),
352
   });
353
354
   my $atomicUpdater_invalid = Koha::AtomicUpdater->new({
355
      scriptDir => $test_file->dirname(),
356
   });
357
358
   $atomicUpdater->applyAtomicUpdate( $test_file1->stringify );
359
360
   $atomicUpdater_invalid->applyAtomicUpdate( $test_file2->stringify );
361
362
   my $atomicUpdate = $atomicUpdater->find('Bug-00001');
363
   my $atomicUpdate_invalid = $atomicUpdater->find('Bug-00002');
364
365
   is($atomicUpdate->filename, "Bug_00001-First-update.perl", "Bug_00001-First-update.perl added to DB");
366
   is($atomicUpdate_invalid->filename, "Bug_00002-Invalid-update.perl", "Bug_00002-Invalid-update.perl added to DB");
367
   is($ENV{ATOMICUPDATE_TESTS_VAL}, 1, "First update execution success.");
368
   is($ENV{ATOMICUPDATE_TESTS_INV}, undef, "Invalid update execution failed.");
369
370
   $test_file1->remove;
371
   $test_file2->remove;
372
   $schema->storage->txn_rollback;
373
};
374
375
$test_file->remove;
376
310
sub create_file {
377
sub create_file {
311
   my ($file) = @_;
378
   my ($file) = @_;
312
   my $tmpdir = File::Spec->tmpdir();
379
   my $tmpdir = File::Spec->tmpdir();
313
- 

Return to bug 14698