4   if (! -d "bin" || ! -d "SL") {
 
   5     print("This tool must be run from the kivitendo 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_client, $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, $opt_auth_db);
 
  49 our (%myconfig, $form, $user, $auth, $locale, $controls, $dbupgrader);
 
  52   return $auth if $auth;
 
  54   $auth = SL::Auth->new;
 
  55   if (!$auth->session_tables_present) {
 
  56     $form->error("The session and user management tables are not present in the authentication database. Please use the administration web interface to create them.");
 
  63   my $help_text = <<"END_HELP"
 
  64 dbupgrade2_tool.pl [options]
 
  66   A validation and information tool for the database upgrade scripts
 
  67   in \'sql/Pg-upgrade2\'.
 
  69   At startup dbupgrade2_tool.pl will always check the consistency
 
  70   of all database upgrade scripts (e.g. circular references, invalid
 
  71   formats, missing meta information). You can but don\'t have to specifiy
 
  75     --list               Lists all database upgrade tags
 
  76     --tree               Lists all database upgrades in tree form
 
  77     --rtree              Lists all database upgrades in reverse tree form
 
  78     --graphviz[=file]    Create a Postscript document showing a tree of
 
  79                          all database upgrades and their dependencies.
 
  80                          If no file name is given then the output is
 
  81                          written to \'db_dependencies.png\'.
 
  82     --format=...         Format for the graphviz output. Defaults to
 
  83                          \'png\'. All values that the command \'dot\' accepts
 
  84                          for it\'s option \'-T\' are acceptable.
 
  85     --nodeps             List all database upgrades that no other upgrade
 
  87     --create=tag         Creates a new upgrade with the supplied tag. This
 
  88                          action accepts several optional other options. See
 
  89                          the option section for those. After creating the
 
  90                          upgrade file your \$EDITOR will be called with it.
 
  91     --apply=tag          Applies the database upgrades \'tag\' and all
 
  92                          upgrades it depends on. If \'--apply\' is used
 
  93                          then the option \'--user\' must be used as well.
 
  94     --applied            List the applied database upgrades for the
 
  95                          database that the user given with \'--user\' uses.
 
  96     --unapplied          List the database upgrades that haven\'t been applied
 
  97                          yet to the database that the user given with
 
  99     --test-utf8          Tests a PostgreSQL cluster for proper UTF-8 support.
 
 100                          You have to specify the database to test with the
 
 101                          parameters --dbname, --dbhost, --dbport, --dbuser
 
 103     --help               Show this help and exit.
 
 106     --client=id-or-name  The name (or database ID) of the client to use for
 
 107                          database connectivity. You must provide both a client
 
 109     --user=name          The name of the user configuration to use for
 
 110                          database connectivity. You must provide both a client
 
 112     --auth-db            Work on the authentication database instead of a
 
 114     --dbname=name        Database connection options for the UTF-8
 
 115     --dbhost=host        handling test.
 
 120   Options for --create:
 
 121     --type               \'sql\' or \'pl\'. Defaults to sql.
 
 122     --description        The description field of the generated upgrade.
 
 123     --encoding           Encoding used for the file. Defaults to \'utf8\'.
 
 124                          Note: Your editor will not be told to open the file in
 
 126     --depends            Tags of upgrades which this upgrade depends upon.
 
 127                          Defaults to the latest stable release upgrade.
 
 128                          Multiple values possible.
 
 141 sub calc_rev_depends {
 
 142   map { $_->{rev_depends} = []; } values %{ $controls };
 
 144   foreach my $control (values %{ $controls }) {
 
 145     map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
 
 150   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 152   print "LIST VIEW\n\n" .
 
 153     "number tag depth priority\n";
 
 156   foreach (@sorted_controls) {
 
 157     print "$i $_->{tag} $_->{depth} $_->{priority}\n";
 
 165   my ($tag, $depth) = @_;
 
 167   print " " x $depth . $tag . "\n";
 
 169   foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
 
 170     dump_node($dep_tag, $depth + 1);
 
 175   print "TREE VIEW\n\n";
 
 179   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 181   foreach my $control (@sorted_controls) {
 
 182     dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
 
 188 sub dump_node_reverse {
 
 189   my ($tag, $depth) = @_;
 
 191   print " " x $depth . $tag . "\n";
 
 193   foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
 
 194     dump_node_reverse($dep_tag, $depth + 1);
 
 198 sub dump_tree_reverse {
 
 199   print "REVERSE TREE VIEW\n\n";
 
 203   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 205   foreach my $control (@sorted_controls) {
 
 206     last if ($control->{depth} > 1);
 
 207     dump_node_reverse($control->{tag}, "");
 
 216   my $format    = $params{format}    || "png";
 
 217   my $file_name = $params{file_name} || "db_dependencies.${format}";
 
 219   print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
 
 220   print "Output will be written to '${file_name}'\n";
 
 224   my $dot = "|dot -T${format} ";
 
 225   open OUT, "${dot}> \"${file_name}\"" || die;
 
 228     "digraph db_dependencies {\n" .
 
 229     "graph [size=\"16.53,11.69!\"];\n" .
 
 230     "node [shape=box style=filled fillcolor=white];\n";
 
 233   foreach my $c (values %{ $controls }) {
 
 234     $ranks{$c->{depth}} ||= [];
 
 236     my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
 
 237       (map "node [fillcolor=$_] ", qw(lightgray white));
 
 239     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 242   foreach (sort keys %ranks) {
 
 243     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 246   foreach my $c (values %{ $controls }) {
 
 247     print OUT qq|"$c->{tag}";\n|;
 
 249     foreach my $d (@{ $c->{depends} }) {
 
 250       print OUT qq|"$c->{tag}" -> "$d";\n|;
 
 261   print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 262     join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
 
 269   my $filename    = $params{filename};
 
 270   my $dbupgrader  = $params{dbupgrader};
 
 271   my $type        = $params{type}        || 'sql';
 
 272   my $description = $params{description} || '';
 
 273   my $encoding    = $params{encoding}    || 'utf-8';
 
 274   my @depends     = @{ $params{depends} };
 
 277     my @releases = grep { /^release_/ } keys %$controls;
 
 278     @depends = ((sort @releases)[-1]);
 
 282   if ($type eq 'sql') {
 
 284   } elsif ($type eq 'pl') {
 
 287     die 'Error: No --type was given but is required for --create.';
 
 289     die 'Error: Unknown --type. Try "sql" or "pl".';
 
 292   my $full_filename = $dbupgrader->path . '/' . $filename . '.' . $type;
 
 294   die "file '$full_filename' already exists, aborting" if -f $full_filename;
 
 297   open my $fh, ">:encoding($encoding)", $full_filename or die "can't open $full_filename";
 
 298   print $fh "$comment \@tag: $filename\n";
 
 299   print $fh "$comment \@description: $description\n";
 
 300   print $fh "$comment \@depends: @depends\n";
 
 301   print $fh "$comment \@encoding: $encoding\n";
 
 304   print "File $full_filename created.\n";
 
 306   system("\$EDITOR $full_filename");
 
 313   my (@order, %tags, @all_tags);
 
 315   if ($name eq "ALL") {
 
 317     @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
 
 320     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 324   foreach my $tag (@all_tags) {
 
 325     build_upgrade_order($tag, \@order, \%tags);
 
 328   my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
 
 330   my $dbh            = $opt_auth_db ? connect_auth()->dbconnect : $form->dbconnect_noauto(\%myconfig);
 
 331   $dbh->{AutoCommit} = 0;
 
 333   $dbh->{PrintWarn}  = 0;
 
 334   $dbh->{PrintError} = 0;
 
 336   $user->create_schema_info_table($form, $dbh);
 
 338   my $query = qq|SELECT tag FROM schema_info|;
 
 339   my $sth = $dbh->prepare($query);
 
 340   $sth->execute() || $form->dberror($query);
 
 341   while (my ($tag) = $sth->fetchrow_array()) {
 
 342     $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
 
 346   @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
 
 347   if (!@upgradescripts) {
 
 348     print "The upgrade has already been applied.\n";
 
 352   foreach my $control (@upgradescripts) {
 
 353     $control->{file} =~ /\.(sql|pl)$/;
 
 357     print "Applying upgrade $control->{file}\n";
 
 359     if ($file_type eq "sql") {
 
 360       $dbupgrader->process_query($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
 
 362       $dbupgrader->process_perl_script($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
 
 366   $dbh->disconnect unless $opt_auth_db;
 
 369 sub dump_sql_result {
 
 370   my ($results, $column_order) = @_;
 
 372   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 374   foreach my $row (@{ $results }) {
 
 375     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 379   if ($column_order && scalar @{ $column_order }) {
 
 380     @sorted_names = @{ $column_order };
 
 382     @sorted_names = sort keys %column_lengths;
 
 385   my $format       = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
 
 387   printf $format, @sorted_names;
 
 388   print  join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
 
 390   foreach my $row (@{ $results }) {
 
 391     printf $format, map { $row->{$_} } @sorted_names;
 
 393   printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
 
 399   my $dbh            = $opt_auth_db ? connect_auth()->dbconnect : $form->dbconnect_noauto(\%myconfig);
 
 400   $dbh->{AutoCommit} = 0;
 
 402   $dbh->{PrintWarn}  = 0;
 
 403   $dbh->{PrintError} = 0;
 
 405   $user->create_schema_info_table($form, $dbh);
 
 407   my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
 
 408   my $sth = $dbh->prepare($query);
 
 409   $sth->execute() || $form->dberror($query);
 
 410   while (my $ref = $sth->fetchrow_hashref()) {
 
 415   $dbh->disconnect unless $opt_auth_db;
 
 417   if (!scalar @results) {
 
 418     print "No database upgrades have been applied yet.\n";
 
 420     dump_sql_result(\@results, [qw(tag login itime)]);
 
 427   my $dbh = $opt_auth_db ? connect_auth()->dbconnect : $form->dbconnect_noauto(\%myconfig);
 
 429   $dbh->{PrintWarn}  = 0;
 
 430   $dbh->{PrintError} = 0;
 
 432   my @unapplied = $dbupgrader->unapplied_upgrade_scripts($dbh);
 
 434   $dbh->disconnect unless $opt_auth_db;
 
 436   if (!scalar @unapplied) {
 
 437     print "All database upgrades have been applied.\n";
 
 439     print map { $_->{tag} . "\n" } @unapplied;
 
 443 sub build_upgrade_order {
 
 448   my $control = $controls->{$name};
 
 450   foreach my $dependency (@{ $control->{depends} }) {
 
 451     next if $tags->{$dependency};
 
 452     $tags->{$dependency} = 1;
 
 453     build_upgrade_order($dependency, $order, $tags);
 
 456   push @{ $order }, $name;
 
 464 $locale = Locale->new;
 
 471 GetOptions("list"         => \$opt_list,
 
 472            "tree"         => \$opt_tree,
 
 473            "rtree"        => \$opt_rtree,
 
 474            "nodeps"       => \$opt_nodeps,
 
 475            "graphviz:s"   => \$opt_graphviz,
 
 476            "format:s"     => \$opt_format,
 
 477            "user=s"       => \$opt_user,
 
 478            "client=s"     => \$opt_client,
 
 479            "apply=s"      => \$opt_apply,
 
 480            "applied"      => \$opt_applied,
 
 481            "create=s"     => \$opt_create,
 
 482            "type=s"       => \$opt_type,
 
 483            "encoding=s"   => \$opt_encoding,
 
 484            "description=s" => \$opt_description,
 
 485            "depends=s"    => \@opt_depends,
 
 486            "unapplied"    => \$opt_unapplied,
 
 487            "test-utf8"    => \$opt_test_utf8,
 
 488            "dbhost:s"     => \$opt_dbhost,
 
 489            "dbport:s"     => \$opt_dbport,
 
 490            "dbname:s"     => \$opt_dbname,
 
 491            "dbuser:s"     => \$opt_dbuser,
 
 492            "dbpassword:s" => \$opt_dbpassword,
 
 493            "auth-db"      => \$opt_auth_db,
 
 494            "help"         => \$opt_help,
 
 497 show_help() if ($opt_help);
 
 499 $dbupgrader = SL::DBUpgrade2->new(form => $form, auth => $opt_auth_db);
 
 500 $controls   = $dbupgrader->parse_dbupdate_controls->{all_controls};
 
 502 dump_list()                                 if ($opt_list);
 
 503 dump_tree()                                 if ($opt_tree);
 
 504 dump_tree_reverse()                         if ($opt_rtree);
 
 505 dump_graphviz('file_name' => $opt_graphviz,
 
 506               'format'    => $opt_format)   if (defined $opt_graphviz);
 
 507 dump_nodeps()                               if ($opt_nodeps);
 
 508 create_upgrade(filename   => $opt_create,
 
 509                dbupgrader  => $dbupgrader,
 
 511                description => $opt_description,
 
 512                encoding    => $opt_encoding,
 
 513                depends     => \@opt_depends) if ($opt_create);
 
 515 if ($opt_client && !connect_auth()->set_client($opt_client)) {
 
 516   $form->error($form->format_string("The client '#1' does not exist.", $opt_client));
 
 520   $form->error("Need a client, too.") if !$auth || !$auth->client;
 
 522   %myconfig = connect_auth()->read_user(login => $opt_user);
 
 524   if (!$myconfig{login}) {
 
 525     $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
 
 528   $locale = new Locale($myconfig{countrycode}, "all");
 
 529   $user   = new User(login => $opt_user);
 
 531   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 535   $form->error("--apply used but no user name given with --user.") if !$user && !$opt_auth_db;
 
 536   apply_upgrade($opt_apply);
 
 540   $form->error("--applied used but no user name given with --user.") if !$user && !$opt_auth_db;
 
 544 if ($opt_unapplied) {
 
 545   $form->error("--unapplied used but no user name given with --user.") if !$user && !$opt_auth_db;
 
 550 if ($opt_test_utf8) {
 
 551   $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
 
 553   my $umlaut_upper       = 'Ä';
 
 555   my $dbconnect          = "dbi:Pg:dbname=${opt_dbname}";
 
 556   $dbconnect            .= ";host=${opt_dbhost}" if ($opt_dbhost);
 
 557   $dbconnect            .= ";port=${opt_dbport}" if ($opt_dbport);
 
 559   my $dbh                = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
 
 561   $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
 
 563   my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
 
 567   if ($umlaut_lower eq 'ä') {
 
 568     print "UTF-8 test was successful.\n";
 
 569   } elsif ($umlaut_lower eq 'Ä') {
 
 570     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";
 
 572     print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";