Bug 7167: New version for updatedatabase
[koha.git] / misc / bin / updatedb.pl
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         # We execute ALL versions where version number >= min_version
77         # OR version is not a number
78         if ( not grep { $v eq $_->{version} } @$versions
79             and ( not $v =~ /\d\.\d{2}\.\d{2}\.\d{3}/ or
80                 C4::Update::Database::TransformToNum($v) >= $min_version ) )
81         {
82             my $report = C4::Update::Database::execute_version $v;
83             push @reports, $report;
84         }
85     }
86 }
87
88 if ( $version or $all ) {
89     say @reports ? "Report:" : "Nothing to report";
90     for my $report (@reports) {
91         my ( $v, $r ) = each %$report;
92         if ( ref($r) eq 'HASH' ) {
93             say "\t$v => $r->{error}";
94         }
95         elsif ( ref($r) eq 'ARRAY' ) {
96             say "\t$_" for @$r;
97         }
98         else {
99             say "\t$v => $r";
100         }
101     }
102 }
103
104 sub usage {
105     say "update.pl";
106     say "This script updates your database for you";
107     say "Usage:";
108     say "\t-h\tShow this help message";
109     say "\t-m\tExecute a given version";
110     say "\t-l\tList all the versions";
111     say "\t-all\tExecute all available versions";
112     say
113       "\t-min\tWith -all, Execute all available versions since a given version";
114     say "\t\tCan be X.XX.XX.XXX or X.XXXXXXX";
115 }