Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright Biblibre 2012 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use C4::Context; |
23 |
use C4::Update::Database; |
24 |
use Getopt::Long; |
25 |
|
26 |
my $help; |
27 |
my $version; |
28 |
my $list; |
29 |
my $all; |
30 |
my $min; |
31 |
|
32 |
GetOptions( |
33 |
'h|help|?' => \$help, |
34 |
'm:s' => \$version, |
35 |
'l|list' => \$list, |
36 |
'a|all' => \$all, |
37 |
'min:s' => \$min, |
38 |
); |
39 |
|
40 |
if ( $help or not( $version or $list or $all ) ) { |
41 |
usage(); |
42 |
exit; |
43 |
} |
44 |
|
45 |
my @reports; |
46 |
if ($version) { |
47 |
my $report = C4::Update::Database::execute_version($version); |
48 |
push @reports, $report; |
49 |
} |
50 |
|
51 |
if ($list) { |
52 |
my $available = C4::Update::Database::list_versions_available(); |
53 |
my $already_applied = C4::Update::Database::list_versions_already_applied(); |
54 |
say "Versions available:"; |
55 |
for my $v (@$available) { |
56 |
if ( not grep { $v eq $_->{version} } @$already_applied ) { |
57 |
say "\t- $_" for $v; |
58 |
} |
59 |
} |
60 |
say "Versions already applied:"; |
61 |
say "\t- $_->{version}" for @$already_applied; |
62 |
|
63 |
} |
64 |
|
65 |
if ($all) { |
66 |
my $versions_available = C4::Update::Database::list_versions_available(); |
67 |
my $versions = C4::Update::Database::list_versions_already_applied; |
68 |
my $min_version = |
69 |
$min |
70 |
? $min =~ m/\d\.\d{2}\.\d{2}\.\d{3}/ |
71 |
? C4::Update::Database::TransformToNum($min) |
72 |
: $min |
73 |
: 0; |
74 |
|
75 |
for my $v (@$versions_available) { |
76 |
if ( not grep { $v eq $_->{version} } @$versions |
77 |
and C4::Update::Database::TransformToNum($v) >= $min_version ) |
78 |
{ |
79 |
my $report = C4::Update::Database::execute_version $v; |
80 |
push @reports, $report; |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
if ( $version or $all ) { |
86 |
say @reports ? "Report:" : "Nothing to report"; |
87 |
for my $report (@reports) { |
88 |
my ( $v, $r ) = each %$report; |
89 |
if ( ref($r) eq 'HASH' ) { |
90 |
say "\t$v => $r->{error}"; |
91 |
} |
92 |
elsif ( ref($r) eq 'ARRAY' ) { |
93 |
say "\t$_" for @$r; |
94 |
} |
95 |
else { |
96 |
say "\t$v => $r"; |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
sub usage { |
102 |
say "update.pl"; |
103 |
say "This script updates your database for you"; |
104 |
say "Usage:"; |
105 |
say "\t-h\tShow this help message"; |
106 |
say "\t-m\tExecute a given version"; |
107 |
say "\t-l\tList all the versions"; |
108 |
say "\t-all\tExecute all available versions"; |
109 |
say |
110 |
"\t-min\tWith -all, Execute all available versions since a given version"; |
111 |
say "\t\tCan be X.XX.XX.XXX or X.XXXXXXX"; |
112 |
} |