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