4 if (! -d "bin" || ! -d "SL") {
5 print("This tool must be run from the Lx-Office ERP base directory.\n");
12 use English '-no_match_vars';
20 $lxdebug = LXDebug->new();
32 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
33 my ($opt_user, $opt_apply);
35 our (%myconfig, $form, $user);
38 my $help_text = <<'END_HELP'
39 dbupgrade2_tool.pl [options]
41 A validation and information tool for the database upgrade scripts
44 At startup dbupgrade2_tool.pl will always check the consistency
45 of all database upgrade scripts (e.g. circular references, invalid
46 formats, missing meta information). You can but don't have to specifiy
50 --list Lists all database upgrade tags
51 --tree Lists all database upgrades in tree form
52 --rtree Lists all database upgrades in reverse tree form
53 --graphviz[=file] Create a Postscript document showing a tree of
54 all database upgrades and their dependencies.
55 If no file name is given then the output is
56 written to 'db_dependencies.ps'.
57 --apply=tag Applies the database upgrades 'tag' and all
58 upgrades it depends on. If '--apply' is used
59 then the option '--user' must be used as well.
60 --help Show this help and exit.
63 --user=name The name of the user configuration to use for
64 database connectivity.
75 sub calc_rev_depends {
76 map({ $_->{"rev_depends"} = []; } values(%{$controls}));
77 foreach my $control (values(%{$controls})) {
78 map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
79 @{$control->{"depends"}});
84 my @sorted_controls = sort_dbupdate_controls($controls);
86 print("LIST VIEW\n\n");
87 print("number tag depth priority\n");
89 foreach (@sorted_controls) {
90 print("$i $_->{tag} $_->{depth} $_->{priority}\n");
98 my ($tag, $depth) = @_;
100 print(" " x $depth . $tag . "\n");
102 my $c = $controls->{$tag};
103 my $num = scalar(@{$c->{"depends"}});
104 for (my $i = 0; $i < $num; $i++) {
105 dump_node($c->{"depends"}[$i], $depth + 1);
110 print("TREE VIEW\n\n");
114 my @sorted_controls = sort_dbupdate_controls($controls);
116 foreach my $control (@sorted_controls) {
117 dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
123 sub dump_node_reverse {
124 my ($tag, $depth) = @_;
126 print(" " x $depth . $tag . "\n");
128 my $c = $controls->{$tag};
129 my $num = scalar(@{$c->{"rev_depends"}});
130 for (my $i = 0; $i < $num; $i++) {
131 dump_node_reverse($c->{"rev_depends"}[$i], $depth + 1);
135 sub dump_tree_reverse {
136 print("REVERSE TREE VIEW\n\n");
140 my @sorted_controls = sort_dbupdate_controls($controls);
142 foreach my $control (@sorted_controls) {
143 last if ($control->{"depth"} > 1);
144 dump_node_reverse($control->{"tag"}, "");
151 my $file_name = shift || "db_dependencies.ps";
153 print("GRAPHVIZ POSTCRIPT\n\n");
154 print("Output will be written to '${file_name}'\n");
159 open OUT, "${dot}> \"${file_name}\"" || die;
162 "digraph db_dependencies {\n" .
163 "node [shape=box style=filled fillcolor=white];\n");
165 foreach my $c (values(%{$controls})) {
166 $ranks{$c->{"depth"}} ||= [];
168 my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if !@{ $c->{"rev_depends"} };
170 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
172 foreach (sort(keys(%ranks))) {
173 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
175 foreach my $c (values(%{$controls})) {
176 print(OUT "$c->{tag};\n");
177 foreach my $d (@{$c->{"depends"}}) {
178 print(OUT "$c->{tag} -> $d;\n");
188 print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
191 grep({ !@{$_->{"rev_depends"}} }
192 values(%{$controls})))) .
199 my (@order, %tags, @all_tags);
201 if ($name eq "ALL") {
203 @all_tags = map { $_->{"tag"} } grep { !@{$_->{"rev_depends"}} } values %{$controls};
206 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
210 foreach my $tag (@all_tags) {
211 build_upgrade_order($tag, \@order, \%tags);
214 my @upgradescripts = map { $controls->{$_}->{"applied"} = 0; $controls->{$_} } @order;
216 my $dbh = $form->dbconnect_noauto(\%myconfig);
218 $dbh->{PrintWarn} = 0;
219 $dbh->{PrintError} = 0;
221 $user->create_schema_info_table($form, $dbh);
223 my $query = qq|SELECT tag FROM schema_info|;
224 $sth = $dbh->prepare($query);
225 $sth->execute() || $form->dberror($query);
226 while (($tag) = $sth->fetchrow_array()) {
227 $controls->{$tag}->{"applied"} = 1 if defined $controls->{$tag};
231 @upgradescripts = sort { $a->{"priority"} <=> $b->{"priority"} } grep { !$_->{"applied"} } @upgradescripts;
232 if (!@upgradescripts) {
233 print "The upgrade has already been applied.\n";
237 foreach my $control (@upgradescripts) {
238 $control->{"file"} =~ /\.(sql|pl)$/;
242 print "Applying upgrade $control->{file}\n";
244 if ($file_type eq "sql") {
245 $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
247 $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
254 sub build_upgrade_order {
259 my $control = $controls->{$name};
261 foreach my $dependency (@{ $control->{"depends"} }) {
262 next if $tags->{$dependency};
263 $tags->{$dependency} = 1;
264 build_upgrade_order($dependency, $order, $tag);
267 push @{ $order }, $name;
275 eval { require "lx-erp.conf"; };
278 $locale = Locale->new("de", "login");
284 GetOptions("list" => \$opt_list,
285 "tree" => \$opt_tree,
286 "rtree" => \$opt_rtree,
287 "nodeps" => \$opt_nodeps,
288 "graphviz:s" => \$opt_graphviz,
289 "user=s" => \$opt_user,
290 "apply=s" => \$opt_apply,
291 "help" => \$opt_help,
298 $controls = parse_dbupdate_controls($form, "Pg");
312 if (defined $opt_graphviz) {
313 dump_graphviz($opt_graphviz);
321 my $file_name = "users/${opt_user}.conf";
323 eval { require($file_name); };
324 $form->error("File '$file_name' was not found") if $@;
325 $locale = new Locale($myconfig{countrycode}, "all");
326 $user = new User("users/members", $opt_user);
327 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
331 $form->error("--apply used but no configuration file given with --user.") if (!$user);
332 apply_upgrade($opt_apply);