4   if (! -d "bin" || ! -d "SL") {
 
   5     print("This tool must be run from the Lx-Office ERP base directory.\n");
 
   9   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
 
  10   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
 
  13 use English '-no_match_vars';
 
  22 $lxdebug = LXDebug->new();
 
  36 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
 
  37 my ($opt_user, $opt_apply, $opt_applied, $opt_format, $opt_test_utf8);
 
  38 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword);
 
  40 our (%myconfig, $form, $user, $auth);
 
  43   my $help_text = <<"END_HELP"
 
  44 dbupgrade2_tool.pl [options]
 
  46   A validation and information tool for the database upgrade scripts
 
  47   in \'sql/Pg-upgrade2\'.
 
  49   At startup dbupgrade2_tool.pl will always check the consistency
 
  50   of all database upgrade scripts (e.g. circular references, invalid
 
  51   formats, missing meta information). You can but don\'t have to specifiy
 
  55     --list               Lists all database upgrade tags
 
  56     --tree               Lists all database upgrades in tree form
 
  57     --rtree              Lists all database upgrades in reverse tree form
 
  58     --graphviz[=file]    Create a Postscript document showing a tree of
 
  59                          all database upgrades and their dependencies.
 
  60                          If no file name is given then the output is
 
  61                          written to \'db_dependencies.png\'.
 
  62     --format=...         Format for the graphviz output. Defaults to
 
  63                          \'png\'. All values that the command \'dot\' accepts
 
  64                          for it\'s option \'-T\' are acceptable.
 
  65     --nodeps             List all database upgrades that no other upgrade
 
  67     --apply=tag          Applies the database upgrades \'tag\' and all
 
  68                          upgrades it depends on. If \'--apply\' is used
 
  69                          then the option \'--user\' must be used as well.
 
  70     --applied            List the applied database upgrades for the
 
  71                          database that the user given with \'--user\' uses.
 
  72     --test-utf8          Tests a PostgreSQL cluster for proper UTF-8 support.
 
  73                          You have to specify the database to test with the
 
  74                          parameters --dbname, --dbhost, --dbport, --dbuser
 
  76     --help               Show this help and exit.
 
  79     --user=name          The name of the user configuration to use for
 
  80                          database connectivity.
 
  81     --dbname=name        Database connection options for the UTF-8
 
  82     --dbhost=host        handling test.
 
  98 sub calc_rev_depends {
 
  99   map { $_->{rev_depends} = []; } values %{ $controls };
 
 101   foreach my $control (values %{ $controls }) {
 
 102     map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
 
 107   my @sorted_controls = sort_dbupdate_controls($controls);
 
 109   print "LIST VIEW\n\n" .
 
 110     "number tag depth priority\n";
 
 113   foreach (@sorted_controls) {
 
 114     print "$i $_->{tag} $_->{depth} $_->{priority}\n";
 
 122   my ($tag, $depth) = @_;
 
 124   print " " x $depth . $tag . "\n";
 
 126   foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
 
 127     dump_node($dep_tag, $depth + 1);
 
 132   print "TREE VIEW\n\n";
 
 136   my @sorted_controls = sort_dbupdate_controls($controls);
 
 138   foreach my $control (@sorted_controls) {
 
 139     dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
 
 145 sub dump_node_reverse {
 
 146   my ($tag, $depth) = @_;
 
 148   print " " x $depth . $tag . "\n";
 
 150   foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
 
 151     dump_node_reverse($dep_tag, $depth + 1);
 
 155 sub dump_tree_reverse {
 
 156   print "REVERSE TREE VIEW\n\n";
 
 160   my @sorted_controls = sort_dbupdate_controls($controls);
 
 162   foreach my $control (@sorted_controls) {
 
 163     last if ($control->{depth} > 1);
 
 164     dump_node_reverse($control->{tag}, "");
 
 173   my $format    = $params{format}    || "png";
 
 174   my $file_name = $params{file_name} || "db_dependencies.${format}";
 
 176   print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
 
 177   print "Output will be written to '${file_name}'\n";
 
 181   $dot = "|dot -T${format} ";
 
 182   open OUT, "${dot}> \"${file_name}\"" || die;
 
 185     "digraph db_dependencies {\n" .
 
 186     "graph [size=\"16.53,11.69!\"];\n" .
 
 187     "node [shape=box style=filled fillcolor=white];\n";
 
 190   foreach my $c (values %{ $controls }) {
 
 191     $ranks{$c->{depth}} ||= [];
 
 193     my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if (!scalar @{ $c->{rev_depends} });
 
 195     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 198   foreach (sort keys %ranks) {
 
 199     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 202   foreach my $c (values %{ $controls }) {
 
 203     print OUT "$c->{tag};\n";
 
 205     foreach my $d (@{ $c->{depends} }) {
 
 206       print OUT "$c->{tag} -> $d;\n";
 
 217   print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 218     join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
 
 225   my (@order, %tags, @all_tags);
 
 227   if ($name eq "ALL") {
 
 229     @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
 
 232     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 236   foreach my $tag (@all_tags) {
 
 237     build_upgrade_order($tag, \@order, \%tags);
 
 240   my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
 
 242   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 244   $dbh->{PrintWarn}  = 0;
 
 245   $dbh->{PrintError} = 0;
 
 247   $user->create_schema_info_table($form, $dbh);
 
 249   my $query = qq|SELECT tag FROM schema_info|;
 
 250   $sth = $dbh->prepare($query);
 
 251   $sth->execute() || $form->dberror($query);
 
 252   while (($tag) = $sth->fetchrow_array()) {
 
 253     $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
 
 257   @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
 
 258   if (!@upgradescripts) {
 
 259     print "The upgrade has already been applied.\n";
 
 263   foreach my $control (@upgradescripts) {
 
 264     $control->{file} =~ /\.(sql|pl)$/;
 
 268     print "Applying upgrade $control->{file}\n";
 
 270     if ($file_type eq "sql") {
 
 271       $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 273       $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 280 sub dump_sql_result {
 
 281   my ($results, $column_order) = @_;
 
 283   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 285   foreach my $row (@{ $results }) {
 
 286     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 290   if ($column_order && scalar @{ $column_order }) {
 
 291     @sorted_names = @{ $column_order };
 
 293     @sorted_names = sort keys %column_lengths;
 
 296   my $format       = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
 
 298   printf $format, @sorted_names;
 
 299   print  join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
 
 301   foreach my $row (@{ $results }) {
 
 302     printf $format, map { $row->{$_} } @sorted_names;
 
 304   printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
 
 310   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 312   $dbh->{PrintWarn}  = 0;
 
 313   $dbh->{PrintError} = 0;
 
 315   $user->create_schema_info_table($form, $dbh);
 
 317   my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
 
 318   $sth = $dbh->prepare($query);
 
 319   $sth->execute() || $form->dberror($query);
 
 320   while (my $ref = $sth->fetchrow_hashref()) {
 
 327   if (!scalar @results) {
 
 328     print "No database upgrades have been applied yet.\n";
 
 330     dump_sql_result(\@results, [qw(tag login itime)]);
 
 334 sub build_upgrade_order {
 
 339   my $control = $controls->{$name};
 
 341   foreach my $dependency (@{ $control->{depends} }) {
 
 342     next if $tags->{$dependency};
 
 343     $tags->{$dependency} = 1;
 
 344     build_upgrade_order($dependency, $order, $tag);
 
 347   push @{ $order }, $name;
 
 355 eval { require "config/lx-erp.conf"; };
 
 356 eval { require "config/lx-erp-local.conf"; } if (-f "config/lx-erp-local.conf");
 
 359 $locale = Locale->new("de");
 
 365 GetOptions("list"         => \$opt_list,
 
 366            "tree"         => \$opt_tree,
 
 367            "rtree"        => \$opt_rtree,
 
 368            "nodeps"       => \$opt_nodeps,
 
 369            "graphviz:s"   => \$opt_graphviz,
 
 370            "format:s"     => \$opt_format,
 
 371            "user=s"       => \$opt_user,
 
 372            "apply=s"      => \$opt_apply,
 
 373            "applied"      => \$opt_applied,
 
 374            "test-utf8"    => \$opt_test_utf8,
 
 375            "dbhost:s"     => \$opt_dbhost,
 
 376            "dbport:s"     => \$opt_dbport,
 
 377            "dbname:s"     => \$opt_dbname,
 
 378            "dbuser:s"     => \$opt_dbuser,
 
 379            "dbpassword:s" => \$opt_dbpassword,
 
 380            "help"         => \$opt_help,
 
 383 show_help() if ($opt_help);
 
 385 $controls = parse_dbupdate_controls($form, "Pg");
 
 387 dump_list()                                 if ($opt_list);
 
 388 dump_tree()                                 if ($opt_tree);
 
 389 dump_tree_reverse()                         if ($opt_rtree);
 
 390 dump_graphviz('file_name' => $opt_graphviz,
 
 391               'format'    => $opt_format)   if (defined $opt_graphviz);
 
 392 dump_nodeps()                               if ($opt_nodeps);
 
 395   $auth = SL::Auth->new();
 
 396   if (!$auth->session_tables_present()) {
 
 397     $form->error("The session and user management tables are not present in the " .
 
 398                  "authentication database. Please use the administration web interface " .
 
 399                  "and to create them.");
 
 402   %myconfig = $auth->read_user($opt_user);
 
 404   if (!$myconfig{login}) {
 
 405     $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
 
 408   $locale = new Locale($myconfig{countrycode}, "all");
 
 409   $user   = new User($opt_user);
 
 411   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 415   $form->error("--apply used but no user name given with --user.") if (!$user);
 
 416   apply_upgrade($opt_apply);
 
 420   $form->error("--applied used but no user name given with --user.") if (!$user);
 
 424 if ($opt_test_utf8) {
 
 425   $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
 
 427   my $iconv_to_utf8      = Text::Iconv->new("ISO-8859-15", "UTF-8");
 
 428   my $iconv_from_utf8    = Text::Iconv->new("UTF-8", "ISO-8859-15");
 
 430   my $umlaut_upper       = 'Ä';
 
 431   my $umlaut_upper_utf8  = $iconv_to_utf8->convert($umlaut_upper);
 
 433   my $dbconnect          = "dbi:Pg:dbname=${opt_dbname}";
 
 434   $dbconnect            .= ";host=${opt_dbhost}" if ($opt_dbhost);
 
 435   $dbconnect            .= ";port=${opt_dbport}" if ($opt_dbport);
 
 437   my $dbh                = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword);
 
 439   $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
 
 441   my ($umlaut_lower_utf8) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper_utf8);
 
 445   my $umlaut_lower = $iconv_from_utf8->convert($umlaut_lower_utf8);
 
 447   if ($umlaut_lower eq 'ä') {
 
 448     print "UTF-8 test was successful.\n";
 
 449   } elsif ($umlaut_lower eq 'Ä') {
 
 450     print "UTF-8 test was NOT successful: Umlauts are not modified (this might be partially ok, but you should probably not use UTF-8 on this cluster).\n";
 
 452     print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";