Neuer Datenbankupgrademechanismus: Die Upgradedateien im neuen Verzeichnis sql/Pg...
[kivitendo-erp.git] / scripts / dbupgrade2_tool.pl
1 #!/usr/bin/perl
2
3 BEGIN {
4   if (! -d "bin" || ! -d "SL") {
5     print("This tool must be run from the Lx-Office ERP base directory.\n");
6     exit(1);
7   }
8 }
9
10 use DBI;
11 use Data::Dumper;
12 use Getopt::Long;
13
14 use SL::LXDebug;
15
16 $lxdebug = LXDebug->new();
17
18 use SL::Form;
19 use SL::DBUpgrade2;
20
21 #######
22 #######
23 #######
24
25 sub show_help {
26   print("dbupgrade2_tool.pl [--list] [--tree] [--rtree] [--graphviz]\n" .
27         "                   [--nodepds] [--help]\n");
28 }
29
30 sub calc_rev_depends {
31   map({ $_->{"rev_depends"} = []; } values(%{$controls}));
32   foreach my $control (values(%{$controls})) {
33     map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
34         @{$control->{"depends"}});
35   }
36 }
37
38 sub dump_list {
39   my @sorted_controls = sort_dbupdate_controls($controls);
40
41   print("LIST VIEW\n\n");
42   print("number tag depth priority\n");
43   $i = 0;
44   foreach (@sorted_controls) {
45     print("$i $_->{tag} $_->{depth} $_->{priority}\n");
46     $i++;
47   }
48
49   print("\n");
50 }
51
52 sub dump_node {
53   my ($tag, $depth) = @_;
54
55   print(" " x $depth . $tag . "\n");
56
57   my $c = $controls->{$tag};
58   my $num = scalar(@{$c->{"depends"}});
59   for (my $i = 0; $i < $num; $i++) {
60     dump_node($c->{"depends"}[$i], $depth + 1);
61   }
62 }
63
64 sub dump_tree {
65   print("TREE VIEW\n\n");
66
67   calc_rev_depends();
68
69   my @sorted_controls = sort_dbupdate_controls($controls);
70
71   foreach my $control (@sorted_controls) {
72     dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
73   }
74
75   print("\n");
76 }
77
78 sub dump_node_reverse {
79   my ($tag, $depth) = @_;
80
81   print(" " x $depth . $tag . "\n");
82
83   my $c = $controls->{$tag};
84   my $num = scalar(@{$c->{"rev_depends"}});
85   for (my $i = 0; $i < $num; $i++) {
86     dump_node_reverse($c->{"rev_depends"}[$i], $depth + 1);
87   }
88 }
89
90 sub dump_tree_reverse {
91   print("REVERSE TREE VIEW\n\n");
92
93   calc_rev_depends();
94
95   my @sorted_controls = sort_dbupdate_controls($controls);
96
97   foreach my $control (@sorted_controls) {
98     last if ($control->{"depth"} > 1);
99     dump_node_reverse($control->{"tag"}, "");
100   }
101
102   print("\n");
103 }
104
105 sub dump_graphviz {
106   print("GRAPHVIZ POSTCRIPT\n\n");
107   print("Output will be written to db_dependencies.ps\n");
108   $dot = "|dot -Tps ";
109   open(OUT, "${dot}> db_dependencies.ps");
110   print(OUT
111         "digraph db_dependencies {\n" .
112         "node [shape=box];\n");
113   my %ranks;
114   foreach my $c (values(%{$controls})) {
115     $ranks{$c->{"depth"}} = [] unless ($ranks{$c->{"depth"}});
116     push(@{$ranks{$c->{"depth"}}}, $c->{"tag"});
117   }
118   foreach (sort(keys(%ranks))) {
119     print(OUT "{ rank = same; " .
120           join("", map({ '"' . $_ . '"; ' } @{$ranks{$_}})) .
121           " }\n");
122   }
123   foreach my $c (values(%{$controls})) {
124     print(OUT "$c->{tag};\n");
125     foreach my $d (@{$c->{"depends"}}) {
126       print(OUT "$c->{tag} -> $d;\n");
127     }
128   }
129   print(OUT "}\n");
130   close(OUT);
131 }
132
133 sub dump_nodeps {
134   calc_rev_depends();
135
136   print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
137         join("\n",
138              map({ $_->{"tag"} }
139                  grep({ !@{$_->{"rev_depends"}} }
140                       values(%{$controls})))) .
141         "\n\n");
142 }
143
144 #######
145 #######
146 #######
147
148 eval { require "lx-erp.conf"; };
149
150 $form = Form->new();
151 $locale = Locale->new("de", "login");
152
153 #######
154 #######
155 #######
156
157 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
158
159 GetOptions("list" => \$opt_list,
160            "tree" => \$opt_tree,
161            "rtree" => \$opt_rtree,
162            "nodeps" => \$opt_nodeps,
163            "graphviz" => \$opt_graphviz,
164            "help" => \$opt_help,
165   );
166
167 if ($opt_help) {
168   show_help();
169   exit(0);
170 }
171
172 $controls = parse_dbupdate_controls($form, "Pg");
173
174 if ($opt_list) {
175   dump_list();
176 }
177
178 if ($opt_tree) {
179   dump_tree();
180 }
181
182 if ($opt_rtree) {
183   dump_tree_reverse();
184 }
185
186 if ($opt_graphviz) {
187   dump_graphviz();
188 }
189
190 if ($opt_nodeps) {
191   dump_nodeps();
192 }