Line 0
Link Here
|
|
|
1 |
package C4::Update::Database; |
2 |
|
3 |
# Copyright 2011 BibLibre |
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 |
|
24 |
use File::Basename; |
25 |
use File::Find::Rule; |
26 |
use Digest::MD5; |
27 |
use List::MoreUtils qw/uniq/; |
28 |
use YAML; |
29 |
|
30 |
=head1 NAME |
31 |
|
32 |
C4::Update::Database.pm |
33 |
|
34 |
=head1 SYNOPSIS |
35 |
|
36 |
use C4::Update::Database; |
37 |
|
38 |
This package is used by admin/updatedatabase.pl, to manage DB updates |
39 |
|
40 |
=head1 FUNCTIONS |
41 |
|
42 |
=cut |
43 |
|
44 |
my $VERSIONS_PATH = C4::Context->config('intranetdir') . '/installer/data/mysql/versions'; |
45 |
|
46 |
my $version; |
47 |
my $list; |
48 |
|
49 |
my $dbh = C4::Context->dbh; |
50 |
|
51 |
=head2 |
52 |
|
53 |
my $file = get_filepath($version); |
54 |
this sub will return the full path of a given DB update number |
55 |
|
56 |
=cut |
57 |
|
58 |
sub get_filepath { |
59 |
my ( $version ) = @_; |
60 |
my @files = File::Find::Rule->file->name( "$version.sql", "$version.pl" ) ->in( ( $VERSIONS_PATH ) ); |
61 |
|
62 |
if ( scalar @files != 1 ) { |
63 |
die "This version ($version) returned has ".scalar @files." corresponding, need only 1"; |
64 |
} |
65 |
|
66 |
return $files[0]; |
67 |
} |
68 |
|
69 |
=head2 get_md5 |
70 |
|
71 |
my $md5 = get_md5($filepath) |
72 |
returns the md5sum of the selected file. |
73 |
This is used to check consistency of updates |
74 |
|
75 |
=cut |
76 |
sub get_md5 { |
77 |
my ( $filepath ) = @_; |
78 |
open(FILE, $filepath); |
79 |
|
80 |
my $ctx = Digest::MD5->new; |
81 |
$ctx->addfile(*FILE); |
82 |
my $md5 = $ctx->hexdigest; |
83 |
close(FILE); |
84 |
return $md5; |
85 |
} |
86 |
|
87 |
=head2 execute_version |
88 |
|
89 |
$result = execute_version($version_number); |
90 |
Execute an update. |
91 |
This sub will detect if the number is made through a .pl or a .sql, and behave accordingly |
92 |
if there is more than 1 file with the same number, an error will be issued |
93 |
if you try to execute a version_number that has already be executed, then it will also issue an error |
94 |
the sub return an result hash, with the version number and the result |
95 |
|
96 |
=cut |
97 |
|
98 |
sub execute_version { |
99 |
my ( $version ) = @_; |
100 |
my $report; |
101 |
|
102 |
my $filepath; |
103 |
eval { |
104 |
$filepath = get_filepath $version; |
105 |
}; |
106 |
if ( $@ ) { |
107 |
return { $version => $@ }; |
108 |
} |
109 |
|
110 |
my @file_infos = fileparse( $filepath, qr/\.[^.]*/ ); |
111 |
my $extension = $file_infos[2]; |
112 |
my $filename = $version . $extension; |
113 |
|
114 |
my $md5 = get_md5 $filepath; |
115 |
my $r = md5_already_exists( $md5 ); |
116 |
if ( scalar @$r ) { |
117 |
my $p = @$r[0]; |
118 |
$report->{$version} = { |
119 |
error => "ALREADY_EXISTS", |
120 |
filepath => $filepath, |
121 |
old_version => @$r[0]->{version}, |
122 |
md5 => @$r[0]->{md5}, |
123 |
}; |
124 |
return $report; |
125 |
} |
126 |
|
127 |
my $queries; |
128 |
given ( $extension ) { |
129 |
when ( /.sql/ ) { |
130 |
$queries = get_queries ( $filepath ); |
131 |
} |
132 |
when ( /.pl/ ) { |
133 |
unless(do $filepath) { |
134 |
$report->{$version} = { |
135 |
error => "PERL_DBREV_FAILED", |
136 |
filename => $filename, |
137 |
}; |
138 |
} |
139 |
} |
140 |
default { |
141 |
$report->{$version} = { |
142 |
error => "BAD_EXTENSION", |
143 |
extension => $extension, |
144 |
}; |
145 |
} |
146 |
} |
147 |
|
148 |
return $report |
149 |
if ( defined $report->{$version} ); |
150 |
|
151 |
my $errors; |
152 |
for my $query ( @{$queries->{queries}} ) { |
153 |
eval { |
154 |
check_coherency( $query ); |
155 |
}; |
156 |
if ( $@ ) { |
157 |
push @$errors, $@ |
158 |
} |
159 |
} |
160 |
|
161 |
if ( $errors ) { |
162 |
$_ =~ s/at [^ ]* line \d*\.$// for @$errors; |
163 |
$report->{$version} = $errors; |
164 |
return $report; |
165 |
} |
166 |
|
167 |
$errors = execute ( $queries ) if $queries; |
168 |
$report->{$version} = scalar( @$errors ) ? $errors : "OK"; |
169 |
set_infos ( $version, $queries, $errors, $md5 ); |
170 |
return $report; |
171 |
} |
172 |
|
173 |
=head2 list_versions_availables |
174 |
|
175 |
my @versions = list_versions_availables; |
176 |
return an array with all version available |
177 |
This list is retrieved from the directory defined in the etc/update/database/config.yaml, versions_dir parameter |
178 |
|
179 |
=cut |
180 |
|
181 |
sub list_versions_availables { |
182 |
my @versions; |
183 |
|
184 |
my @files = File::Find::Rule->file->name( "*.sql", "*.pl" ) ->in( ( $VERSIONS_PATH ) ); |
185 |
|
186 |
for my $f ( @files ) { |
187 |
my @file_infos = fileparse( $f, qr/\.[^.]*/ ); |
188 |
push @versions, $file_infos[0]; |
189 |
} |
190 |
@versions = uniq @versions; |
191 |
return \@versions; |
192 |
} |
193 |
|
194 |
=head2 list_versions_already_knows |
195 |
|
196 |
my @versions = list_versions_availables; |
197 |
return an array with all version that have already been applied |
198 |
This sub check first that the updatedb tables exist and create them if needed |
199 |
|
200 |
=cut |
201 |
|
202 |
sub list_versions_already_knows { |
203 |
# 1st check if tables exist, otherwise create them |
204 |
$dbh->do(qq{ |
205 |
CREATE TABLE IF NOT EXISTS `updatedb_error` ( `version` varchar(32) DEFAULT NULL, `error` text ) ENGINE=InnoDB CHARSET=utf8; |
206 |
}); |
207 |
$dbh->do(qq{ |
208 |
CREATE TABLE IF NOT EXISTS `updatedb_query` ( `version` varchar(32) DEFAULT NULL, `query` text ) ENGINE=InnoDB CHARSET=utf8; |
209 |
}); |
210 |
$dbh->do(qq{ |
211 |
CREATE TABLE IF NOT EXISTS `updatedb_report` ( `version` text, `md5` varchar(50) DEFAULT NULL, `comment` text, `status` int(1) DEFAULT NULL ) ENGINE=InnoDB CHARSET=utf8; |
212 |
}); |
213 |
|
214 |
my $query = qq/ SELECT version, comment, status FROM updatedb_report ORDER BY version/; |
215 |
my $sth = $dbh->prepare( $query ); |
216 |
$sth->execute; |
217 |
my $versions = $sth->fetchall_arrayref( {} ); |
218 |
map { |
219 |
my $version = $_; |
220 |
my @comments = defined $_->{comment} ? split '\\\n', $_->{comment} : ""; |
221 |
push @{ $version->{comments} }, { comment => $_ } for @comments; |
222 |
delete $version->{comment}; |
223 |
} @$versions; |
224 |
$sth->finish; |
225 |
for my $version ( @$versions ) { |
226 |
$query = qq/ SELECT query FROM updatedb_query WHERE version = ? ORDER BY version/; |
227 |
$sth = $dbh->prepare( $query ); |
228 |
$sth->execute( $version->{version} ); |
229 |
$version->{queries} = $sth->fetchall_arrayref( {} ); |
230 |
$sth->finish; |
231 |
$query = qq/ SELECT error FROM updatedb_error WHERE version = ? ORDER BY version/; |
232 |
$sth = $dbh->prepare( $query ); |
233 |
$sth->execute( $version->{version} ); |
234 |
$version->{errors} = $sth->fetchall_arrayref( {} ); |
235 |
$sth->finish; |
236 |
} |
237 |
return $versions; |
238 |
} |
239 |
|
240 |
=head2 execute |
241 |
|
242 |
my @errors = $execute(\@queries); |
243 |
This sub will execute queries coming from an execute_version based on a .sql file |
244 |
|
245 |
=cut |
246 |
|
247 |
sub execute { |
248 |
my ( $queries ) = @_; |
249 |
my @errors; |
250 |
for my $query ( @{$queries->{queries}} ) { |
251 |
eval { |
252 |
$dbh->do( $query ); |
253 |
}; |
254 |
push @errors, get_error(); |
255 |
} |
256 |
return \@errors; |
257 |
} |
258 |
|
259 |
=head2 get_tables_name |
260 |
|
261 |
my $tables = get_tables_name; |
262 |
return an array with all Koha mySQL table names |
263 |
|
264 |
=cut |
265 |
|
266 |
sub get_tables_name { |
267 |
my $sth = $dbh->prepare("SHOW TABLES"); |
268 |
$sth->execute(); |
269 |
my @tables; |
270 |
while ( my ( $table ) = $sth->fetchrow_array ) { |
271 |
push @tables, $table; |
272 |
} |
273 |
return \@tables; |
274 |
} |
275 |
my $tables; |
276 |
|
277 |
=head2 check_coherency |
278 |
|
279 |
my $errors = check_coherency($query); |
280 |
This sub will try to check if a SQL query is useless or no. |
281 |
for queries that are CREATE TABLE, it will check if the table already exists |
282 |
for queries that are ALTER TABLE, it will search if the modification has already been made |
283 |
for queries that are INSERT, it will search if the insert has already been made if it's a syspref or a permission |
284 |
|
285 |
Those test cover 90% of the updatedatabases cases. That will help finding duplicate or inconsistencies |
286 |
|
287 |
=cut |
288 |
|
289 |
sub check_coherency { |
290 |
my ( $query ) = @_; |
291 |
$tables = get_tables_name() if not $tables; |
292 |
|
293 |
given ( $query ) { |
294 |
when ( /CREATE TABLE(?:.*?)? `?(\w+)`?/ ) { |
295 |
my $table_name = $1; |
296 |
if ( grep { /$table_name/ } @$tables ) { |
297 |
die "COHERENCY: Table $table_name already exists"; |
298 |
} |
299 |
} |
300 |
|
301 |
when ( /ALTER TABLE *`?(\w+)`? *ADD *(?:COLUMN)? `?(\w+)`?/ ) { |
302 |
my $table_name = $1; |
303 |
my $column_name = $2; |
304 |
next if $column_name =~ /(UNIQUE|CONSTRAINT|INDEX|KEY|FOREIGN)/; |
305 |
if ( not grep { /$table_name/ } @$tables ) { |
306 |
return "COHERENCY: Table $table_name does not exist"; |
307 |
} else { |
308 |
my $sth = $dbh->prepare( "DESC $table_name $column_name" ); |
309 |
my $rv = $sth->execute; |
310 |
if ( $rv > 0 ) { |
311 |
die "COHERENCY: Field $table_name.$column_name already exists"; |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
when ( /INSERT INTO `?(\w+)`?.*?VALUES *\((.*?)\)/ ) { |
317 |
my $table_name = $1; |
318 |
my @values = split /,/, $2; |
319 |
s/^ *'// foreach @values; |
320 |
s/' *$// foreach @values; |
321 |
given ( $table_name ) { |
322 |
when ( /systempreferences/ ) { |
323 |
my $syspref = $values[0]; |
324 |
my $sth = $dbh->prepare( "SELECT COUNT(*) FROM systempreferences WHERE variable = ?" ); |
325 |
$sth->execute( $syspref ); |
326 |
if ( ( my $count = $sth->fetchrow_array ) > 0 ) { |
327 |
die "COHERENCY: Syspref $syspref already exists"; |
328 |
} |
329 |
} |
330 |
|
331 |
when ( /permissions/){ |
332 |
my $module_bit = $values[0]; |
333 |
my $code = $values[1]; |
334 |
my $sth = $dbh->prepare( "SELECT COUNT(*) FROM permissions WHERE module_bit = ? AND code = ?" ); |
335 |
$sth->execute($module_bit, $code); |
336 |
if ( ( my $count = $sth->fetchrow_array ) > 0 ) { |
337 |
die "COHERENCY: Permission $code already exists"; |
338 |
} |
339 |
} |
340 |
} |
341 |
} |
342 |
} |
343 |
return 1; |
344 |
} |
345 |
|
346 |
=head2 get_error |
347 |
|
348 |
my $errors = get_error() |
349 |
This sub will return any mySQL error that occured during an update |
350 |
|
351 |
=cut |
352 |
|
353 |
sub get_error { |
354 |
my @errors = $dbh->selectrow_array(qq{SHOW ERRORS}); # Get errors |
355 |
my @warnings = $dbh->selectrow_array(qq{SHOW WARNINGS}); # Get warnings |
356 |
if ( @errors ) { # Catch specifics errors |
357 |
return qq{$errors[0] : $errors[1] => $errors[2]}; |
358 |
} elsif ( @warnings ) { |
359 |
return qq{$warnings[0] : $warnings[1] => $warnings[2]} |
360 |
if $warnings[0] ne 'Note'; |
361 |
} |
362 |
return; |
363 |
} |
364 |
|
365 |
=head2 |
366 |
|
367 |
my $result = get_queries($filepath); |
368 |
this sub will return a hashref with 2 entries: |
369 |
$result->{queries} is an array with all queries to execute |
370 |
$result->{comments} is an array with all comments in the .sql file |
371 |
|
372 |
=cut |
373 |
|
374 |
sub get_queries { |
375 |
my ( $filepath ) = @_; |
376 |
open my $fh, "<", $filepath; |
377 |
my @queries; |
378 |
my @comments; |
379 |
my $old_delimiter = $/; |
380 |
while ( <$fh> ) { |
381 |
my $line = $_; |
382 |
chomp $line; |
383 |
$line =~ s/^\s*//; |
384 |
if ( $line =~ s/^--(.*)$// ) { |
385 |
push @comments, $1; |
386 |
next; |
387 |
} |
388 |
if ( $line =~ /^delimiter (.*)$/i ) { |
389 |
$/ = $1; |
390 |
next; |
391 |
} |
392 |
$line =~ s#$/##; |
393 |
push @queries, $line if not $line =~ /^\s*$/; # Push if query is not empty |
394 |
} |
395 |
$/ = $old_delimiter; |
396 |
close $fh; |
397 |
|
398 |
return { queries => \@queries, comments => \@comments }; |
399 |
} |
400 |
|
401 |
=head2 md5_already_exists |
402 |
|
403 |
my $result = md5_already_exists($md5); |
404 |
check if the md5 of an update has already been applied on the database. |
405 |
If yes, it will return a hash with the version related to this md5 |
406 |
|
407 |
=cut |
408 |
|
409 |
sub md5_already_exists { |
410 |
my ( $md5 ) = @_; |
411 |
my $query = qq/SELECT version, md5 FROM updatedb_report WHERE md5 = ?/; |
412 |
my $sth = $dbh->prepare( $query ); |
413 |
$sth->execute( $md5 ); |
414 |
my @r; |
415 |
while ( my ( $version, $md5 ) = $sth->fetchrow ) { |
416 |
push @r, { version => $version, md5 => $md5 }; |
417 |
} |
418 |
$sth->finish; |
419 |
return \@r; |
420 |
} |
421 |
|
422 |
=head2 set_infos |
423 |
|
424 |
set_info($version,$queries, $error, $md5); |
425 |
this sub will insert into the updatedb tables what has been made on the database (queries, errors, result) |
426 |
|
427 |
=cut |
428 |
sub set_infos { |
429 |
my ( $version, $queries, $errors, $md5 ) = @_; |
430 |
SetVersion($version) if not -s $errors; |
431 |
for my $query ( @{ $queries->{queries} } ) { |
432 |
my $sth = $dbh->prepare("INSERT INTO updatedb_query(version, query) VALUES (?, ?)"); |
433 |
$sth->execute( $version, $query ); |
434 |
$sth->finish; |
435 |
} |
436 |
for my $error ( @$errors ) { |
437 |
my $sth = $dbh->prepare("INSERT INTO updatedb_error(version, error) VALUES (?, ?)"); |
438 |
$sth->execute( $version, $error ); |
439 |
} |
440 |
my $sth = $dbh->prepare("INSERT INTO updatedb_report(version, md5, comment, status) VALUES (?, ?, ?, ?)"); |
441 |
$sth->execute( |
442 |
$version, |
443 |
$md5, |
444 |
$queries&&exists $queries->{comments}? join ('\n', @{ $queries->{comments} }):'', |
445 |
( @$errors > 0 ) ? 0 : 1 |
446 |
); |
447 |
} |
448 |
|
449 |
=head2 mark_as_ok |
450 |
|
451 |
mark_as_ok($version); |
452 |
this sub will force to mark as "OK" an update that has failed |
453 |
once this has been made, the status will look as "forced OK", and appear in green like versions that have been applied without any problem |
454 |
|
455 |
=cut |
456 |
sub mark_as_ok { |
457 |
my ( $version ) = @_; |
458 |
my $sth = $dbh->prepare( "UPDATE updatedb_report SET status = 2 WHERE version=?" ); |
459 |
my $affected = $sth->execute( $version ); |
460 |
if ( $affected < 1 ) { |
461 |
# For "Coherency" |
462 |
my $filepath = get_filepath $version; |
463 |
my $queries = get_queries $filepath; |
464 |
my $errors; |
465 |
for my $query ( @{$queries->{queries}} ) { |
466 |
eval { |
467 |
check_coherency( $query ); |
468 |
}; |
469 |
if ( $@ ) { |
470 |
push @$errors, $@ |
471 |
} |
472 |
} |
473 |
|
474 |
$_ =~ s/at [^ ]* line \d*\.$// for @$errors; |
475 |
my $md5 = get_md5 $filepath; |
476 |
set_infos $version, $queries, $errors, $md5; |
477 |
|
478 |
$sth->execute( $version ); |
479 |
} |
480 |
$sth->finish; |
481 |
} |
482 |
|
483 |
=head2 is_uptodate |
484 |
is_uptodate(); |
485 |
return 1 if the database is up to date else 0. |
486 |
The database is up to date if all versions are excecuted. |
487 |
=cut |
488 |
sub is_uptodate { |
489 |
my $versions_availables = C4::Update::Database::list_versions_availables; |
490 |
my $versions = C4::Update::Database::list_versions_already_knows; |
491 |
for my $v ( @$versions_availables ) { |
492 |
if ( not grep { $v eq $$_{version} } @$versions ) { |
493 |
return 0; |
494 |
} |
495 |
} |
496 |
return 1; |
497 |
} |
498 |
|
499 |
=head2 TransformToNum |
500 |
|
501 |
Transform the Koha version from a 4 parts string |
502 |
to a number, with just 1 . (ie: it's a number) |
503 |
|
504 |
=cut |
505 |
sub TransformToNum { |
506 |
my $version = shift; |
507 |
|
508 |
# remove the 3 last . to have a Perl number |
509 |
$version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; |
510 |
$version =~ s/Bug(\d+)/$1/; |
511 |
return $version; |
512 |
} |
513 |
|
514 |
sub SetVersion { |
515 |
my $new_version = TransformToNum(shift); |
516 |
my $current_version = TransformToNum( C4::Context->preference('Version') ); |
517 |
unless ( C4::Context->preference('Version') ) { |
518 |
my $finish = $dbh->prepare(qq{ |
519 |
INSERT IGNORE INTO systempreferences (variable,value,explanation) |
520 |
VALUES ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller') |
521 |
}); |
522 |
$finish->execute($new_version); |
523 |
return; |
524 |
} |
525 |
if ( $new_version > $current_version ) { |
526 |
my $finish = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'"); |
527 |
$finish->execute($new_version); |
528 |
} |
529 |
} |
530 |
|
531 |
=head2 TableExists($table) |
532 |
|
533 |
=cut |
534 |
sub TableExists { |
535 |
my $table = shift; |
536 |
eval { |
537 |
local $dbh->{PrintError} = 0; |
538 |
local $dbh->{RaiseError} = 1; |
539 |
$dbh->do(qq{SELECT * FROM $table WHERE 1 = 0 }); |
540 |
}; |
541 |
return 1 unless $@; |
542 |
return 0; |
543 |
} |
544 |
|
545 |
1; |