-sub dbupdate {
- $main::lxdebug->enter_sub();
-
- my ($self, $form) = @_;
-
- local *SQLDIR;
-
- my @upgradescripts = ();
- my $query;
- my $rc = -2;
-
- if ($form->{dbupdate}) {
-
- # read update scripts into memory
- opendir(SQLDIR, "sql/Pg-upgrade")
- or &error("", "sql/Pg-upgrade : $!");
- @upgradescripts =
- sort(cmp_script_version
- grep(/Pg-upgrade-.*?\.(sql|pl)$/,
- readdir(SQLDIR)));
- closedir(SQLDIR);
- }
-
- my $db_charset = $::lx_office_conf{system}->{dbcharset};
- $db_charset ||= Common::DEFAULT_CHARSET;
-
- my $dbupdater = SL::DBUpgrade2->new(form => $form);
-
- foreach my $db (split(/ /, $form->{dbupdate})) {
-
- next unless $form->{$db};
-
- # strip db from dataset
- $db =~ s/^db//;
- &dbconnect_vars($form, $db);
-
- my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
- or $form->dberror;
-
- $dbh->do($form->{dboptions}) if ($form->{dboptions});
-
- # check version
- $query = qq|SELECT version FROM defaults|;
- my ($version) = selectrow_query($form, $dbh, $query);
-
- next unless $version;
-
- $version = calc_version($version);
-
- foreach my $upgradescript (@upgradescripts) {
- my $a = $upgradescript;
- $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
-
- my ($mindb, $maxdb) = split /-/, $a;
- my $str_maxdb = $maxdb;
- $mindb = calc_version($mindb);
- $maxdb = calc_version($maxdb);
-
- next if ($version >= $maxdb);
-
- # if there is no upgrade script exit
- last if ($version < $mindb);
-
- # apply upgrade
- $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
- $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
-
- $version = $maxdb;
-
- }
-
- $rc = 0;
- $dbh->disconnect;
-
- }
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-