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';
 
  21 $lxdebug = LXDebug->new();
 
  34 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
 
  35 my ($opt_user, $opt_apply, $opt_applied);
 
  37 our (%myconfig, $form, $user, $auth);
 
  40   my $help_text = <<'END_HELP'
 
  41 dbupgrade2_tool.pl [options]
 
  43   A validation and information tool for the database upgrade scripts
 
  46   At startup dbupgrade2_tool.pl will always check the consistency
 
  47   of all database upgrade scripts (e.g. circular references, invalid
 
  48   formats, missing meta information). You can but don't have to specifiy
 
  52     --list               Lists all database upgrade tags
 
  53     --tree               Lists all database upgrades in tree form
 
  54     --rtree              Lists all database upgrades in reverse tree form
 
  55     --graphviz[=file]    Create a Postscript document showing a tree of
 
  56                          all database upgrades and their dependencies.
 
  57                          If no file name is given then the output is
 
  58                          written to 'db_dependencies.ps'.
 
  59     --nodeps             List all database upgrades that no other upgrade
 
  61     --apply=tag          Applies the database upgrades 'tag' and all
 
  62                          upgrades it depends on. If '--apply' is used
 
  63                          then the option '--user' must be used as well.
 
  64     --applied            List the applied database upgrades for the
 
  65                          database that the user given with '--user' uses.
 
  66     --help               Show this help and exit.
 
  69     --user=name          The name of the user configuration to use for
 
  70                          database connectivity.
 
  74   # Syntax-Highlighting-Fix für Emacs: '
 
  84 sub calc_rev_depends {
 
  85   map { $_->{rev_depends} = []; } values %{ $controls };
 
  87   foreach my $control (values %{ $controls }) {
 
  88     map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
 
  93   my @sorted_controls = sort_dbupdate_controls($controls);
 
  95   print "LIST VIEW\n\n" .
 
  96     "number tag depth priority\n";
 
  99   foreach (@sorted_controls) {
 
 100     print "$i $_->{tag} $_->{depth} $_->{priority}\n";
 
 108   my ($tag, $depth) = @_;
 
 110   print " " x $depth . $tag . "\n";
 
 112   foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
 
 113     dump_node($dep_tag, $depth + 1);
 
 118   print "TREE VIEW\n\n";
 
 122   my @sorted_controls = sort_dbupdate_controls($controls);
 
 124   foreach my $control (@sorted_controls) {
 
 125     dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
 
 131 sub dump_node_reverse {
 
 132   my ($tag, $depth) = @_;
 
 134   print " " x $depth . $tag . "\n";
 
 136   foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
 
 137     dump_node_reverse($dep_tag, $depth + 1);
 
 141 sub dump_tree_reverse {
 
 142   print "REVERSE TREE VIEW\n\n";
 
 146   my @sorted_controls = sort_dbupdate_controls($controls);
 
 148   foreach my $control (@sorted_controls) {
 
 149     last if ($control->{depth} > 1);
 
 150     dump_node_reverse($control->{tag}, "");
 
 157   my $file_name = shift || "db_dependencies.ps";
 
 159   print "GRAPHVIZ POSTCRIPT\n\n";
 
 160   print "Output will be written to '${file_name}'\n";
 
 165   open OUT, "${dot}> \"${file_name}\"" || die;
 
 168     "digraph db_dependencies {\n" .
 
 169     "graph [size=\"16.53,11.69!\"];\n" .
 
 170     "node [shape=box style=filled fillcolor=white];\n";
 
 173   foreach my $c (values %{ $controls }) {
 
 174     $ranks{$c->{depth}} ||= [];
 
 176     my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if (!scalar @{ $c->{rev_depends} });
 
 178     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 181   foreach (sort keys %ranks) {
 
 182     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 185   foreach my $c (values %{ $controls }) {
 
 186     print OUT "$c->{tag};\n";
 
 188     foreach my $d (@{ $c->{depends} }) {
 
 189       print OUT "$c->{tag} -> $d;\n";
 
 200   print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 201     join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
 
 208   my (@order, %tags, @all_tags);
 
 210   if ($name eq "ALL") {
 
 212     @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
 
 215     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 219   foreach my $tag (@all_tags) {
 
 220     build_upgrade_order($tag, \@order, \%tags);
 
 223   my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
 
 225   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 227   $dbh->{PrintWarn}  = 0;
 
 228   $dbh->{PrintError} = 0;
 
 230   $user->create_schema_info_table($form, $dbh);
 
 232   my $query = qq|SELECT tag FROM schema_info|;
 
 233   $sth = $dbh->prepare($query);
 
 234   $sth->execute() || $form->dberror($query);
 
 235   while (($tag) = $sth->fetchrow_array()) {
 
 236     $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
 
 240   @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
 
 241   if (!@upgradescripts) {
 
 242     print "The upgrade has already been applied.\n";
 
 246   foreach my $control (@upgradescripts) {
 
 247     $control->{file} =~ /\.(sql|pl)$/;
 
 251     print "Applying upgrade $control->{file}\n";
 
 253     if ($file_type eq "sql") {
 
 254       $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 256       $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 263 sub dump_sql_result {
 
 264   my ($results, $column_order) = @_;
 
 266   my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
 
 268   foreach my $row (@{ $results }) {
 
 269     map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
 
 273   if ($column_order && scalar @{ $column_order }) {
 
 274     @sorted_names = @{ $column_order };
 
 276     @sorted_names = sort keys %column_lengths;
 
 279   my $format       = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
 
 281   printf $format, @sorted_names;
 
 282   print  join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
 
 284   foreach my $row (@{ $results }) {
 
 285     printf $format, map { $row->{$_} } @sorted_names;
 
 287   printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
 
 293   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 295   $dbh->{PrintWarn}  = 0;
 
 296   $dbh->{PrintError} = 0;
 
 298   $user->create_schema_info_table($form, $dbh);
 
 300   my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
 
 301   $sth = $dbh->prepare($query);
 
 302   $sth->execute() || $form->dberror($query);
 
 303   while (my $ref = $sth->fetchrow_hashref()) {
 
 310   if (!scalar @results) {
 
 311     print "No database upgrades have been applied yet.\n";
 
 313     dump_sql_result(\@results, [qw(tag login itime)]);
 
 317 sub build_upgrade_order {
 
 322   my $control = $controls->{$name};
 
 324   foreach my $dependency (@{ $control->{depends} }) {
 
 325     next if $tags->{$dependency};
 
 326     $tags->{$dependency} = 1;
 
 327     build_upgrade_order($dependency, $order, $tag);
 
 330   push @{ $order }, $name;
 
 338 eval { require "config/lx-erp.conf"; };
 
 339 eval { require "config/lx-erp-local.conf"; } if (-f "config/lx-erp-local.conf");
 
 342 $locale = Locale->new("de", "login");
 
 348 GetOptions("list"       => \$opt_list,
 
 349            "tree"       => \$opt_tree,
 
 350            "rtree"      => \$opt_rtree,
 
 351            "nodeps"     => \$opt_nodeps,
 
 352            "graphviz:s" => \$opt_graphviz,
 
 353            "user=s"     => \$opt_user,
 
 354            "apply=s"    => \$opt_apply,
 
 355            "applied"    => \$opt_applied,
 
 356            "help"       => \$opt_help,
 
 359 show_help() if ($opt_help);
 
 361 $controls = parse_dbupdate_controls($form, "Pg");
 
 363 dump_list()                  if ($opt_list);
 
 364 dump_tree()                  if ($opt_tree);
 
 365 dump_tree_reverse()          if ($opt_rtree);
 
 366 dump_graphviz($opt_graphviz) if (defined $opt_graphviz);
 
 367 dump_nodeps()                if ($opt_nodeps);
 
 370   $auth = SL::Auth->new();
 
 371   if (!$auth->session_tables_present()) {
 
 372     $form->error("The session and user management tables are not present in the " .
 
 373                  "authentication database. Please use the administration web interface " .
 
 374                  "and to create them.");
 
 377   %myconfig = $auth->read_user($opt_user);
 
 379   if (!$myconfig{login}) {
 
 380     $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
 
 383   $locale = new Locale($myconfig{countrycode}, "all");
 
 384   $user   = new User($opt_user);
 
 386   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 390   $form->error("--apply used but no user name given with --user.") if (!$user);
 
 391   apply_upgrade($opt_apply);
 
 395   $form->error("--applied used but no user name given with --user.") if (!$user);