WebshopApi: falsche sql update Abhängigkeit
[kivitendo-erp.git] / SL / DBUpgrade2.pm
index 283e5a4..82bd27f 100644 (file)
@@ -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;
 
@@ -38,8 +39,6 @@ sub path {
 }
 
 sub parse_dbupdate_controls {
-  $::lxdebug->enter_sub();
-
   my ($self) = @_;
 
   my $form   = $self->{form};
@@ -124,8 +123,6 @@ sub parse_dbupdate_controls {
 
   $self->{all_controls} = \%all_controls;
 
-  $::lxdebug->leave_sub();
-
   return $self;
 }
 
@@ -151,9 +148,6 @@ sub process_query {
     # Remove DOS and Unix style line endings.
     chomp;
 
-    # remove comments
-    s/--.*$//;
-
     for (my $i = 0; $i < length($_); $i++) {
       my $char = substr($_, $i, 1);
 
@@ -180,6 +174,11 @@ sub process_query {
              &&  $tag      =~ /^ (?= [A-Za-z_] [A-Za-z0-9_]* | ) $/x) {  # tag is identifier
           push @quote_chars, $char = '$' . $tag . '$';
           $i = $tag_end;
+        } elsif ($char eq "-") {
+          if ( substr($_, $i+1, 1) eq "-") {
+            # found a comment outside quote
+            last;
+          }
         } elsif ($char eq ";") {
 
           # Query is complete. Send it.
@@ -190,11 +189,13 @@ sub process_query {
             return $errstr // '<unknown database error>' if $self->{return_on_error};
             $sth->finish();
             $dbh->rollback();
-            $form->dberror("The database update/creation did not succeed. " .
-                           "The file ${filename} containing the following " .
-                           "query failed:<br>${query}<br>" .
-                           "The error message was: ${errstr}<br>" .
-                           "All changes in that file have been reverted.");
+            if (!ref $version_or_control || ref $version_or_control ne 'HASH' || !$version_or_control->{may_fail})  {
+              $form->dberror("The database update/creation did not succeed. " .
+                             "The file ${filename} containing the following " .
+                             "query failed:<br>${query}<br>" .
+                             "The error message was: ${errstr}<br>" .
+                             "All changes in that file have been reverted.")
+            }
           }
           $sth->finish();
 
@@ -238,12 +239,11 @@ sub process_perl_script {
 
   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);
+  my %form_values = %$::form;
 
   $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(
@@ -256,18 +256,16 @@ 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();
+    $::dispatcher->end_request;
   } elsif (1 != $result) {
     SL::System::InstallationLock->unlock if 2 == $result;
-    ::end_of_request();
+    $::dispatcher->end_request;
   }
 
   if (ref($version_or_control) eq "HASH") {
@@ -293,8 +291,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 {
@@ -340,6 +342,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);
@@ -347,6 +351,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;
@@ -381,13 +387,11 @@ sub _control_error {
 }
 
 sub _dbupdate2_calculate_depth {
-  $::lxdebug->enter_sub(2);
-
   my ($tree, $tag) = @_;
 
   my $node = $tree->{$tag};
 
-  return $::lxdebug->leave_sub(2) if (defined($node->{"depth"}));
+  return if (defined($node->{"depth"}));
 
   my $max_depth = 0;
 
@@ -398,8 +402,6 @@ sub _dbupdate2_calculate_depth {
   }
 
   $node->{"depth"} = $max_depth + 1;
-
-  $::lxdebug->leave_sub(2);
 }
 
 sub sort_dbupdate_controls {
@@ -616,7 +618,7 @@ The global C<SL::Form> 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 {