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();
 
  33 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
 
  34 my ($opt_user, $opt_apply);
 
  36 our (%myconfig, $form, $user);
 
  39   my $help_text = <<'END_HELP'
 
  40 dbupgrade2_tool.pl [options]
 
  42   A validation and information tool for the database upgrade scripts
 
  45   At startup dbupgrade2_tool.pl will always check the consistency
 
  46   of all database upgrade scripts (e.g. circular references, invalid
 
  47   formats, missing meta information). You can but don't have to specifiy
 
  51     --list               Lists all database upgrade tags
 
  52     --tree               Lists all database upgrades in tree form
 
  53     --rtree              Lists all database upgrades in reverse tree form
 
  54     --graphviz[=file]    Create a Postscript document showing a tree of
 
  55                          all database upgrades and their dependencies.
 
  56                          If no file name is given then the output is
 
  57                          written to 'db_dependencies.ps'.
 
  58     --nodeps             List all database upgrades that no other upgrade
 
  60     --apply=tag          Applies the database upgrades 'tag' and all
 
  61                          upgrades it depends on. If '--apply' is used
 
  62                          then the option '--user' must be used as well.
 
  63     --help               Show this help and exit.
 
  66     --user=name          The name of the user configuration to use for
 
  67                          database connectivity.
 
  71   # Syntax-Highlighting-Fix für Emacs: '
 
  81 sub calc_rev_depends {
 
  82   map({ $_->{"rev_depends"} = []; } values(%{$controls}));
 
  83   foreach my $control (values(%{$controls})) {
 
  84     map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
 
  85         @{$control->{"depends"}});
 
  90   my @sorted_controls = sort_dbupdate_controls($controls);
 
  92   print("LIST VIEW\n\n");
 
  93   print("number tag depth priority\n");
 
  95   foreach (@sorted_controls) {
 
  96     print("$i $_->{tag} $_->{depth} $_->{priority}\n");
 
 104   my ($tag, $depth) = @_;
 
 106   print(" " x $depth . $tag . "\n");
 
 108   my $c = $controls->{$tag};
 
 109   my $num = scalar(@{$c->{"depends"}});
 
 110   for (my $i = 0; $i < $num; $i++) {
 
 111     dump_node($c->{"depends"}[$i], $depth + 1);
 
 116   print("TREE VIEW\n\n");
 
 120   my @sorted_controls = sort_dbupdate_controls($controls);
 
 122   foreach my $control (@sorted_controls) {
 
 123     dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
 
 129 sub dump_node_reverse {
 
 130   my ($tag, $depth) = @_;
 
 132   print(" " x $depth . $tag . "\n");
 
 134   my $c = $controls->{$tag};
 
 135   my $num = scalar(@{$c->{"rev_depends"}});
 
 136   for (my $i = 0; $i < $num; $i++) {
 
 137     dump_node_reverse($c->{"rev_depends"}[$i], $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         "node [shape=box style=filled fillcolor=white];\n");
 
 171   foreach my $c (values(%{$controls})) {
 
 172     $ranks{$c->{"depth"}} ||= [];
 
 174     my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if !@{ $c->{"rev_depends"} };
 
 176     push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
 
 178   foreach (sort(keys(%ranks))) {
 
 179     print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
 
 181   foreach my $c (values(%{$controls})) {
 
 182     print(OUT "$c->{tag};\n");
 
 183     foreach my $d (@{$c->{"depends"}}) {
 
 184       print(OUT "$c->{tag} -> $d;\n");
 
 194   print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
 
 197                  grep({ !@{$_->{"rev_depends"}} }
 
 198                       values(%{$controls})))) .
 
 205   my (@order, %tags, @all_tags);
 
 207   if ($name eq "ALL") {
 
 209     @all_tags = map { $_->{"tag"} } grep { !@{$_->{"rev_depends"}} } values %{$controls};
 
 212     $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
 
 216   foreach my $tag (@all_tags) {
 
 217     build_upgrade_order($tag, \@order, \%tags);
 
 220   my @upgradescripts = map { $controls->{$_}->{"applied"} = 0; $controls->{$_} } @order;
 
 222   my $dbh = $form->dbconnect_noauto(\%myconfig);
 
 224   $dbh->{PrintWarn}  = 0;
 
 225   $dbh->{PrintError} = 0;
 
 227   $user->create_schema_info_table($form, $dbh);
 
 229   my $query = qq|SELECT tag FROM schema_info|;
 
 230   $sth = $dbh->prepare($query);
 
 231   $sth->execute() || $form->dberror($query);
 
 232   while (($tag) = $sth->fetchrow_array()) {
 
 233     $controls->{$tag}->{"applied"} = 1 if defined $controls->{$tag};
 
 237   @upgradescripts = sort { $a->{"priority"} <=> $b->{"priority"} } grep { !$_->{"applied"} } @upgradescripts;
 
 238   if (!@upgradescripts) {
 
 239     print "The upgrade has already been applied.\n";
 
 243   foreach my $control (@upgradescripts) {
 
 244     $control->{"file"} =~ /\.(sql|pl)$/;
 
 248     print "Applying upgrade $control->{file}\n";
 
 250     if ($file_type eq "sql") {
 
 251       $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 253       $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
 
 260 sub build_upgrade_order {
 
 265   my $control = $controls->{$name};
 
 267   foreach my $dependency (@{ $control->{"depends"} }) {
 
 268     next if $tags->{$dependency};
 
 269     $tags->{$dependency} = 1;
 
 270     build_upgrade_order($dependency, $order, $tag);
 
 273   push @{ $order }, $name;
 
 281 eval { require "lx-erp.conf"; };
 
 284 $locale = Locale->new("de", "login");
 
 290 GetOptions("list" => \$opt_list,
 
 291            "tree" => \$opt_tree,
 
 292            "rtree" => \$opt_rtree,
 
 293            "nodeps" => \$opt_nodeps,
 
 294            "graphviz:s" => \$opt_graphviz,
 
 295            "user=s" => \$opt_user,
 
 296            "apply=s" => \$opt_apply,
 
 297            "help" => \$opt_help,
 
 304 $controls = parse_dbupdate_controls($form, "Pg");
 
 318 if (defined $opt_graphviz) {
 
 319   dump_graphviz($opt_graphviz);
 
 327   my $file_name = "users/${opt_user}.conf";
 
 329   eval { require($file_name); };
 
 330   $form->error("File '$file_name' was not found") if $@;
 
 331   $locale = new Locale($myconfig{countrycode}, "all");
 
 332   $user = new User("users/members", $opt_user);
 
 333   map { $form->{$_} = $myconfig{$_} } keys %myconfig;
 
 337   $form->error("--apply used but no configuration file given with --user.") if (!$user);
 
 338   apply_upgrade($opt_apply);