X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUpgrade2.pm;h=0b14eca5bb2a3c439aa3ab9c4aa81f0f44b8bccf;hb=43816cb7289ee9225c3bb229d077bd87a75562cc;hp=5c8280b2c067555f47cb2e97f52a45e97dc5b783;hpb=865ea676d6394b4fb7e675866812248eeedd5beb;p=kivitendo-erp.git diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index 5c8280b2c..0b14eca5b 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -7,6 +7,7 @@ use List::MoreUtils qw(any); use SL::Common; use SL::DBUpgrade2::Base; use SL::DBUtils; +use SL::System::Process; use strict; @@ -26,7 +27,7 @@ sub init { $params{path_suffix} ||= ''; $params{schema} ||= ''; - $params{path} ||= "sql/Pg-upgrade2" . $params{path_suffix}; + $params{path} ||= SL::System::Process->exe_dir . "/sql/Pg-upgrade2" . $params{path_suffix}; map { $self->{$_} = $params{$_} } keys %params; @@ -55,9 +56,10 @@ sub parse_dbupdate_controls { $file =~ s|.*/||; my $control = { - "priority" => 1000, - "depends" => [], - "locales" => [], + priority => 1000, + depends => [], + required_by => [], + locales => [], }; while () { @@ -70,8 +72,8 @@ sub parse_dbupdate_controls { my @fields = split(/\s*:\s*/, $_, 2); next unless (scalar(@fields) == 2); - if ($fields[0] eq "depends") { - push(@{$control->{"depends"}}, split(/\s+/, $fields[1])); + if ($fields[0] =~ m{^(?:depends|required_by)$}) { + push(@{$control->{$fields[0]}}, split(/\s+/, $fields[1])); } elsif ($fields[0] eq "locales") { push @{$control->{locales}}, $fields[1]; } else { @@ -97,17 +99,31 @@ sub parse_dbupdate_controls { _control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ; } + delete @{$control}{qw(depth applied)}; + + my @unknown_keys = grep { !m{^ (?: depends | required_by | description | file | ignore | locales | may_fail | priority | superuser_privileges | tag ) $}x } keys %{ $control }; + if (@unknown_keys) { + _control_error($form, $file_name, sprintf($locale->text("Unknown control fields: #1", join(' ', sort({ lc $a cmp lc $b } @unknown_keys))))); + } + $control->{"priority"} *= 1; $control->{"priority"} ||= 1000; $control->{"file"} = $file; - delete @{$control}{qw(depth applied)}; - $all_controls{$control->{"tag"}} = $control; close(IN); } + foreach my $name (keys %all_controls) { + my $control = $all_controls{$name}; + + foreach my $dependency (@{ delete $control->{required_by} }) { + _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency})); + push @{ $all_controls{$dependency}->{depends} }, $name; + } + } + foreach my $control (values(%all_controls)) { foreach my $dependency (@{$control->{"depends"}}) { _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency})); @@ -290,8 +306,12 @@ sub process_perl_script { sub process_file { my ($self, $dbh, $filename, $version_or_control) = @_; - return $filename =~ m/sql$/ ? $self->process_query( $dbh, $filename, $version_or_control) - : $self->process_perl_script($dbh, $filename, $version_or_control); + my $result = $filename =~ m/sql$/ ? $self->process_query( $dbh, $filename, $version_or_control) + : $self->process_perl_script($dbh, $filename, $version_or_control); + + $::lxdebug->log_time("DB upgrade script '${filename}' finished"); + + return $result; } sub unapplied_upgrade_scripts { @@ -310,14 +330,6 @@ sub unapplied_upgrade_scripts { return grep { !$_->{applied} } @all_scripts; } -sub update2_available { - my ($self, $dbh) = @_; - - my @unapplied_scripts = $self->unapplied_upgrade_scripts($dbh); - - return !!@unapplied_scripts; -} - sub apply_admin_dbupgrade_scripts { my ($self, $called_from_admin) = @_; @@ -337,6 +349,8 @@ sub apply_admin_dbupgrade_scripts { print $self->{form}->parse_html_template("dbupgrade/header", { dbname => $::auth->{DB_config}->{db} }); + $::lxdebug->log_time("DB upgrades commencing"); + foreach my $control (@unapplied_scripts) { $::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}"); print $self->{form}->parse_html_template("dbupgrade/upgrade_message2", $control); @@ -344,6 +358,8 @@ sub apply_admin_dbupgrade_scripts { $self->process_file($dbh, "sql/Pg-upgrade2-auth/$control->{file}", $control); } + $::lxdebug->log_time("DB upgrades finished"); + print $self->{form}->parse_html_template("dbupgrade/footer", { is_admin => 1 }) if $called_from_admin; return 1; @@ -609,7 +625,7 @@ The global C object. =back A Perl script can actually implement queries that fail while -continueing the process by handling the transaction itself, e.g. with +continuing the process by handling the transaction itself, e.g. with the following function: sub do_query { @@ -643,14 +659,6 @@ to the database that is checked. Requires that the scripts have been parsed. -=item C - -Returns trueish if at least one upgrade script hasn't been applied to -a database yet. C<$dbh> is an open handle to the database that is -checked. - -Requires that the scripts have been parsed. - =back =head1 BUGS