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_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     --create=tag         Creates a new upgrade with the supplied tag. This
 
  77                          action accepts several optional other options. See
 
  78                          the option section for those. After creating the
 
  79                          upgrade file your \$EDITOR will be called with it.
 
  80     --apply=tag          Applies the database upgrades \'tag\' and all
 
  81                          upgrades it depends on. If \'--apply\' is used
 
  82                          then the option \'--user\' must be used as well.
 
  83     --applied            List the applied database upgrades for the
 
  84                          database that the user given with \'--user\' uses.
 
  85     --unapplied          List the database upgrades that haven\'t been applied
 
  86                          yet to the database that the user given with
 
  88     --test-utf8          Tests a PostgreSQL cluster for proper UTF-8 support.
 
  89                          You have to specify the database to test with the
 
  90                          parameters --dbname, --dbhost, --dbport, --dbuser
 
  92     --help               Show this help and exit.
 
  95     --user=name          The name of the user configuration to use for
 
  96                          database connectivity.
 
  97     --dbname=name        Database connection options for the UTF-8
 
  98     --dbhost=host        handling test.
 
 103   Options for --create:
 
 104     --type               \'sql\' or \'pl\'. Defaults to sql.
 
 105     --description        The description field of the generated upgrade.
 
 106     --encoding           Encoding used for the file. Defaults to \'utf8\'.
 
 107                          Note: Your editor will not be told to open the file in
 
 109     --depends            Tags of upgrades which this upgrade depends upon.
 
 110                          Defaults to the latest stable release upgrade.
 
 111                          Multiple values possible.
 
 124 sub calc_rev_depends {
 
 125   map { $_->{rev_depends} = []; } values %{ $controls };
 
 127   foreach my $control (values %{ $controls }) {
 
 128     map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
 
 133   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 135   print "LIST VIEW\n\n" .
 
 136     "number tag depth priority\n";
 
 139   foreach (@sorted_controls) {
 
 140     print "$i $_->{tag} $_->{depth} $_->{priority}\n";
 
 148   my ($tag, $depth) = @_;
 
 150   print " " x $depth . $tag . "\n";
 
 152   foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
 
 153     dump_node($dep_tag, $depth + 1);
 
 158   print "TREE VIEW\n\n";
 
 162   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 164   foreach my $control (@sorted_controls) {
 
 165     dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
 
 171 sub dump_node_reverse {
 
 172   my ($tag, $depth) = @_;
 
 174   print " " x $depth . $tag . "\n";
 
 176   foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
 
 177     dump_node_reverse($dep_tag, $depth + 1);
 
 181 sub dump_tree_reverse {
 
 182   print "REVERSE TREE VIEW\n\n";
 
 186   my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
 
 188   foreach my $control (@sorted_controls) {
 
 189     last if ($control->{depth} > 1);
 
 190     dump_node_reverse($control->{tag}, "");
 
 199   my $format    = $params{format}    || "png";
 
 200   my $file_name = $params{file_name} || "db_dependencies.${format}";
 
 202   print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
 
 203   print "Output will be written to '${file_name}'\n";
 
 207   my $dot = "|dot -T${format} ";
 
 208   open OUT, "${dot}> \"${file_name}\"" || die;
 
 211     "digraph db_dependencies {\n" .
 
 212     "graph [size=\"16.53,11.69!\"];\n" .
 
 213     "node [shape=box style=filled fillcolor=white];\n";
 
 216   foreach my $c (values %{ $controls }) {
 
 217     $ranks{$c->{depth}} ||= [];
 
 219     my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
 
 220       (map "node [fillcolor=$_] ", qw(lightgray white));
 
 222     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 225   foreach (sort keys %ranks) {
 
 226     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 229   foreach my $c (values %{ $controls }) {
 
 230     print OUT qq|"$c->{tag}";\n|;
 
 232     foreach my $d (@{ $c->{depends} }) {
 
 233       print OUT qq|"$c->{tag}" -> "$d";\n|;
 
 244   print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 245     join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
 
 252   my $filename    = $params{filename};
 
 253   my $dbupgrader  = $params{dbupgrader};
 
 254   my $type        = $params{type}        || 'sql';
 
 255   my $description = $params{description} || '';
 
 256   my $encoding    = $params{encoding}    || 'utf-8';
 
 257   my @depends     = @{ $params{depends} };
 
 260     my @releases = grep { /^release_/ } keys %$controls;
 
 261     @depends = ((sort @releases)[-1]);
 
 265   if ($type eq 'sql') {
 
 267   } elsif ($type eq 'pl') {
 
 270     die 'Error: No --type was given but is required for --create.';
 
 272     die 'Error: Unknown --type. Try "sql" or "pl".';
 
 275   my $full_filename = $dbupgrader->path . '/' . $filename . '.' . $type;
 
 277   die "file '$full_filename' already exists, aborting" if -f $full_filename;
 
 280   open my $fh, ">:encoding($encoding)", $full_filename or die "can't open $full_filename";
 
 281   print $fh "$comment \@tag: $filename\n";
 
 282   print $fh "$comment \@description: $description\n";
 
 283   print $fh "$comment \@depends: @depends\n";
 
 284   print $fh "$comment \@encoding: $encoding\n";
 
 287   print "File $full_filename created.\n";
 
 289   system("\$EDITOR $full_filename");
 
 296   my (@order, %tags, @all_tags);
 
 298   if ($name eq "ALL") {
 
 300     @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
 
 303     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 307   foreach my $tag (@all_tags) {
 
 308     build_upgrade_order($tag, \@order, \%tags);
 
 311   my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
 
 313   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 315   $dbh->{PrintWarn}  = 0;
 
 316   $dbh->{PrintError} = 0;
 
 318   $user->create_schema_info_table($form, $dbh);
 
 320   my $query = qq|SELECT tag FROM schema_info|;
 
 321   my $sth = $dbh->prepare($query);
 
 322   $sth->execute() || $form->dberror($query);
 
 323   while (my ($tag) = $sth->fetchrow_array()) {
 
 324     $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
 
 328   @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
 
 329   if (!@upgradescripts) {
 
 330     print "The upgrade has already been applied.\n";
 
 334   my $db_charset = $::lx_office_conf{system}->{dbcharset};
 
 335   $db_charset ||= Common::DEFAULT_CHARSET();
 
 337   foreach my $control (@upgradescripts) {
 
 338     $control->{file} =~ /\.(sql|pl)$/;
 
 342     print "Applying upgrade $control->{file}\n";
 
 344     if ($file_type eq "sql") {
 
 345       $dbupgrader->process_query($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control, $db_charset);
 
 347       $dbupgrader->process_perl_script($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control, $db_charset);
 
 354 sub dump_sql_result {
 
 355   my ($results, $column_order) = @_;
 
 357   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 359   foreach my $row (@{ $results }) {
 
 360     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 364   if ($column_order && scalar @{ $column_order }) {
 
 365     @sorted_names = @{ $column_order };
 
 367     @sorted_names = sort keys %column_lengths;
 
 370   my $format       = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
 
 372   printf $format, @sorted_names;
 
 373   print  join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
 
 375   foreach my $row (@{ $results }) {
 
 376     printf $format, map { $row->{$_} } @sorted_names;
 
 378   printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
 
 384   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 386   $dbh->{PrintWarn}  = 0;
 
 387   $dbh->{PrintError} = 0;
 
 389   $user->create_schema_info_table($form, $dbh);
 
 391   my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
 
 392   my $sth = $dbh->prepare($query);
 
 393   $sth->execute() || $form->dberror($query);
 
 394   while (my $ref = $sth->fetchrow_hashref()) {
 
 401   if (!scalar @results) {
 
 402     print "No database upgrades have been applied yet.\n";
 
 404     dump_sql_result(\@results, [qw(tag login itime)]);
 
 411   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 413   $dbh->{PrintWarn}  = 0;
 
 414   $dbh->{PrintError} = 0;
 
 416   my @unapplied = $dbupgrader->unapplied_upgrade_scripts($dbh);
 
 420   if (!scalar @unapplied) {
 
 421     print "All database upgrades have been applied.\n";
 
 423     print map { $_->{tag} . "\n" } @unapplied;
 
 427 sub build_upgrade_order {
 
 432   my $control = $controls->{$name};
 
 434   foreach my $dependency (@{ $control->{depends} }) {
 
 435     next if $tags->{$dependency};
 
 436     $tags->{$dependency} = 1;
 
 437     build_upgrade_order($dependency, $order, $tags);
 
 440   push @{ $order }, $name;
 
 448 $locale = Locale->new;
 
 455 GetOptions("list"         => \$opt_list,
 
 456            "tree"         => \$opt_tree,
 
 457            "rtree"        => \$opt_rtree,
 
 458            "nodeps"       => \$opt_nodeps,
 
 459            "graphviz:s"   => \$opt_graphviz,
 
 460            "format:s"     => \$opt_format,
 
 461            "user=s"       => \$opt_user,
 
 462            "apply=s"      => \$opt_apply,
 
 463            "applied"      => \$opt_applied,
 
 464            "create=s"     => \$opt_create,
 
 465            "type=s"       => \$opt_type,
 
 466            "encoding=s"   => \$opt_encoding,
 
 467            "description=s" => \$opt_description,
 
 468            "depends=s"    => \@opt_depends,
 
 469            "unapplied"    => \$opt_unapplied,
 
 470            "test-utf8"    => \$opt_test_utf8,
 
 471            "dbhost:s"     => \$opt_dbhost,
 
 472            "dbport:s"     => \$opt_dbport,
 
 473            "dbname:s"     => \$opt_dbname,
 
 474            "dbuser:s"     => \$opt_dbuser,
 
 475            "dbpassword:s" => \$opt_dbpassword,
 
 476            "help"         => \$opt_help,
 
 479 show_help() if ($opt_help);
 
 481 $dbupgrader = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg');
 
 482 $controls   = $dbupgrader->parse_dbupdate_controls->{all_controls};
 
 484 dump_list()                                 if ($opt_list);
 
 485 dump_tree()                                 if ($opt_tree);
 
 486 dump_tree_reverse()                         if ($opt_rtree);
 
 487 dump_graphviz('file_name' => $opt_graphviz,
 
 488               'format'    => $opt_format)   if (defined $opt_graphviz);
 
 489 dump_nodeps()                               if ($opt_nodeps);
 
 490 create_upgrade(filename   => $opt_create,
 
 491                dbupgrader  => $dbupgrader,
 
 493                description => $opt_description,
 
 494                encoding    => $opt_encoding,
 
 495                depends     => \@opt_depends) if ($opt_create);
 
 498   $auth = SL::Auth->new();
 
 499   if (!$auth->session_tables_present()) {
 
 500     $form->error("The session and user management tables are not present in the " .
 
 501                  "authentication database. Please use the administration web interface " .
 
 502                  "and to create them.");
 
 505   %myconfig = $auth->read_user(login => $opt_user);
 
 507   if (!$myconfig{login}) {
 
 508     $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
 
 511   $locale = new Locale($myconfig{countrycode}, "all");
 
 512   $user   = new User(login => $opt_user);
 
 514   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 518   $form->error("--apply used but no user name given with --user.") if (!$user);
 
 519   apply_upgrade($opt_apply);
 
 523   $form->error("--applied used but no user name given with --user.") if (!$user);
 
 527 if ($opt_unapplied) {
 
 528   $form->error("--unapplied used but no user name given with --user.") if (!$user);
 
 533 if ($opt_test_utf8) {
 
 534   $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
 
 536   my $umlaut_upper       = 'Ä';
 
 538   my $dbconnect          = "dbi:Pg:dbname=${opt_dbname}";
 
 539   $dbconnect            .= ";host=${opt_dbhost}" if ($opt_dbhost);
 
 540   $dbconnect            .= ";port=${opt_dbport}" if ($opt_dbport);
 
 542   my $dbh                = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
 
 544   $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
 
 546   my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
 
 550   if ($umlaut_lower eq 'ä') {
 
 551     print "UTF-8 test was successful.\n";
 
 552   } elsif ($umlaut_lower eq 'Ä') {
 
 553     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";
 
 555     print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";