kivitendo/SL/DBUpgrade2.pm @ 38a2e789
4fd8bdbf | Moritz Bunkus | package SL::DBUpgrade2;
|
||
a1d1605e | Moritz Bunkus | use IO::File;
|
||
38a2e789 | Moritz Bunkus | use List::MoreUtils qw(any);
|
||
a1d1605e | Moritz Bunkus | |||
faef45c2 | Moritz Bunkus | use SL::Common;
|
||
a1d1605e | Moritz Bunkus | use SL::Iconv;
|
||
faef45c2 | Moritz Bunkus | |||
76c486e3 | Sven Schöling | use strict;
|
||
4fd8bdbf | Moritz Bunkus | |||
7e0ad798 | Moritz Bunkus | sub new {
|
||
0b6cb3b8 | Moritz Bunkus | my $package = shift;
|
||
return bless({}, $package)->init(@_);
|
||||
7e0ad798 | Moritz Bunkus | }
|
||
0b6cb3b8 | Moritz Bunkus | sub init {
|
||
my ($self, %params) = @_;
|
||||
d0fb3d4d | Moritz Bunkus | if ($params{auth}) {
|
||
$params{path_suffix} = "-auth";
|
||||
$params{schema} = "auth.";
|
||||
}
|
||||
$params{path_suffix} ||= '';
|
||||
$params{schame} ||= '';
|
||||
0b6cb3b8 | Moritz Bunkus | map { $self->{$_} = $params{$_} } keys %params;
|
||
a1d1605e | Moritz Bunkus | return $self;
|
||
}
|
||||
4fd8bdbf | Moritz Bunkus | sub parse_dbupdate_controls {
|
||
dbcd72ed | Moritz Bunkus | $::lxdebug->enter_sub();
|
||
4fd8bdbf | Moritz Bunkus | |||
7e0ad798 | Moritz Bunkus | my ($self) = @_;
|
||
4fd8bdbf | Moritz Bunkus | |||
7e0ad798 | Moritz Bunkus | my $form = $self->{form};
|
||
dbcd72ed | Moritz Bunkus | my $locale = $::locale;
|
||
4fd8bdbf | Moritz Bunkus | |||
local *IN;
|
||||
my %all_controls;
|
||||
d0fb3d4d | Moritz Bunkus | my $path = "sql/" . $self->{dbdriver} . "-upgrade2" . $self->{path_suffix};
|
||
4fd8bdbf | Moritz Bunkus | |||
foreach my $file_name (<$path/*.sql>, <$path/*.pl>) {
|
||||
next unless (open(IN, $file_name));
|
||||
my $file = $file_name;
|
||||
$file =~ s|.*/||;
|
||||
my $control = {
|
||||
"priority" => 1000,
|
||||
53c7990b | Moritz Bunkus | "depends" => [],
|
||
4fd8bdbf | Moritz Bunkus | };
|
||
while (<IN>) {
|
||||
chomp();
|
||||
next unless (/^(--|\#)\s*\@/);
|
||||
s/^(--|\#)\s*\@//;
|
||||
s/\s*$//;
|
||||
next if ($_ eq "");
|
||||
my @fields = split(/\s*:\s*/, $_, 2);
|
||||
next unless (scalar(@fields) == 2);
|
||||
if ($fields[0] eq "depends") {
|
||||
push(@{$control->{"depends"}}, split(/\s+/, $fields[1]));
|
||||
} else {
|
||||
$control->{$fields[0]} = $fields[1];
|
||||
}
|
||||
}
|
||||
91836534 | Moritz Bunkus | next if ($control->{ignore});
|
||
faef45c2 | Moritz Bunkus | $control->{charset} ||= Common::DEFAULT_CHARSET;
|
||
53c7990b | Moritz Bunkus | if (!$control->{"tag"}) {
|
||
_control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ;
|
||||
}
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/) {
|
||
_control_error($form, $file_name, $locale->text("The 'tag' field must only consist of alphanumeric characters or the carachters - _ ( )"))
|
||||
}
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | if (defined($all_controls{$control->{"tag"}})) {
|
||
_control_error($form, $file_name, sprintf($locale->text("More than one control file with the tag '%s' exist."), $control->{"tag"}))
|
||||
}
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | if (!$control->{"description"}) {
|
||
_control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ;
|
||||
}
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | $control->{"priority"} *= 1;
|
||
$control->{"priority"} ||= 1000;
|
||||
$control->{"file"} = $file;
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | delete @{$control}{qw(depth applied)};
|
||
4fd8bdbf | Moritz Bunkus | |||
$all_controls{$control->{"tag"}} = $control;
|
||||
close(IN);
|
||||
}
|
||||
foreach my $control (values(%all_controls)) {
|
||||
foreach my $dependency (@{$control->{"depends"}}) {
|
||||
53c7990b | Moritz Bunkus | _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency}));
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
map({ $_->{"loop"} = 0; } values(%all_controls));
|
||||
53c7990b | Moritz Bunkus | _check_for_loops($form, $control->{"file"}, \%all_controls, $control->{"tag"});
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
|
||||
values(%all_controls));
|
||||
7e0ad798 | Moritz Bunkus | $self->{all_controls} = \%all_controls;
|
||
dbcd72ed | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
4fd8bdbf | Moritz Bunkus | |||
38a2e789 | Moritz Bunkus | return $self;
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
a1d1605e | Moritz Bunkus | sub process_query {
|
||
dbcd72ed | Moritz Bunkus | $::lxdebug->enter_sub();
|
||
a1d1605e | Moritz Bunkus | |||
my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
|
||||
my $form = $self->{form};
|
||||
my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
|
||||
my $query = "";
|
||||
my $sth;
|
||||
my @quote_chars;
|
||||
my $file_charset = Common::DEFAULT_CHARSET;
|
||||
while (<$fh>) {
|
||||
last if !/^--/;
|
||||
next if !/^--\s*\@charset:\s*(.+)/;
|
||||
$file_charset = $1;
|
||||
last;
|
||||
}
|
||||
$fh->seek(0, SEEK_SET);
|
||||
$db_charset ||= Common::DEFAULT_CHARSET;
|
||||
$dbh->begin_work();
|
||||
while (<$fh>) {
|
||||
$_ = SL::Iconv::convert($file_charset, $db_charset, $_);
|
||||
# Remove DOS and Unix style line endings.
|
||||
chomp;
|
||||
# remove comments
|
||||
s/--.*$//;
|
||||
for (my $i = 0; $i < length($_); $i++) {
|
||||
my $char = substr($_, $i, 1);
|
||||
# Are we inside a string?
|
||||
if (@quote_chars) {
|
||||
if ($char eq $quote_chars[-1]) {
|
||||
pop(@quote_chars);
|
||||
}
|
||||
$query .= $char;
|
||||
} else {
|
||||
if (($char eq "'") || ($char eq "\"")) {
|
||||
push(@quote_chars, $char);
|
||||
} elsif ($char eq ";") {
|
||||
# Query is complete. Send it.
|
||||
$sth = $dbh->prepare($query);
|
||||
if (!$sth->execute()) {
|
||||
my $errstr = $dbh->errstr;
|
||||
$sth->finish();
|
||||
$dbh->rollback();
|
||||
$form->dberror("The database update/creation did not succeed. " .
|
||||
"The file ${filename} containing the following " .
|
||||
"query failed:<br>${query}<br>" .
|
||||
"The error message was: ${errstr}<br>" .
|
||||
"All changes in that file have been reverted.");
|
||||
}
|
||||
$sth->finish();
|
||||
$char = "";
|
||||
$query = "";
|
||||
}
|
||||
$query .= $char;
|
||||
}
|
||||
}
|
||||
# Insert a space at the end of each line so that queries split
|
||||
# over multiple lines work properly.
|
||||
if ($query ne '') {
|
||||
$query .= @quote_chars ? "\n" : ' ';
|
||||
}
|
||||
}
|
||||
if (ref($version_or_control) eq "HASH") {
|
||||
d0fb3d4d | Moritz Bunkus | $dbh->do("INSERT INTO " . $self->{schema} . "schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
|
||
a1d1605e | Moritz Bunkus | } elsif ($version_or_control) {
|
||
d0fb3d4d | Moritz Bunkus | $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control));
|
||
a1d1605e | Moritz Bunkus | }
|
||
$dbh->commit();
|
||||
$fh->close();
|
||||
dbcd72ed | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
a1d1605e | Moritz Bunkus | }
|
||
8b39e389 | Moritz Bunkus | # Process a Perl script which updates the database.
|
||
# If the script returns 1 then the update was successful.
|
||||
# Return code "2" means "needs more interaction; remove
|
||||
# users/nologin and end current request".
|
||||
# All other return codes are fatal errors.
|
||||
sub process_perl_script {
|
||||
dbcd72ed | Moritz Bunkus | $::lxdebug->enter_sub();
|
||
8b39e389 | Moritz Bunkus | |||
my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
|
||||
my $form = $self->{form};
|
||||
my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
|
||||
my $file_charset = Common::DEFAULT_CHARSET;
|
||||
if (ref($version_or_control) eq "HASH") {
|
||||
$file_charset = $version_or_control->{charset};
|
||||
} else {
|
||||
while (<$fh>) {
|
||||
last if !/^--/;
|
||||
next if !/^--\s*\@charset:\s*(.+)/;
|
||||
$file_charset = $1;
|
||||
last;
|
||||
}
|
||||
$fh->seek(0, SEEK_SET);
|
||||
}
|
||||
my $contents = join "", <$fh>;
|
||||
$fh->close();
|
||||
$db_charset ||= Common::DEFAULT_CHARSET;
|
||||
my $iconv = SL::Iconv::get_converter($file_charset, $db_charset);
|
||||
$dbh->begin_work();
|
||||
# setup dbup_ export vars
|
||||
my %dbup_myconfig = ();
|
||||
map({ $dbup_myconfig{$_} = $form->{$_}; } qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
|
||||
my $dbup_locale = $::locale;
|
||||
my $result = eval($contents);
|
||||
if (1 != $result) {
|
||||
$dbh->rollback();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
if (!defined($result)) {
|
||||
print $form->parse_html_template("dbupgrade/error",
|
||||
{ "file" => $filename,
|
||||
"error" => $@ });
|
||||
::end_of_request();
|
||||
} elsif (1 != $result) {
|
||||
unlink("users/nologin") if (2 == $result);
|
||||
::end_of_request();
|
||||
}
|
||||
if (ref($version_or_control) eq "HASH") {
|
||||
d0fb3d4d | Moritz Bunkus | $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
|
||
8b39e389 | Moritz Bunkus | } elsif ($version_or_control) {
|
||
d0fb3d4d | Moritz Bunkus | $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control));
|
||
8b39e389 | Moritz Bunkus | }
|
||
$dbh->commit();
|
||||
dbcd72ed | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
8b39e389 | Moritz Bunkus | }
|
||
35636cc2 | Moritz Bunkus | sub process_file {
|
||
my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
|
||||
if ($filename =~ m/sql$/) {
|
||||
$self->process_query($dbh, $filename, $version_or_control, $db_charset);
|
||||
} else {
|
||||
$self->process_perl_script($dbh, $filename, $version_or_control, $db_charset);
|
||||
}
|
||||
}
|
||||
38a2e789 | Moritz Bunkus | 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;
|
||||
}
|
||||
4fd8bdbf | Moritz Bunkus | sub _check_for_loops {
|
||
my ($form, $file_name, $controls, $tag, @path) = @_;
|
||||
push(@path, $tag);
|
||||
ad876674 | Moritz Bunkus | my $ctrl = $controls->{$tag};
|
||
if ($ctrl->{"loop"} == 1) {
|
||||
# Not done yet.
|
||||
dbcd72ed | Moritz Bunkus | _control_error($form, $file_name, $::locale->text("Dependency loop detected:") . " " . join(" -> ", @path))
|
||
53c7990b | Moritz Bunkus | |||
ad876674 | Moritz Bunkus | } elsif ($ctrl->{"loop"} == 0) {
|
||
# Not checked yet.
|
||||
$ctrl->{"loop"} = 1;
|
||||
53c7990b | Moritz Bunkus | map({ _check_for_loops($form, $file_name, $controls, $_, @path); } @{ $ctrl->{"depends"} });
|
||
ad876674 | Moritz Bunkus | $ctrl->{"loop"} = 2;
|
||
}
|
||||
4fd8bdbf | Moritz Bunkus | }
|
||
sub _control_error {
|
||||
my ($form, $file_name, $message) = @_;
|
||||
dbcd72ed | Moritz Bunkus | $form = $::form;
|
||
my $locale = $::locale;
|
||||
4fd8bdbf | Moritz Bunkus | |||
53c7990b | Moritz Bunkus | $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message));
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
sub _dbupdate2_calculate_depth {
|
||||
dbcd72ed | Moritz Bunkus | $::lxdebug->enter_sub(2);
|
||
4fd8bdbf | Moritz Bunkus | |||
my ($tree, $tag) = @_;
|
||||
my $node = $tree->{$tag};
|
||||
dbcd72ed | Moritz Bunkus | return $::lxdebug->leave_sub(2) if (defined($node->{"depth"}));
|
||
4fd8bdbf | Moritz Bunkus | |||
my $max_depth = 0;
|
||||
foreach $tag (@{$node->{"depends"}}) {
|
||||
_dbupdate2_calculate_depth($tree, $tag);
|
||||
my $value = $tree->{$tag}->{"depth"};
|
||||
$max_depth = $value if ($value > $max_depth);
|
||||
}
|
||||
$node->{"depth"} = $max_depth + 1;
|
||||
dbcd72ed | Moritz Bunkus | $::lxdebug->leave_sub(2);
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
sub sort_dbupdate_controls {
|
||||
7e0ad798 | Moritz Bunkus | my $self = shift;
|
||
dbcd72ed | Moritz Bunkus | return sort { ($a->{depth} <=> $b->{depth}) || ($a->{priority} <=> $b->{priority}) || ($a->{tag} cmp $b->{tag}) } values %{ $self->{all_controls} };
|
||
4fd8bdbf | Moritz Bunkus | }
|
||
1;
|