X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUpgrade2.pm;h=650258378af4a44875d578568116bfaaddc0e147;hb=3270e88361efc8ceff21314b489732229f0f98f7;hp=d98627c563362906f280ef6958766d0bc16815ad;hpb=a23454bb1b039a31b7f77710ff663fa9152d530c;p=kivitendo-erp.git diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index d98627c56..650258378 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -1,5 +1,6 @@ package SL::DBUpgrade2; +use English qw(-no_match_vars); use IO::File; use List::MoreUtils qw(any); @@ -26,7 +27,7 @@ sub init { $params{path_suffix} ||= ''; $params{schema} ||= ''; - $params{path} = "sql/" . $params{dbdriver} . "-upgrade2" . $params{path_suffix}; + $params{path} = "sql/Pg-upgrade2" . $params{path_suffix}; map { $self->{$_} = $params{$_} } keys %params; @@ -80,9 +81,6 @@ sub parse_dbupdate_controls { next if ($control->{ignore}); - $control->{charset} = 'UTF-8' if $file =~ m/\.pl$/; - $control->{charset} = $control->{charset} || $control->{encoding} || Common::DEFAULT_CHARSET; - if (!$control->{"tag"}) { _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ; } @@ -132,29 +130,23 @@ sub parse_dbupdate_controls { sub process_query { $::lxdebug->enter_sub(); - my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_; + my ($self, $dbh, $filename, $version_or_control) = @_; my $form = $self->{form}; - my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n"); + my $fh = IO::File->new($filename, "r"); my $query = ""; my $sth; my @quote_chars; - my $file_charset = Common::DEFAULT_CHARSET; - while (<$fh>) { - last if !/^--/; - next if !/^--\s*\@(?:charset|encoding):\s*(.+)/; - $file_charset = $1; - last; + if (!$fh) { + return "No such file: $filename" if $self->{return_on_error}; + $form->error("$filename : $!\n"); } - $fh->seek(0, SEEK_SET); - - $db_charset ||= Common::DEFAULT_CHARSET; $dbh->begin_work(); while (<$fh>) { - $_ = SL::Iconv::convert($file_charset, $db_charset, $_); + $_ = SL::Iconv::convert('UTF-8', 'UTF-8', $_); # Remove DOS and Unix style line endings. chomp; @@ -195,6 +187,7 @@ sub process_query { $sth = $dbh->prepare($query); if (!$sth->execute()) { my $errstr = $dbh->errstr; + return $errstr // '' if $self->{return_on_error}; $sth->finish(); $dbh->rollback(); $form->dberror("The database update/creation did not succeed. " . @@ -230,38 +223,50 @@ sub process_query { $fh->close(); $::lxdebug->leave_sub(); + + # Signal "no error" + return undef; } # Process a Perl script which updates the database. # If the script returns 1 then the update was successful. -# Return code "2" means "needs more interaction; remove -# users/nologin and end current request". +# Return code "2" means "needs more interaction; unlock +# the system and end current request". # All other return codes are fatal errors. sub process_perl_script { $::lxdebug->enter_sub(); - my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_; + my ($self, $dbh, $filename, $version_or_control) = @_; + + my %form_values = map { $_ => $::form->{$_} } qw(dbconnect dbdefault dbhost dbname dboptions dbpasswd dbport dbupdate dbuser login template_object version); $dbh->begin_work; # setup dbup_ export vars & run script + my $old_dbh = $::form->set_standard_dbh($dbh); my %dbup_myconfig = map { ($_ => $::form->{$_}) } qw(dbname dbuser dbpasswd dbhost dbport dbconnect); - my $result = SL::DBUpgrade2::Base::execute_script( - file_name => $filename, - tag => $version_or_control->{tag}, - dbh => $dbh, - myconfig => \%dbup_myconfig, - ); + my $result = eval { + SL::DBUpgrade2::Base::execute_script( + file_name => $filename, + tag => $version_or_control->{tag}, + dbh => $dbh, + myconfig => \%dbup_myconfig, + ); + }; - if (1 != ($result // 1)) { - $dbh->rollback(); - } + my $error = $EVAL_ERROR; + + $::form->set_standard_dbh($old_dbh); + + $dbh->rollback if 1 != ($result // -1); + + return $error if $self->{return_on_error} && (1 != ($result // -1)); if (!defined($result)) { - print $::form->parse_html_template("dbupgrade/error", { file => $filename, error => $@ }); + print $::form->parse_html_template("dbupgrade/error", { file => $filename, error => $error }); ::end_of_request(); } elsif (1 != $result) { - unlink("users/nologin") if (2 == $result); + SL::System::InstallationLock->unlock if 2 == $result; ::end_of_request(); } @@ -270,32 +275,26 @@ sub process_perl_script { } elsif ($version_or_control) { $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control)); } - $dbh->commit(); - - $::lxdebug->leave_sub(); -} -sub process_file { - my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_; + $dbh->commit if !$dbh->{AutoCommit} || $dbh->{BegunWork}; - if ($filename =~ m/sql$/) { - $self->process_query($dbh, $filename, $version_or_control, $db_charset); - } else { - $self->process_perl_script($dbh, $filename, $version_or_control, $db_charset); - } -} + # Clear $::form of values that may have been set so that following + # Perl upgrade scripts won't have to work with old data (think of + # the usual 'continued' mechanism that's used for determining + # whether or not the upgrade form must be displayed). + delete @{ $::form }{ keys %{ $::form } }; + $::form->{$_} = $form_values{$_} for keys %form_values; -sub update_available { - my ($self, $cur_version) = @_; + $::lxdebug->leave_sub(); - local *SQLDIR; + return undef; +} - my $dbdriver = $self->{dbdriver}; - opendir SQLDIR, "sql/${dbdriver}-upgrade" || error("", "sql/${dbdriver}-upgrade: $!"); - my @upgradescripts = grep /${dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)$/, readdir SQLDIR; - closedir SQLDIR; +sub process_file { + my ($self, $dbh, $filename, $version_or_control) = @_; - return ($#upgradescripts > -1); + return $filename =~ m/sql$/ ? $self->process_query( $dbh, $filename, $version_or_control) + : $self->process_perl_script($dbh, $filename, $version_or_control); } sub update2_available { @@ -346,10 +345,9 @@ sub apply_admin_dbupgrade_scripts { return 0 if !@unapplied_scripts; - my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET; $self->{form}->{login} ||= 'admin'; - map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $self->{all_controls} }; + map { $_->{description} = SL::Iconv::convert('UTF-8', 'UTF-8', $_->{description}) } values %{ $self->{all_controls} }; if ($called_from_admin) { $self->{form}->{title} = $::locale->text('Dataset upgrade'); @@ -362,7 +360,7 @@ sub apply_admin_dbupgrade_scripts { $::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}"); print $self->{form}->parse_html_template("dbupgrade/upgrade_message2", $control); - $self->process_file($dbh, "sql/$self->{dbdriver}-upgrade2-auth/$control->{file}", $control, $db_charset); + $self->process_file($dbh, "sql/Pg-upgrade2-auth/$control->{file}", $control); } print $self->{form}->parse_html_template("dbupgrade/footer", { is_admin => 1 }) if $called_from_admin; @@ -438,8 +436,7 @@ __END__ =head1 NAME SL::DBUpgrade2 - Parse database upgrade files stored in -C and C (and also in -C) +C and C =head1 SYNOPSIS @@ -449,7 +446,6 @@ C) # Apply outstanding updates to the authentication database my $scripts = SL::DBUpgrade2->new( form => $::form, - dbdriver => 'Pg', auth => 1 ); $scripts->apply_admin_dbupgrade_scripts(1); @@ -457,10 +453,11 @@ C) # Apply updates to a user database my $scripts = SL::DBUpgrade2->new( form => $::form, - dbdriver => $::form->{dbdriver}, auth => 1 ); - User->dbupdate2($form, $scripts->parse_dbupdate_controls); + User->dbupdate2(form => $form, + updater => $scripts->parse_dbupdate_controls, + database => $dbname); =head1 OVERVIEW @@ -487,14 +484,8 @@ applied. Database upgrade files come in two flavours: SQL files and Perl files. For both there are control fields that determine the order in -which they're executed, what charset the scripts are written in -etc. The control fields are tag/value pairs contained in comments. - -=head1 OLD UPGRADE FILES - -The files in C are so old that I don't bother -documenting them. They're handled by this class, too, but new files -are only created as C files. +which they're executed etc. The control fields are tag/value pairs +contained in comments. =head1 CONTROL FIELDS @@ -539,12 +530,6 @@ A space-separated list of tags of scripts this particular script depends on. All other upgrades listed in C will be applied before the current one is applied. -=item charset -=item encoding - -The charset this file uses. Defaults to C if -missing. Both terms are recognized. - =item priority Ordering the scripts by their dependencies alone produces a lot of @@ -590,11 +575,6 @@ Path to the upgrade files to parse. Required. C object to use. Required. -=item dbdriver - -Name of the database driver. Currently only C for PostgreSQL is -supported. - =item auth Optional parameter defaulting to 0. If trueish then the scripts read @@ -610,30 +590,27 @@ are missing/wrong (e.g. a tag name listed in C is not found). Sets C<$Self->{all_controls}> to the list of database scripts. -=item C +=item C Applies a single database upgrade file. Calls L for Perl update files and C for SQL update files. Requires an open database handle(C<$dbh>), the file name -(C<$filename>), a hash structure of the file's control fields as -produced by L (C<$version_or_control>) and -the database charset (for on-the-fly charset recoding of the script if -required, C<$db_charset>). +(C<$filename>) and a hash structure of the file's control fields as +produced by L (C<$version_or_control>). Returns the result of the actual function called. -=item C +=item C Applies a single Perl database upgrade file. Requires an open database -handle(C<$dbh>), the file name (C<$filename>), a hash structure of the -file's control fields as produced by L -(C<$version_or_control>) and the database charset (for on-the-fly -charset recoding of the script if required, C<$db_charset>). +handle(C<$dbh>), the file name (C<$filename>) and a hash structure of +the file's control fields as produced by L +(C<$version_or_control>). Perl scripts are executed via L. If L returns falsish then an error is expected. There are two special return values: If the script returns C<1> then the update was successful. Return code C<2> -means "needs more interaction from the user; remove users/nologin and +means "needs more interaction from the user; unlock the system and end current upgrade process". All other return codes are fatal errors. Inside the Perl script several local variables exist that can be used: @@ -668,13 +645,12 @@ the following function: } } -=item C +=item C Applies a single SQL database upgrade file. Requires an open database -handle(C<$dbh>), the file name (C<$filename>), a hash structure of the -ofile's control fields as produced by L -(C<$version_or_control>) and the database charset (for on-the-fly -charset recoding of the script if required, C<$db_charset>). +handle(C<$dbh>), the file name (C<$filename>), and a hash structure of +the file's control fields as produced by L +(C<$version_or_control>). =item C