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.
 
  18 use English '-no_match_vars';
 
  29 SL::LxOfficeConf->read;
 
  30 our $lxdebug = LXDebug->new();
 
  44 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
 
  45 my ($opt_user, $opt_apply, $opt_applied, $opt_unapplied, $opt_format, $opt_test_utf8);
 
  46 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword, $opt_create, $opt_type);
 
  47 my ($opt_description, $opt_encoding, @opt_depends);
 
  49 our (%myconfig, $form, $user, $auth, $locale, $controls, $dbupgrader);
 
  52   my $help_text = <<"END_HELP"
 
  53 dbupgrade2_tool.pl [options]
 
  55   A validation and information tool for the database upgrade scripts
 
  56   in \'sql/Pg-upgrade2\'.
 
  58   At startup dbupgrade2_tool.pl will always check the consistency
 
  59   of all database upgrade scripts (e.g. circular references, invalid
 
  60   formats, missing meta information). You can but don\'t have to specifiy
 
  64     --list               Lists all database upgrade tags
 
  65     --tree               Lists all database upgrades in tree form
 
  66     --rtree              Lists all database upgrades in reverse tree form
 
  67     --graphviz[=file]    Create a Postscript document showing a tree of
 
  68                          all database upgrades and their dependencies.
 
  69                          If no file name is given then the output is
 
  70                          written to \'db_dependencies.png\'.
 
  71     --format=...         Format for the graphviz output. Defaults to
 
  72                          \'png\'. All values that the command \'dot\' accepts
 
  73                          for it\'s option \'-T\' are acceptable.
 
  74     --nodeps             List all database upgrades that no other upgrade
 
  76     --apply=tag          Applies the database upgrades \'tag\' and all
 
  77                          upgrades it depends on. If \'--apply\' is used
 
  78                          then the option \'--user\' must be used as well.
 
  79     --applied            List the applied database upgrades for the
 
  80                          database that the user given with \'--user\' uses.
 
  81     --unapplied          List the database upgrades that haven\'t been applied
 
  82                          yet to the database that the user given with
 
  84     --test-utf8          Tests a PostgreSQL cluster for proper UTF-8 support.
 
  85                          You have to specify the database to test with the
 
  86                          parameters --dbname, --dbhost, --dbport, --dbuser
 
  88     --help               Show this help and exit.
 
  91     --user=name          The name of the user configuration to use for
 
  92                          database connectivity.
 
  93     --dbname=name        Database connection options for the UTF-8
 
  94     --dbhost=host        handling test.
 
 110 sub calc_rev_depends {
 
 111   map { $_->{rev_depends} = []; } values %{ $controls };
 
 113   foreach my $control (values %{ $controls }) {
 
 114     map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
 
 119   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 121   print "LIST VIEW\n\n" .
 
 122     "number tag depth priority\n";
 
 125   foreach (@sorted_controls) {
 
 126     print "$i $_->{tag} $_->{depth} $_->{priority}\n";
 
 134   my ($tag, $depth) = @_;
 
 136   print " " x $depth . $tag . "\n";
 
 138   foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
 
 139     dump_node($dep_tag, $depth + 1);
 
 144   print "TREE VIEW\n\n";
 
 148   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 150   foreach my $control (@sorted_controls) {
 
 151     dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
 
 157 sub dump_node_reverse {
 
 158   my ($tag, $depth) = @_;
 
 160   print " " x $depth . $tag . "\n";
 
 162   foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
 
 163     dump_node_reverse($dep_tag, $depth + 1);
 
 167 sub dump_tree_reverse {
 
 168   print "REVERSE TREE VIEW\n\n";
 
 172   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 174   foreach my $control (@sorted_controls) {
 
 175     last if ($control->{depth} > 1);
 
 176     dump_node_reverse($control->{tag}, "");
 
 185   my $format    = $params{format}    || "png";
 
 186   my $file_name = $params{file_name} || "db_dependencies.${format}";
 
 188   print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
 
 189   print "Output will be written to '${file_name}'\n";
 
 193   my $dot = "|dot -T${format} ";
 
 194   open OUT, "${dot}> \"${file_name}\"" || die;
 
 197     "digraph db_dependencies {\n" .
 
 198     "graph [size=\"16.53,11.69!\"];\n" .
 
 199     "node [shape=box style=filled fillcolor=white];\n";
 
 202   foreach my $c (values %{ $controls }) {
 
 203     $ranks{$c->{depth}} ||= [];
 
 205     my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
 
 206       (map "node [fillcolor=$_] ", qw(lightgray white));
 
 208     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 211   foreach (sort keys %ranks) {
 
 212     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 215   foreach my $c (values %{ $controls }) {
 
 216     print OUT qq|"$c->{tag}";\n|;
 
 218     foreach my $d (@{ $c->{depends} }) {
 
 219       print OUT qq|"$c->{tag}" -> "$d";\n|;
 
 230   print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 231     join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
 
 238   my $filename    = $params{filename};
 
 239   my $dbupgrader  = $params{dbupgrader};
 
 240   my $type        = $params{type}        || '';
 
 241   my $description = $params{description} || '';
 
 242   my $encoding    = $params{encoding}    || 'utf-8';
 
 243   my @depends     = @{ $params{depends} };
 
 246     my @releases = grep { /^release_/ } keys %$controls;
 
 247     @depends = ((sort @releases)[-1]);
 
 251   if ($type eq 'sql') {
 
 253   } elsif ($type eq 'pl') {
 
 256     die 'Error: No --type was given but is required for --create.';
 
 258     die 'Error: Unknown --type. Try "sql" or "pl".';
 
 261   my $full_filename = $dbupgrader->path . '/' . $filename . '.' . $type;
 
 263   die "file '$full_filename' already exists, aborting" if -f $full_filename;
 
 266   open my $fh, ">:encoding($encoding)", $full_filename or die "can't open $full_filename";
 
 267   print $fh "$comment \@tag: $filename\n";
 
 268   print $fh "$comment \@description: $description\n";
 
 269   print $fh "$comment \@depends: @depends\n";
 
 270   print $fh "$comment \@encoding: $encoding\n";
 
 273   system("\$EDITOR $full_filename");
 
 280   my (@order, %tags, @all_tags);
 
 282   if ($name eq "ALL") {
 
 284     @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
 
 287     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 291   foreach my $tag (@all_tags) {
 
 292     build_upgrade_order($tag, \@order, \%tags);
 
 295   my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
 
 297   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 299   $dbh->{PrintWarn}  = 0;
 
 300   $dbh->{PrintError} = 0;
 
 302   $user->create_schema_info_table($form, $dbh);
 
 304   my $query = qq|SELECT tag FROM schema_info|;
 
 305   my $sth = $dbh->prepare($query);
 
 306   $sth->execute() || $form->dberror($query);
 
 307   while (my ($tag) = $sth->fetchrow_array()) {
 
 308     $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
 
 312   @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
 
 313   if (!@upgradescripts) {
 
 314     print "The upgrade has already been applied.\n";
 
 318   foreach my $control (@upgradescripts) {
 
 319     $control->{file} =~ /\.(sql|pl)$/;
 
 323     print "Applying upgrade $control->{file}\n";
 
 325     if ($file_type eq "sql") {
 
 326       $dbupgrader->process_query($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 328       $dbupgrader->process_perl_script($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 335 sub dump_sql_result {
 
 336   my ($results, $column_order) = @_;
 
 338   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 340   foreach my $row (@{ $results }) {
 
 341     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 345   if ($column_order && scalar @{ $column_order }) {
 
 346     @sorted_names = @{ $column_order };
 
 348     @sorted_names = sort keys %column_lengths;
 
 351   my $format       = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
 
 353   printf $format, @sorted_names;
 
 354   print  join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
 
 356   foreach my $row (@{ $results }) {
 
 357     printf $format, map { $row->{$_} } @sorted_names;
 
 359   printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
 
 365   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 367   $dbh->{PrintWarn}  = 0;
 
 368   $dbh->{PrintError} = 0;
 
 370   $user->create_schema_info_table($form, $dbh);
 
 372   my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
 
 373   my $sth = $dbh->prepare($query);
 
 374   $sth->execute() || $form->dberror($query);
 
 375   while (my $ref = $sth->fetchrow_hashref()) {
 
 382   if (!scalar @results) {
 
 383     print "No database upgrades have been applied yet.\n";
 
 385     dump_sql_result(\@results, [qw(tag login itime)]);
 
 392   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 394   $dbh->{PrintWarn}  = 0;
 
 395   $dbh->{PrintError} = 0;
 
 397   my @unapplied = $dbupgrader->unapplied_upgrade_scripts($dbh);
 
 401   if (!scalar @unapplied) {
 
 402     print "All database upgrades have been applied.\n";
 
 404     print map { $_->{tag} . "\n" } @unapplied;
 
 408 sub build_upgrade_order {
 
 413   my $control = $controls->{$name};
 
 415   foreach my $dependency (@{ $control->{depends} }) {
 
 416     next if $tags->{$dependency};
 
 417     $tags->{$dependency} = 1;
 
 418     build_upgrade_order($dependency, $order, $tags);
 
 421   push @{ $order }, $name;
 
 429 $locale = Locale->new;
 
 436 GetOptions("list"         => \$opt_list,
 
 437            "tree"         => \$opt_tree,
 
 438            "rtree"        => \$opt_rtree,
 
 439            "nodeps"       => \$opt_nodeps,
 
 440            "graphviz:s"   => \$opt_graphviz,
 
 441            "format:s"     => \$opt_format,
 
 442            "user=s"       => \$opt_user,
 
 443            "apply=s"      => \$opt_apply,
 
 444            "applied"      => \$opt_applied,
 
 445            "create=s"     => \$opt_create,
 
 446            "type=s"       => \$opt_type,
 
 447            "encoding=s"   => \$opt_encoding,
 
 448            "description=s" => \$opt_description,
 
 449            "depends=s"    => \@opt_depends,
 
 450            "unapplied"    => \$opt_unapplied,
 
 451            "test-utf8"    => \$opt_test_utf8,
 
 452            "dbhost:s"     => \$opt_dbhost,
 
 453            "dbport:s"     => \$opt_dbport,
 
 454            "dbname:s"     => \$opt_dbname,
 
 455            "dbuser:s"     => \$opt_dbuser,
 
 456            "dbpassword:s" => \$opt_dbpassword,
 
 457            "help"         => \$opt_help,
 
 460 show_help() if ($opt_help);
 
 462 $dbupgrader = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg');
 
 463 $controls   = $dbupgrader->parse_dbupdate_controls->{all_controls};
 
 465 dump_list()                                 if ($opt_list);
 
 466 dump_tree()                                 if ($opt_tree);
 
 467 dump_tree_reverse()                         if ($opt_rtree);
 
 468 dump_graphviz('file_name' => $opt_graphviz,
 
 469               'format'    => $opt_format)   if (defined $opt_graphviz);
 
 470 dump_nodeps()                               if ($opt_nodeps);
 
 471 create_upgrade(filename   => $opt_create,
 
 472                dbupgrader  => $dbupgrader,
 
 474                description => $opt_description,
 
 475                encoding    => $opt_encoding,
 
 476                depends     => \@opt_depends) if ($opt_create);
 
 479   $auth = SL::Auth->new();
 
 480   if (!$auth->session_tables_present()) {
 
 481     $form->error("The session and user management tables are not present in the " .
 
 482                  "authentication database. Please use the administration web interface " .
 
 483                  "and to create them.");
 
 486   %myconfig = $auth->read_user(login => $opt_user);
 
 488   if (!$myconfig{login}) {
 
 489     $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
 
 492   $locale = new Locale($myconfig{countrycode}, "all");
 
 493   $user   = new User(login => $opt_user);
 
 495   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 499   $form->error("--apply used but no user name given with --user.") if (!$user);
 
 500   apply_upgrade($opt_apply);
 
 504   $form->error("--applied used but no user name given with --user.") if (!$user);
 
 508 if ($opt_unapplied) {
 
 509   $form->error("--unapplied used but no user name given with --user.") if (!$user);
 
 514 if ($opt_test_utf8) {
 
 515   $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
 
 517   my $umlaut_upper       = 'Ä';
 
 519   my $dbconnect          = "dbi:Pg:dbname=${opt_dbname}";
 
 520   $dbconnect            .= ";host=${opt_dbhost}" if ($opt_dbhost);
 
 521   $dbconnect            .= ";port=${opt_dbport}" if ($opt_dbport);
 
 523   my $dbh                = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
 
 525   $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
 
 527   my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
 
 531   if ($umlaut_lower eq 'ä') {
 
 532     print "UTF-8 test was successful.\n";
 
 533   } elsif ($umlaut_lower eq 'Ä') {
 
 534     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";
 
 536     print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";