X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUpgrade2.pm;h=a583af8a55d49c8ca76a7208f56eb44016624af9;hb=417cc3a7e5d4a96713b71b1d723196bddeeb01cc;hp=3d5d0de5ced8dec64004f54fe28600c805f921de;hpb=0b6cb3b882036db18ce9c283bfaebe3bc483507e;p=kivitendo-erp.git diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index 3d5d0de5c..a583af8a5 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -1,8 +1,10 @@ package SL::DBUpgrade2; use IO::File; +use List::MoreUtils qw(any); use SL::Common; +use SL::DBUtils; use SL::Iconv; use strict; @@ -16,23 +18,31 @@ sub new { sub init { my ($self, %params) = @_; + if ($params{auth}) { + $params{path_suffix} = "-auth"; + $params{schema} = "auth."; + } + + $params{path_suffix} ||= ''; + $params{schame} ||= ''; + map { $self->{$_} = $params{$_} } keys %params; return $self; } sub parse_dbupdate_controls { - $main::lxdebug->enter_sub(); + $::lxdebug->enter_sub(); my ($self) = @_; my $form = $self->{form}; - my $locale = $main::locale; + my $locale = $::locale; local *IN; my %all_controls; - my $path = "sql/" . $self->{dbdriver} . "-upgrade2"; + my $path = "sql/" . $self->{dbdriver} . "-upgrade2" . $self->{path_suffix}; foreach my $file_name (<$path/*.sql>, <$path/*.pl>) { next unless (open(IN, $file_name)); @@ -107,13 +117,13 @@ sub parse_dbupdate_controls { $self->{all_controls} = \%all_controls; - $main::lxdebug->leave_sub(); + $::lxdebug->leave_sub(); - return \%all_controls; + return $self; } sub process_query { - $main::lxdebug->enter_sub(); + $::lxdebug->enter_sub(); my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_; @@ -192,18 +202,15 @@ sub process_query { } 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)); + $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control)); } $dbh->commit(); $fh->close(); - $main::lxdebug->leave_sub(); + $::lxdebug->leave_sub(); } # Process a Perl script which updates the database. @@ -212,7 +219,7 @@ sub process_query { # users/nologin and end current request". # All other return codes are fatal errors. sub process_perl_script { - $main::lxdebug->enter_sub(); + $::lxdebug->enter_sub(); my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_; @@ -238,7 +245,7 @@ sub process_perl_script { $db_charset ||= Common::DEFAULT_CHARSET; - my $iconv = SL::Iconv::get_converter($file_charset, $db_charset); + my $iconv = SL::Iconv->new($file_charset, $db_charset); $dbh->begin_work(); @@ -266,16 +273,13 @@ 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)); + $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control)); } $dbh->commit(); - $main::lxdebug->leave_sub(); + $::lxdebug->leave_sub(); } sub process_file { @@ -288,6 +292,91 @@ sub process_file { } } +sub update_available { + my ($self, $cur_version) = @_; + + local *SQLDIR; + + 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; + + return ($#upgradescripts > -1); +} + +sub update2_available { + $::lxdebug->enter_sub(); + + my ($self, $dbh) = @_; + + map { $_->{applied} = 0; } values %{ $self->{all_controls} }; + + my $sth = $dbh->prepare(qq|SELECT tag FROM | . $self->{schema} . qq|schema_info|); + if ($sth->execute) { + while (my ($tag) = $sth->fetchrow_array) { + $self->{all_controls}->{$tag}->{applied} = 1 if defined $self->{all_controls}->{$tag}; + } + } + $sth->finish(); + + my $needs_update = any { !$_->{applied} } values %{ $self->{all_controls} }; + + $::lxdebug->leave_sub(); + + 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) = @_; @@ -297,7 +386,7 @@ sub _check_for_loops { if ($ctrl->{"loop"} == 1) { # Not done yet. - _control_error($form, $file_name, $main::locale->text("Dependency loop detected:") . " " . join(" -> ", @path)) + _control_error($form, $file_name, $::locale->text("Dependency loop detected:") . " " . join(" -> ", @path)) } elsif ($ctrl->{"loop"} == 0) { # Not checked yet. @@ -310,20 +399,20 @@ sub _check_for_loops { sub _control_error { my ($form, $file_name, $message) = @_; - $form = $main::form; - my $locale = $main::locale; + $form = $::form; + my $locale = $::locale; $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message)); } sub _dbupdate2_calculate_depth { - $main::lxdebug->enter_sub(2); + $::lxdebug->enter_sub(2); my ($tree, $tag) = @_; my $node = $tree->{$tag}; - return $main::lxdebug->leave_sub(2) if (defined($node->{"depth"})); + return $::lxdebug->leave_sub(2) if (defined($node->{"depth"})); my $max_depth = 0; @@ -335,15 +424,15 @@ sub _dbupdate2_calculate_depth { $node->{"depth"} = $max_depth + 1; - $main::lxdebug->leave_sub(2); + $::lxdebug->leave_sub(2); } sub sort_dbupdate_controls { my $self = shift; - return sort({ $a->{"depth"} != $b->{"depth"} ? $a->{"depth"} <=> $b->{"depth"} - : $a->{"priority"} != $b->{"priority"} ? $a->{"priority"} <=> $b->{"priority"} - : $a->{"tag"} cmp $b->{"tag"} } values(%{ $self->{all_controls} })); + $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} }; } 1;