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

(-)a/Koha/AtomicUpdater.pm (-12 / +31 lines)
Lines 166-184 sub applyAtomicUpdates { Link Here
166
        my $atomicUpdate = $atomicUpdates->{$issueId};
166
        my $atomicUpdate = $atomicUpdates->{$issueId};
167
        next unless $atomicUpdate; #Not each ordered Git commit necessarily have a atomicupdate-script.
167
        next unless $atomicUpdate; #Not each ordered Git commit necessarily have a atomicupdate-script.
168
168
169
        my $filename = $atomicUpdate->filename;
169
        $self->applyAtomicUpdate($atomicUpdate);
170
        print "Applying file '$filename'\n" if $self->{verbose} > 2;
171
172
        if ( $filename =~ /\.sql$/ ) {
173
            my $installer = C4::Installer->new();
174
            my $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1;
175
        } elsif ( $filename =~ /\.(perl|pl)$/ ) {
176
            do $self->{scriptDir}.'/'.$filename;
177
        }
178
179
        $atomicUpdate->store();
180
        $appliedUpdates{$issueId} = $atomicUpdate;
170
        $appliedUpdates{$issueId} = $atomicUpdate;
181
        print "File '$filename' applied\n" if $self->{verbose} > 2;
182
    }
171
    }
183
172
184
    #Check that we have actually applied all the updates.
173
    #Check that we have actually applied all the updates.
Lines 191-196 sub applyAtomicUpdates { Link Here
191
    return \%appliedUpdates;
180
    return \%appliedUpdates;
192
}
181
}
193
182
183
sub applyAtomicUpdate {
184
    my ($self, $atomicUpdate) = @_;
185
    #Validate params
186
    unless ($atomicUpdate) {
187
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->applyAtomicUpdate($atomicUpdate):> Parameter must be a Koha::AtomicUpdate-object or a path to a valid atomicupdates-script!");
188
    }
189
    if ($atomicUpdate && ref($atomicUpdate) eq '') { #We have a scalar, presumably a filepath to atomicUpdate-script.
190
        $atomicUpdate = Koha::AtomicUpdate->new({filename => $atomicUpdate});
191
    }
192
    $atomicUpdate = Koha::AtomicUpdater->cast($atomicUpdate);
193
194
    my $filename = $atomicUpdate->filename;
195
    print "Applying file '$filename'\n" if $self->{verbose} > 2;
196
197
    if ( $filename =~ /\.sql$/ ) {
198
        my $installer = C4::Installer->new();
199
        my $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1;
200
    } elsif ( $filename =~ /\.(perl|pl)$/ ) {
201
        my $fileAndPath = $self->{scriptDir}.'/'.$filename;
202
        unless (my $return = do $fileAndPath ) {
203
            warn "couldn't parse $fileAndPath: $@\n" if $@;
204
            warn "couldn't do $fileAndPath: $!\n"    unless defined $return;
205
            warn "couldn't run $fileAndPath\n"       unless $return;
206
        }
207
    }
208
209
    $atomicUpdate->store();
210
    print "File '$filename' applied\n" if $self->{verbose} > 2;
211
}
212
194
=head _getValidAtomicUpdateScripts
213
=head _getValidAtomicUpdateScripts
195
214
196
@RETURNS HASHRef of Koha::AtomicUpdate-objects, of all the files
215
@RETURNS HASHRef of Koha::AtomicUpdate-objects, of all the files
(-)a/installer/data/mysql/atomicupdate.pl (-1 / +15 lines)
Lines 34-39 my $list = 0; Link Here
34
my $pending = 0;
34
my $pending = 0;
35
my $directory = '';
35
my $directory = '';
36
my $git = '';
36
my $git = '';
37
my $single = '';
37
38
38
GetOptions(
39
GetOptions(
39
    'v|verbose:i'       => \$verbose,
40
    'v|verbose:i'       => \$verbose,
Lines 45-50 GetOptions( Link Here
45
    'l|list'            => \$list,
46
    'l|list'            => \$list,
46
    'p|pending'         => \$pending,
47
    'p|pending'         => \$pending,
47
    'g|git:s'           => \$git,
48
    'g|git:s'           => \$git,
49
    's|single:s'        => \$single,
48
);
50
);
49
51
50
my $usage = << 'ENDUSAGE';
52
my $usage = << 'ENDUSAGE';
Lines 58-78 applied. Link Here
58
Also acts as a gateway to CRUD the koha.database_updates-table.
60
Also acts as a gateway to CRUD the koha.database_updates-table.
59
61
60
    -v --verbose        Integer, 1 is not so verbose, 3 is maximally verbose.
62
    -v --verbose        Integer, 1 is not so verbose, 3 is maximally verbose.
63
61
    -h --help           Flag, This nice help!
64
    -h --help           Flag, This nice help!
65
62
    -a --apply          Flag, Apply all the pending atomicupdates from the
66
    -a --apply          Flag, Apply all the pending atomicupdates from the
63
                        atomicupdates-directory.
67
                        atomicupdates-directory.
68
64
    -d --directory      Path, From which directory to look for atomicupdate-scripts.
69
    -d --directory      Path, From which directory to look for atomicupdate-scripts.
65
                        Defaults to '$KOHA_PATH/installer/data/mysql/atomicupdate/'
70
                        Defaults to '$KOHA_PATH/installer/data/mysql/atomicupdate/'
71
72
    -s --single         Path, execute a single atomicupdate-script.
73
                        eg. atomicupdate/Bug01243-SingleFeature.pl
74
66
    -r --remove         String, Remove the upgrade entry from koha.database_updates
75
    -r --remove         String, Remove the upgrade entry from koha.database_updates
67
                        eg. --remove "Bug71337"
76
                        eg. --remove "Bug71337"
77
68
    -i --insert         Path, Add an upgrade log entry for the given atomicupdate-file.
78
    -i --insert         Path, Add an upgrade log entry for the given atomicupdate-file.
69
                        Useful to revert an accidental --remove -operation or for
79
                        Useful to revert an accidental --remove -operation or for
70
                        testing.
80
                        testing. Does not execute the update script, simply adds
81
                        the log entry.
71
                        eg. -i installer/data/mysql/atomicupdate/Bug5453-Example.pl
82
                        eg. -i installer/data/mysql/atomicupdate/Bug5453-Example.pl
83
72
    -l --list           Flag, List all entries in the koha.database_updates-table.
84
    -l --list           Flag, List all entries in the koha.database_updates-table.
73
                        This typically means all applied atomicupdates.
85
                        This typically means all applied atomicupdates.
86
74
    -p --pending        Flag, List all pending atomicupdates from the
87
    -p --pending        Flag, List all pending atomicupdates from the
75
                        atomicupdates-directory.
88
                        atomicupdates-directory.
89
76
    -g --git            Path, Build the update order from the Git repository given,
90
    -g --git            Path, Build the update order from the Git repository given,
77
                        or default to the Git repository in $KOHA_PATH.
91
                        or default to the Git repository in $KOHA_PATH.
78
                        Eg. --git 1, to build with default values, or
92
                        Eg. --git 1, to build with default values, or
(-)a/t/db_dependent/Koha/AtomicUpdater.t (-1 / +33 lines)
Lines 270-274 sub listPendingAtomicupdates { Link Here
270
    t::lib::TestObjects::AtomicUpdateFactory->tearDownTestContext($subtestContext);
270
    t::lib::TestObjects::AtomicUpdateFactory->tearDownTestContext($subtestContext);
271
}
271
}
272
272
273
subtest "Apply single atomicupdate from file" => \&applySingleAtomicUpdateFromFile;
274
sub applySingleAtomicUpdateFromFile {
275
    my $subtestContext = {};
276
    eval {
277
    my $files = t::lib::TestObjects::FileFactory->createTestGroup([
278
                        {   filepath => 'atomicupdate/',
279
                            filename => 'Bug_01243-SingleUpdate.pl',
280
                            content  => '$ENV{ATOMICUPDATE_TESTS_2} = 10;',},
281
                        ],
282
                        undef, $subtestContext, $testContext);
283
    my $atomicUpdater = Koha::AtomicUpdater->new({
284
                                                  scriptDir => $files->{'Bug_01243-SingleUpdate.pl'}->dirname()
285
                                                });
286
287
    $atomicUpdater->applyAtomicUpdate($files->{'Bug_01243-SingleUpdate.pl'}->stringify);
288
289
    my $atomicUpdate = $atomicUpdater->find({issue_id => 'Bug01243'});
290
    t::lib::TestObjects::AtomicUpdateFactory->addToContext($atomicUpdate, undef, $subtestContext, $testContext); #Keep track of changes
291
292
    is($atomicUpdate->filename,
293
       "Bug_01243-SingleUpdate.pl",
294
       "Bug_01243-SingleUpdate.pl added to DB");
295
    is($ENV{ATOMICUPDATE_TESTS_2},
296
       10,
297
       "Bug_01243-SingleUpdate.pl executed");
298
299
    };
300
    if ($@) {
301
        ok(0, $@);
302
    }
303
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($subtestContext);
304
}
305
273
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
306
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
274
done_testing;
307
done_testing;
275
- 

Return to bug 14698