X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUpgrade2.pm;h=83210316c795ea4e718330138172f40cc73bd930;hb=4486e3bc8eb00c37cf8029e663eb94b4b9c5346a;hp=08fdd874abcad9f7f378167dfd928c15e913ac20;hpb=dbcd72edf94df04115f1844768a92a194289c5f3;p=kivitendo-erp.git diff --git a/SL/DBUpgrade2.pm b/SL/DBUpgrade2.pm index 08fdd874a..83210316c 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; @@ -22,13 +24,18 @@ sub init { } $params{path_suffix} ||= ''; - $params{schame} ||= ''; + $params{schema} ||= ''; + $params{path} = "sql/" . $params{dbdriver} . "-upgrade2" . $params{path_suffix}; map { $self->{$_} = $params{$_} } keys %params; return $self; } +sub path { + $_[0]{path}; +} + sub parse_dbupdate_controls { $::lxdebug->enter_sub(); @@ -40,7 +47,7 @@ sub parse_dbupdate_controls { local *IN; my %all_controls; - my $path = "sql/" . $self->{dbdriver} . "-upgrade2" . $self->{path_suffix}; + my $path = $self->path; foreach my $file_name (<$path/*.sql>, <$path/*.pl>) { next unless (open(IN, $file_name)); @@ -72,7 +79,7 @@ sub parse_dbupdate_controls { next if ($control->{ignore}); - $control->{charset} ||= Common::DEFAULT_CHARSET; + $control->{charset} = $control->{charset} || $control->{encoding} || Common::DEFAULT_CHARSET; if (!$control->{"tag"}) { _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ; @@ -117,7 +124,7 @@ sub parse_dbupdate_controls { $::lxdebug->leave_sub(); - return \%all_controls; + return $self; } sub process_query { @@ -134,7 +141,7 @@ sub process_query { my $file_charset = Common::DEFAULT_CHARSET; while (<$fh>) { last if !/^--/; - next if !/^--\s*\@charset:\s*(.+)/; + next if !/^--\s*\@(?:charset|encoding):\s*(.+)/; $file_charset = $1; last; } @@ -160,13 +167,25 @@ sub process_query { if (@quote_chars) { if ($char eq $quote_chars[-1]) { pop(@quote_chars); + } elsif (length $quote_chars[-1] > 1 + && substr($_, $i, length $quote_chars[-1]) eq $quote_chars[-1]) { + $i += length($quote_chars[-1]) - 1; + $char = $quote_chars[-1]; + pop(@quote_chars); } $query .= $char; } else { + my ($tag, $tag_end); if (($char eq "'") || ($char eq "\"")) { push(@quote_chars, $char); + } elsif ($char eq '$' # start of dollar quoting + && ($tag_end = index($_, '$', $i + 1)) > -1 # ends on same line + && (do { $tag = substr($_, $i + 1, $tag_end - $i - 1); 1 }) # extract tag + && $tag =~ /^ (?= [A-Za-z_] [A-Za-z0-9_]* | ) $/x) { # tag is identifier + push @quote_chars, $char = '$' . $tag . '$'; + $i = $tag_end; } elsif ($char eq ";") { # Query is complete. Send it. @@ -231,7 +250,7 @@ sub process_perl_script { } else { while (<$fh>) { last if !/^--/; - next if !/^--\s*\@charset:\s*(.+)/; + next if !/^--\s*\@(?:charset|encoding):\s*(.+)/; $file_charset = $1; last; } @@ -243,7 +262,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(); @@ -271,7 +290,7 @@ 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)); } @@ -290,6 +309,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) = @_; @@ -343,7 +447,289 @@ sub _dbupdate2_calculate_depth { sub sort_dbupdate_controls { my $self = shift; + $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; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DBUpgrade2 - Parse database upgrade files stored in +C and C (and also in +C) + +=head1 SYNOPSIS + + use SL::User; + use SL::DBUpgrade2; + + # Apply outstanding updates to the authentication database + my $scripts = SL::DBUpgrade2->new( + form => $::form, + dbdriver => 'Pg', + auth => 1 + ); + $scripts->apply_admin_dbupgrade_scripts(1); + + # Apply updates to a user database + my $scripts = SL::DBUpgrade2->new( + form => $::form, + dbdriver => $::form->{dbdriver}, + auth => 1 + ); + User->dbupdate2($form, $scripts->parse_dbupdate_controls); + +=head1 OVERVIEW + +Database upgrade files are used to upgrade the database structure and +content of both the authentication database and the user +databases. They're applied when a user logs in. As long as the +authentication database is not up to date users cannot log in in +general, and the admin has to log in first in order to get his +database updated. + +Database scripts form a tree by specifying which upgrade file depends +on which other upgrade file. This means that such files are always +applied in a well-defined order. + +Each script is run in a separate transaction. If a script fails the +current transaction is rolled back and the whole upgrade process is +stopped. The user/admin is required to fix the issue manually. + +A list of applied upgrade scripts is maintained in a table called +C for the user database and C) for the +authentication database. They contain the tags, the login name of the +user having applied the script and the timestamp when the script was +applied. + +Database upgrade files come in two flavours: SQL files and Perl +files. For both there are control fields that determine the order in +which they're executed, what charset the scripts are written in +etc. The control fields are tag/value pairs contained in comments. + +=head1 OLD UPGRADE FILES + +The files in C are so old that I don't bother +documenting them. They're handled by this class, too, but new files +are only created as C files. + +=head1 CONTROL FIELDS + +=head2 SYNTAX + +Control fields for Perl files: + + # @tag1: value1 + # @tag2: some more values + sub do_stuff { + } + 1; + +Control fields for SQL files: + + -- @tag1: value1 + -- @tag2: some more values + ALTER TABLE ...; + +=head2 TAGS AND THEIR MEANING + +The following tags are recognized: + +=over 4 + +=item tag + +The name for this file. The C is also used for dependency +resolution (see C). + +This is mandatory. + +=item description + +A description presented to the user when the update is applied. + +This is mandatory. + +=item depends + +A space-separated list of tags of scripts this particular script +depends on. All other upgrades listed in C will be applied +before the current one is applied. + +=item charset +=item encoding + +The charset this file uses. Defaults to C if +missing. Both terms are recognized. + +=item priority + +Ordering the scripts by their dependencies alone produces a lot of +groups of scripts that could be applied at the same time (e.g. if both +B and C depend only on A then B could be applied before C or the other +way around). This field determines the order inside such a +group. Scripts with lower priority fields are executed before scripts +with higher priority fields. + +If two scripts have equal priorities then their tag name decides. + +The priority defaults to 1000. + +=back + +=head1 FUNCTIONS + +=over 4 + +=item C + +Applies all unapplied upgrade files to the authentication/admin +database. The parameter C<$called_from_admin> should be truish if the +function is called from the web interface and falsish if it's called +from e.g. a command line script like C. + +=item C + +Initializes the object. Is called directly from L and should not +be called again. + +=item C + +Creates a new object. Possible parameters are: + +=over 4 + +=item path + +Path to the upgrade files to parse. Required. + +=item form + +C object to use. Required. + +=item dbdriver + +Name of the database driver. Currently only C for PostgreSQL is +supported. + +=item auth + +Optional parameter defaulting to 0. If trueish then the scripts read +are the ones applying to the authentication database. + +=back + +=item C + +Parses all files located in C (see L), ananlyzes their +control fields, builds the tree, and signals errors if control fields +are missing/wrong (e.g. a tag name listed in C is not +found). Sets C<$Self->{all_controls}> to the list of database +scripts. + +=item C + +Applies a single database upgrade file. Calls L +for Perl update files and C for SQL update +files. Requires an open database handle(C<$dbh>), the file name +(C<$filename>), a hash structure of the file's control fields as +produced by L (C<$version_or_control>) and +the database charset (for on-the-fly charset recoding of the script if +required, C<$db_charset>). + +Returns the result of the actual function called. + +=item C + +Applies a single Perl database upgrade file. Requires an open database +handle(C<$dbh>), the file name (C<$filename>), a hash structure of the +file's control fields as produced by L +(C<$version_or_control>) and the database charset (for on-the-fly +charset recoding of the script if required, C<$db_charset>). + +Perl scripts are executed via L. If L returns falsish then +an error is expected. There are two special return values: If the +script returns C<1> then the update was successful. Return code C<2> +means "needs more interaction from the user; remove users/nologin and +end current upgrade process". All other return codes are fatal errors. + +Inside the Perl script several local variables exist that can be used: + +=over 4 + +=item $dbup_locale + +A locale object for translating messages + +=item $dbh + +The database handle (inside a transaction). + +=item $::form + +The global C object. + +=back + +A Perl script can actually implement queries that fail while +continueing the process by handling the transaction itself, e.g. with +the following function: + + sub do_query { + my ($query, $may_fail) = @_; + + if (!$dbh->do($query)) { + die($dbup_locale->text("Database update error:") . "
$msg
" . $DBI::errstr) unless $may_fail; + $dbh->rollback(); + $dbh->begin_work(); + } + } + +=item C + +Applies a single SQL database upgrade file. Requires an open database +handle(C<$dbh>), the file name (C<$filename>), a hash structure of the +ofile's control fields as produced by L +(C<$version_or_control>) and the database charset (for on-the-fly +charset recoding of the script if required, C<$db_charset>). + +=item C + +Sorts the database upgrade scripts according to their C and +C control fields. Returns a list of their hash +representations that can be applied in order. + +=item C + +Returns a list if upgrade scripts (their internal hash representation) +that haven't been applied to a database yet. C<$dbh> is an open handle +to the database that is checked. + +Requires that the scripts have been parsed. + +=item C + +Returns trueish if at least one upgrade script hasn't been applied to +a database yet. C<$dbh> is an open handle to the database that is +checked. + +Requires that the scripts have been parsed. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut