Merge branch 'rb-wiederkehrende-rechnungen' into after-262
[kivitendo-erp.git] / SL / DBUpgrade2.pm
index c9c742c..af5037a 100644 (file)
@@ -4,6 +4,7 @@ use IO::File;
 use List::MoreUtils qw(any);
 
 use SL::Common;
+use SL::DBUtils;
 use SL::Iconv;
 
 use strict;
@@ -272,7 +273,7 @@ sub process_perl_script {
   }
 
   if (ref($version_or_control) eq "HASH") {
-    $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
+    $dbh->do("INSERT INTO " . $self->{schema} . "schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
   } elsif ($version_or_control) {
     $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control));
   }
@@ -326,6 +327,56 @@ sub update2_available {
   return $needs_update;
 }
 
+sub unapplied_upgrade_scripts {
+  my ($self, $dbh) = @_;
+
+  my @all_scripts = map { $_->{applied} = 0; $_ } $self->sort_dbupdate_controls;
+
+  my $query = qq|SELECT tag FROM | . $self->{schema} . qq|schema_info|;
+  my $sth   = $dbh->prepare($query);
+  $sth->execute || $self->{form}->dberror($query);
+  while (my ($tag) = $sth->fetchrow_array()) {
+    $self->{all_controls}->{$tag}->{applied} = 1 if defined $self->{all_controls}->{$tag};
+  }
+  $sth->finish;
+
+  return grep { !$_->{applied} } @all_scripts;
+}
+
+sub apply_admin_dbupgrade_scripts {
+  my ($self, $called_from_admin) = @_;
+
+  return 0 if !$self->{auth};
+
+  my $dbh               = $::auth->dbconnect;
+  my @unapplied_scripts = $self->unapplied_upgrade_scripts($dbh);
+
+  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} };
+
+  if ($called_from_admin) {
+    $self->{form}->{title} = $::locale->text('Dataset upgrade');
+    $self->{form}->header;
+  }
+
+  print $self->{form}->parse_html_template("dbupgrade/header", { dbname => $::auth->{DB_config}->{db} });
+
+  foreach my $control (@unapplied_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);
+  }
+
+  print $self->{form}->parse_html_template("dbupgrade/footer", { is_admin => 1 }) if $called_from_admin;
+
+  return 1;
+}
+
 sub _check_for_loops {
   my ($form, $file_name, $controls, $tag, @path) = @_;
 
@@ -379,6 +430,8 @@ sub _dbupdate2_calculate_depth {
 sub sort_dbupdate_controls {
   my $self = shift;
 
+  $self->parse_dbupdate_controls unless $self->{all_controls};
+
   return sort { ($a->{depth} <=> $b->{depth}) || ($a->{priority} <=> $b->{priority}) || ($a->{tag} cmp $b->{tag}) } values %{ $self->{all_controls} };
 }