From: Moritz Bunkus Date: Tue, 30 Nov 2010 07:42:07 +0000 (+0100) Subject: Funktionen update_available, update2_available nach DBUpgrade2.pm verschoben X-Git-Tag: release-2.6.2beta1~17^2~11^2~5 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=38a2e7894debd7bc5787e74129f8f6f4e74ed2dc;p=kivitendo-erp.git Funktionen update_available, update2_available nach DBUpgrade2.pm verschoben --- diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index 08fdd874a..c9c742c21 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -1,6 +1,7 @@ package SL::DBUpgrade2; use IO::File; +use List::MoreUtils qw(any); use SL::Common; use SL::Iconv; @@ -117,7 +118,7 @@ sub parse_dbupdate_controls { $::lxdebug->leave_sub(); - return \%all_controls; + return $self; } sub process_query { @@ -290,6 +291,41 @@ 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 _check_for_loops { my ($form, $file_name, $controls, $tag, @path) = @_; diff --git a/SL/User.pm b/SL/User.pm index ec0968705..e1ec6ef49 100644 --- a/SL/User.pm +++ b/SL/User.pm @@ -121,19 +121,16 @@ sub login { $self->create_schema_info_table($form, $dbh); - $dbh->disconnect; - $rc = 0; - my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $myconfig{dbdriver}); - my $controls = $dbupdater->parse_dbupdate_controls; + my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $myconfig{dbdriver})->parse_dbupdate_controls; - map({ $form->{$_} = $myconfig{$_} } - qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat)); - - if (update_available($myconfig{"dbdriver"}, $dbversion) || - update2_available($form, $controls)) { + map({ $form->{$_} = $myconfig{$_} } qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat)); + dbconnect_vars($form, $form->{dbname}); + my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh); + $dbh->disconnect; + if ($update_available) { $form->{"stylesheet"} = "lx-office-erp.css"; $form->{"title"} = $main::locale->text("Dataset upgrade"); $form->header(); @@ -456,8 +453,8 @@ sub dbneedsupdate { my ($self, $form) = @_; - my %members = $main::auth->read_all_users(); - my $controls = SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls; + my %members = $main::auth->read_all_users(); + my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls; my ($query, $sth, %dbs_needing_updates); @@ -479,11 +476,13 @@ sub dbneedsupdate { ($version) = $sth->fetchrow_array(); } $sth->finish(); - $dbh->disconnect(); - next unless $version; + $dbh->disconnect and next unless $version; + + my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh); + $dbh->disconnect; - if (update_available($form->{dbdriver}, $version) || update2_available($form, $controls)) { + if ($update_available) { my $dbinfo = {}; map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member }; $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo; @@ -537,18 +536,6 @@ sub cmp_script_version { return $res_a <=> $res_b; } -sub update_available { - my ($dbdriver, $cur_version) = @_; - - local *SQLDIR; - - 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 create_schema_info_table { $main::lxdebug->enter_sub(); @@ -730,38 +717,6 @@ sub dbupdate2 { return $rc; } -sub update2_available { - $main::lxdebug->enter_sub(); - - my ($form, $controls) = @_; - - map({ $_->{"applied"} = 0; } values(%{$controls})); - - dbconnect_vars($form, $form->{"dbname"}); - - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) || - $form->dberror; - - my ($query, $tag, $sth); - - $query = qq|SELECT tag FROM schema_info|; - $sth = $dbh->prepare($query); - if ($sth->execute()) { - while (($tag) = $sth->fetchrow_array()) { - $controls->{$tag}->{"applied"} = 1 if (defined($controls->{$tag})); - } - } - $sth->finish(); - $dbh->disconnect(); - - map({ $main::lxdebug->leave_sub() and return 1 if (!$_->{"applied"}) } - values(%{$controls})); - - $main::lxdebug->leave_sub(); - return 0; -} - sub save_member { $main::lxdebug->enter_sub(); diff --git a/bin/mozilla/admin.pl b/bin/mozilla/admin.pl index c5c97cd89..0e61c3c85 100755 --- a/bin/mozilla/admin.pl +++ b/bin/mozilla/admin.pl @@ -719,15 +719,13 @@ sub dbupdate { map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd); - my $controls = parse_dbupdate_controls($form, $form->{dbdriver}); - print $form->parse_html_template("admin/dbupgrade_header"); $form->{dbupdate} = $form->{dbname}; $form->{$form->{dbname}} = 1; User->dbupdate($form); - User->dbupdate2($form, $controls); + User->dbupdate2($form, SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls); print $form->parse_html_template("admin/dbupgrade_footer"); }