From 9b0c72696ee2208dcc756b74a55941b50fcb546f Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 30 Nov 2010 08:52:15 +0100 Subject: [PATCH] DRY --- SL/DBUpgrade2.pm | 16 ++++++++++++++++ SL/User.pm | 42 ++++++------------------------------------ 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index c9c742c21..929d59210 100644 --- a/SL/DBUpgrade2.pm +++ b/SL/DBUpgrade2.pm @@ -326,6 +326,22 @@ 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 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 _check_for_loops { my ($form, $file_name, $controls, $tag, @path) = @_; diff --git a/SL/User.pm b/SL/User.pm index e1ec6ef49..6edcbc603 100644 --- a/SL/User.pm +++ b/SL/User.pm @@ -647,59 +647,29 @@ sub dbupdate2 { $form->{sid} = $form->{dbdefault}; - my @upgradescripts = (); - my ($query, $sth, $tag); - my $rc = -2; - - @upgradescripts = $dbupdater->sort_dbupdate_controls; + my $rc = -2; + my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET; - my $db_charset = $main::dbcharset; - $db_charset ||= Common::DEFAULT_CHARSET; + map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} }; foreach my $db (split / /, $form->{dbupdate}) { - next unless $form->{$db}; # strip db from dataset $db =~ s/^db//; &dbconnect_vars($form, $db); - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; + my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) or $form->dberror; $dbh->do($form->{dboptions}) if ($form->{dboptions}); - map({ $_->{"applied"} = 0; } @upgradescripts); - $self->create_schema_info_table($form, $dbh); - $query = qq|SELECT tag FROM schema_info|; - $sth = $dbh->prepare($query); - $sth->execute() || $form->dberror($query); - while (($tag) = $sth->fetchrow_array()) { - $dbupdater->{all_controls}->{$tag}->{"applied"} = 1 if (defined($dbupdater->{all_controls}->{$tag})); - } - $sth->finish(); - - my $all_applied = 1; - foreach (@upgradescripts) { - if (!$_->{"applied"}) { - $all_applied = 0; - last; - } - } + my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh); - next if ($all_applied); + $dbh->disconnect and next if !@upgradescripts; foreach my $control (@upgradescripts) { - next if ($control->{"applied"}); - - $control->{description} = SL::Iconv::convert($control->{charset}, $db_charset, $control->{description}); - - $control->{"file"} =~ /\.(sql|pl)$/; - my $file_type = $1; - # apply upgrade $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}"); print $form->parse_html_template("dbupgrade/upgrade_message2", $control); -- 2.20.1