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

(-)a/Koha/AtomicUpdater.pm (-12 / +34 lines)
Lines 58-63 sub new { Link Here
58
    $self->{verbose} = $params->{verbose} || $self->{verbose} || 0;
58
    $self->{verbose} = $params->{verbose} || $self->{verbose} || 0;
59
    $self->{scriptDir} = $params->{scriptDir} || $self->{scriptDir} || C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/';
59
    $self->{scriptDir} = $params->{scriptDir} || $self->{scriptDir} || C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/';
60
    $self->{gitRepo} = $params->{gitRepo} || $self->{gitRepo} || $ENV{KOHA_PATH};
60
    $self->{gitRepo} = $params->{gitRepo} || $self->{gitRepo} || $ENV{KOHA_PATH};
61
    $self->{dryRun} = $params->{dryRun} || $self->{dryRun} || 0;
61
62
62
    return $self;
63
    return $self;
63
}
64
}
Lines 166-184 sub applyAtomicUpdates { Link Here
166
        my $atomicUpdate = $atomicUpdates->{$issueId};
167
        my $atomicUpdate = $atomicUpdates->{$issueId};
167
        next unless $atomicUpdate; #Not each ordered Git commit necessarily have a atomicupdate-script.
168
        next unless $atomicUpdate; #Not each ordered Git commit necessarily have a atomicupdate-script.
168
169
169
        my $filename = $atomicUpdate->filename;
170
        $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;
171
        $appliedUpdates{$issueId} = $atomicUpdate;
181
        print "File '$filename' applied\n" if $self->{verbose} > 2;
182
    }
172
    }
183
173
184
    #Check that we have actually applied all the updates.
174
    #Check that we have actually applied all the updates.
Lines 191-196 sub applyAtomicUpdates { Link Here
191
    return \%appliedUpdates;
181
    return \%appliedUpdates;
192
}
182
}
193
183
184
sub applyAtomicUpdate {
185
    my ($self, $atomicUpdate) = @_;
186
    #Validate params
187
    unless ($atomicUpdate) {
188
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->applyAtomicUpdate($atomicUpdate):> Parameter must be a Koha::AtomicUpdate-object or a path to a valid atomicupdates-script!");
189
    }
190
    if ($atomicUpdate && ref($atomicUpdate) eq '') { #We have a scalar, presumably a filepath to atomicUpdate-script.
191
        $atomicUpdate = Koha::AtomicUpdate->new({filename => $atomicUpdate});
192
    }
193
    $atomicUpdate = Koha::AtomicUpdater->cast($atomicUpdate);
194
195
    my $filename = $atomicUpdate->filename;
196
    print "Applying file '$filename'\n" if $self->{verbose} > 2;
197
198
    unless ($self->{dryRun}) {
199
        if ( $filename =~ /\.sql$/ ) {
200
            my $installer = C4::Installer->new();
201
            my $rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1;
202
        } elsif ( $filename =~ /\.(perl|pl)$/ ) {
203
            my $fileAndPath = $self->{scriptDir}.'/'.$filename;
204
            unless (my $return = do $fileAndPath ) {
205
                warn "couldn't parse $fileAndPath: $@\n" if $@;
206
                warn "couldn't do $fileAndPath: $!\n"    unless defined $return;
207
                warn "couldn't run $fileAndPath\n"       unless $return;
208
            }
209
        }
210
        $atomicUpdate->store();
211
    }
212
213
    print "File '$filename' applied\n" if $self->{verbose} > 2;
214
}
215
194
=head _getValidAtomicUpdateScripts
216
=head _getValidAtomicUpdateScripts
195
217
196
@RETURNS HASHRef of Koha::AtomicUpdate-objects, of all the files
218
@RETURNS HASHRef of Koha::AtomicUpdate-objects, of all the files
(-)a/installer/data/mysql/atomicupdate.pl (-3 / +24 lines)
Lines 19-25 Link Here
19
#
19
#
20
20
21
use Modern::Perl;
21
use Modern::Perl;
22
use Getopt::Long;
22
use Getopt::Long qw(:config no_ignore_case);
23
23
24
use C4::Context;
24
use C4::Context;
25
25
Lines 29-50 my $verbose = 0; Link Here
29
my $help = 0;
29
my $help = 0;
30
my $apply = 0;
30
my $apply = 0;
31
my $remove = '';
31
my $remove = '';
32
my $dryRun = 0;
32
my $insert = '';
33
my $insert = '';
33
my $list = 0;
34
my $list = 0;
34
my $pending = 0;
35
my $pending = 0;
35
my $directory = '';
36
my $directory = '';
36
my $git = '';
37
my $git = '';
38
my $single = '';
37
39
38
GetOptions(
40
GetOptions(
39
    'v|verbose:i'       => \$verbose,
41
    'v|verbose:i'       => \$verbose,
40
    'h|help'            => \$help,
42
    'h|help'            => \$help,
41
    'a|apply'           => \$apply,
43
    'a|apply'           => \$apply,
44
    'D|dry-run'         => \$dryRun,
42
    'd|directory:s'     => \$directory,
45
    'd|directory:s'     => \$directory,
43
    'r|remove:s'        => \$remove,
46
    'r|remove:s'        => \$remove,
44
    'i|insert:s'        => \$insert,
47
    'i|insert:s'        => \$insert,
45
    'l|list'            => \$list,
48
    'l|list'            => \$list,
46
    'p|pending'         => \$pending,
49
    'p|pending'         => \$pending,
47
    'g|git:s'           => \$git,
50
    'g|git:s'           => \$git,
51
    's|single:s'        => \$single,
48
);
52
);
49
53
50
my $usage = << 'ENDUSAGE';
54
my $usage = << 'ENDUSAGE';
Lines 58-78 applied. Link Here
58
Also acts as a gateway to CRUD the koha.database_updates-table.
62
Also acts as a gateway to CRUD the koha.database_updates-table.
59
63
60
    -v --verbose        Integer, 1 is not so verbose, 3 is maximally verbose.
64
    -v --verbose        Integer, 1 is not so verbose, 3 is maximally verbose.
65
66
    -D --dry-run        Flag, Run the script but don't execute any atomicupdates.
67
                        You should use --verbose 3 to see what is happening.
68
61
    -h --help           Flag, This nice help!
69
    -h --help           Flag, This nice help!
70
62
    -a --apply          Flag, Apply all the pending atomicupdates from the
71
    -a --apply          Flag, Apply all the pending atomicupdates from the
63
                        atomicupdates-directory.
72
                        atomicupdates-directory.
73
64
    -d --directory      Path, From which directory to look for atomicupdate-scripts.
74
    -d --directory      Path, From which directory to look for atomicupdate-scripts.
65
                        Defaults to '$KOHA_PATH/installer/data/mysql/atomicupdate/'
75
                        Defaults to '$KOHA_PATH/installer/data/mysql/atomicupdate/'
76
77
    -s --single         Path, execute a single atomicupdate-script.
78
                        eg. atomicupdate/Bug01243-SingleFeature.pl
79
66
    -r --remove         String, Remove the upgrade entry from koha.database_updates
80
    -r --remove         String, Remove the upgrade entry from koha.database_updates
67
                        eg. --remove "Bug71337"
81
                        eg. --remove "Bug71337"
82
68
    -i --insert         Path, Add an upgrade log entry for the given atomicupdate-file.
83
    -i --insert         Path, Add an upgrade log entry for the given atomicupdate-file.
69
                        Useful to revert an accidental --remove -operation or for
84
                        Useful to revert an accidental --remove -operation or for
70
                        testing.
85
                        testing. Does not execute the update script, simply adds
86
                        the log entry.
71
                        eg. -i installer/data/mysql/atomicupdate/Bug5453-Example.pl
87
                        eg. -i installer/data/mysql/atomicupdate/Bug5453-Example.pl
88
72
    -l --list           Flag, List all entries in the koha.database_updates-table.
89
    -l --list           Flag, List all entries in the koha.database_updates-table.
73
                        This typically means all applied atomicupdates.
90
                        This typically means all applied atomicupdates.
91
74
    -p --pending        Flag, List all pending atomicupdates from the
92
    -p --pending        Flag, List all pending atomicupdates from the
75
                        atomicupdates-directory.
93
                        atomicupdates-directory.
94
76
    -g --git            Path, Build the update order from the Git repository given,
95
    -g --git            Path, Build the update order from the Git repository given,
77
                        or default to the Git repository in $KOHA_PATH.
96
                        or default to the Git repository in $KOHA_PATH.
78
                        Eg. --git 1, to build with default values, or
97
                        Eg. --git 1, to build with default values, or
Lines 126-132 if ( $help ) { Link Here
126
145
127
my $atomicupdater = Koha::AtomicUpdater->new({verbose => $verbose,
146
my $atomicupdater = Koha::AtomicUpdater->new({verbose => $verbose,
128
                                              scriptDir => $directory,
147
                                              scriptDir => $directory,
129
                                              gitRepo => (length($git) == 1) ? '' : $git});
148
                                              gitRepo => (length($git) == 1) ? '' : $git,
149
                                              dryRun => $dryRun,}
150
                                            ,);
130
151
131
if ($git) {
152
if ($git) {
132
    $atomicupdater->buildUpdateOrderFromGit(10000);
153
    $atomicupdater->buildUpdateOrderFromGit(10000);
(-)a/t/db_dependent/Koha/AtomicUpdater.t (-1 / +49 lines)
Lines 45-50 my $atomicupdates = t::lib::TestObjects::AtomicUpdateFactory->createTestGroup([ Link Here
45
    package Koha::AtomicUpdater;
45
    package Koha::AtomicUpdater;
46
    sub _getGitCommits { #instead of requiring a Git repository, we just mock the input.
46
    sub _getGitCommits { #instead of requiring a Git repository, we just mock the input.
47
        return [#Newest commit
47
        return [#Newest commit
48
                '2e8a39762b506738195f21c8ff67e4e7bfe6dbba Bug_01243-SingleUpdate',
48
                '2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #:-55 : Fiftyfive',
49
                '2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #:-55 : Fiftyfive',
49
                '2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #54 - KohaCon in Finland next year',
50
                '2e8a39762b506738195f21c8ff67e4e7bfe6d7ab #54 - KohaCon in Finland next year',
50
                'b447b595acacb0c4823582acf9d8a08902118e59 #53 - Place to be.pl',
51
                'b447b595acacb0c4823582acf9d8a08902118e59 #53 - Place to be.pl',
Lines 270-274 sub listPendingAtomicupdates { Link Here
270
    t::lib::TestObjects::AtomicUpdateFactory->tearDownTestContext($subtestContext);
271
    t::lib::TestObjects::AtomicUpdateFactory->tearDownTestContext($subtestContext);
271
}
272
}
272
273
274
subtest "Apply single atomicupdate from file" => \&applySingleAtomicUpdateFromFile;
275
sub applySingleAtomicUpdateFromFile {
276
    my $subtestContext = {};
277
    eval {
278
    my $files = t::lib::TestObjects::FileFactory->createTestGroup([
279
                        {   filepath => 'atomicupdate/',
280
                            filename => 'Bug_01243-SingleUpdate.pl',
281
                            content  => '$ENV{ATOMICUPDATE_TESTS_2} = 10;',},
282
                        ],
283
                        undef, $subtestContext, $testContext);
284
    ###  Try first as a dry-run  ###
285
    my $atomicUpdater = Koha::AtomicUpdater->new({
286
                                                  scriptDir => $files->{'Bug_01243-SingleUpdate.pl'}->dirname(),
287
                                                  dryRun => 1,
288
                                                });
289
290
    $atomicUpdater->applyAtomicUpdate($files->{'Bug_01243-SingleUpdate.pl'}->stringify);
291
    my $atomicUpdate = $atomicUpdater->find({issue_id => 'Bug01243'});
292
293
    ok(not($atomicUpdate),
294
       "--dry-run doesn't add anything");
295
    is($ENV{ATOMICUPDATE_TESTS_2},
296
       undef,
297
       "--dry-run doesn't execute anything");
298
299
    ###  Make a change!  ###
300
    $atomicUpdater = Koha::AtomicUpdater->new({
301
                                                  scriptDir => $files->{'Bug_01243-SingleUpdate.pl'}->dirname(),
302
                                                });
303
304
    $atomicUpdater->applyAtomicUpdate($files->{'Bug_01243-SingleUpdate.pl'}->stringify);
305
    $atomicUpdate = $atomicUpdater->find({issue_id => 'Bug01243'});
306
    t::lib::TestObjects::AtomicUpdateFactory->addToContext($atomicUpdate, undef, $subtestContext, $testContext); #Keep track of changes
307
308
    is($atomicUpdate->filename,
309
       "Bug_01243-SingleUpdate.pl",
310
       "Bug_01243-SingleUpdate.pl added to DB");
311
    is($ENV{ATOMICUPDATE_TESTS_2},
312
       10,
313
       "Bug_01243-SingleUpdate.pl executed");
314
315
    };
316
    if ($@) {
317
        ok(0, $@);
318
    }
319
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($subtestContext);
320
}
321
273
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
322
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
274
done_testing;
323
done_testing;
275
- 

Return to bug 14698