X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUpgrade2.pm;h=3cdb9a44b9bbce7836e702202ab5303e4a44c03c;hb=1f1cec8a23e29c0645e5676a37c50cb4bbc7c211;hp=54a651c057025a5505f2f358170fbf325c9543e2;hpb=dbda14c263efd93aca3b7114015a47d86b8581e3;p=kivitendo-erp.git diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index 54a651c05..3cdb9a44b 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -81,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} || 'UTF-8'; - if (!$control->{"tag"}) { _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ; } @@ -136,24 +133,20 @@ sub process_query { 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 = 'UTF-8'; - 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); $dbh->begin_work(); while (<$fh>) { - $_ = SL::Iconv::convert($file_charset, 'UTF-8', $_); + $_ = SL::Iconv::convert('UTF-8', 'UTF-8', $_); # Remove DOS and Unix style line endings. chomp; @@ -194,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. " . @@ -229,6 +223,9 @@ sub process_query { $fh->close(); $::lxdebug->leave_sub(); + + # Signal "no error" + return undef; } # Process a Perl script which updates the database. @@ -246,6 +243,7 @@ sub process_perl_script { $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 = eval { SL::DBUpgrade2::Base::execute_script( @@ -258,8 +256,12 @@ sub process_perl_script { 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 => $error }); ::end_of_request(); @@ -273,7 +275,8 @@ sub process_perl_script { } elsif ($version_or_control) { $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control)); } - $dbh->commit(); + + $dbh->commit if !$dbh->{AutoCommit} || $dbh->{BegunWork}; # 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 @@ -283,28 +286,15 @@ sub process_perl_script { $::form->{$_} = $form_values{$_} for keys %form_values; $::lxdebug->leave_sub(); + + return undef; } sub process_file { my ($self, $dbh, $filename, $version_or_control) = @_; - if ($filename =~ m/sql$/) { - $self->process_query($dbh, $filename, $version_or_control); - } else { - $self->process_perl_script($dbh, $filename, $version_or_control); - } -} - -sub update_available { - my ($self, $cur_version) = @_; - - local *SQLDIR; - - opendir SQLDIR, "sql/Pg-upgrade" || error("", "sql/Pg-upgrade: $!"); - my @upgradescripts = grep /Pg-upgrade-\Q$cur_version\E.*\.(sql|pl)$/, readdir SQLDIR; - closedir SQLDIR; - - 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 { @@ -357,7 +347,7 @@ sub apply_admin_dbupgrade_scripts { $self->{form}->{login} ||= 'admin'; - map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{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'); @@ -446,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 @@ -495,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 @@ -547,13 +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