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/YAML"; # Use our own version of YAML.
10 push @INC, "modules"; # 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 --apply=tag Applies the database upgrades 'tag' and all
59 upgrades it depends on. If '--apply' is used
60 then the option '--user' must be used as well.
61 --help Show this help and exit.
64 --user=name The name of the user configuration to use for
65 database connectivity.
76 sub calc_rev_depends {
77 map({ $_->{"rev_depends"} = []; } values(%{$controls}));
78 foreach my $control (values(%{$controls})) {
79 map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
80 @{$control->{"depends"}});
85 my @sorted_controls = sort_dbupdate_controls($controls);
87 print("LIST VIEW\n\n");
88 print("number tag depth priority\n");
90 foreach (@sorted_controls) {
91 print("$i $_->{tag} $_->{depth} $_->{priority}\n");
99 my ($tag, $depth) = @_;
101 print(" " x $depth . $tag . "\n");
103 my $c = $controls->{$tag};
104 my $num = scalar(@{$c->{"depends"}});
105 for (my $i = 0; $i < $num; $i++) {
106 dump_node($c->{"depends"}[$i], $depth + 1);
111 print("TREE VIEW\n\n");
115 my @sorted_controls = sort_dbupdate_controls($controls);
117 foreach my $control (@sorted_controls) {
118 dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
124 sub dump_node_reverse {
125 my ($tag, $depth) = @_;
127 print(" " x $depth . $tag . "\n");
129 my $c = $controls->{$tag};
130 my $num = scalar(@{$c->{"rev_depends"}});
131 for (my $i = 0; $i < $num; $i++) {
132 dump_node_reverse($c->{"rev_depends"}[$i], $depth + 1);
136 sub dump_tree_reverse {
137 print("REVERSE TREE VIEW\n\n");
141 my @sorted_controls = sort_dbupdate_controls($controls);
143 foreach my $control (@sorted_controls) {
144 last if ($control->{"depth"} > 1);
145 dump_node_reverse($control->{"tag"}, "");
152 my $file_name = shift || "db_dependencies.ps";
154 print("GRAPHVIZ POSTCRIPT\n\n");
155 print("Output will be written to '${file_name}'\n");
160 open OUT, "${dot}> \"${file_name}\"" || die;
163 "digraph db_dependencies {\n" .
164 "node [shape=box style=filled fillcolor=white];\n");
166 foreach my $c (values(%{$controls})) {
167 $ranks{$c->{"depth"}} ||= [];
169 my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if !@{ $c->{"rev_depends"} };
171 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
173 foreach (sort(keys(%ranks))) {
174 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
176 foreach my $c (values(%{$controls})) {
177 print(OUT "$c->{tag};\n");
178 foreach my $d (@{$c->{"depends"}}) {
179 print(OUT "$c->{tag} -> $d;\n");
189 print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
192 grep({ !@{$_->{"rev_depends"}} }
193 values(%{$controls})))) .
200 my (@order, %tags, @all_tags);
202 if ($name eq "ALL") {
204 @all_tags = map { $_->{"tag"} } grep { !@{$_->{"rev_depends"}} } values %{$controls};
207 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
211 foreach my $tag (@all_tags) {
212 build_upgrade_order($tag, \@order, \%tags);
215 my @upgradescripts = map { $controls->{$_}->{"applied"} = 0; $controls->{$_} } @order;
217 my $dbh = $form->dbconnect_noauto(\%myconfig);
219 $dbh->{PrintWarn} = 0;
220 $dbh->{PrintError} = 0;
222 $user->create_schema_info_table($form, $dbh);
224 my $query = qq|SELECT tag FROM schema_info|;
225 $sth = $dbh->prepare($query);
226 $sth->execute() || $form->dberror($query);
227 while (($tag) = $sth->fetchrow_array()) {
228 $controls->{$tag}->{"applied"} = 1 if defined $controls->{$tag};
232 @upgradescripts = sort { $a->{"priority"} <=> $b->{"priority"} } grep { !$_->{"applied"} } @upgradescripts;
233 if (!@upgradescripts) {
234 print "The upgrade has already been applied.\n";
238 foreach my $control (@upgradescripts) {
239 $control->{"file"} =~ /\.(sql|pl)$/;
243 print "Applying upgrade $control->{file}\n";
245 if ($file_type eq "sql") {
246 $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
248 $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
255 sub build_upgrade_order {
260 my $control = $controls->{$name};
262 foreach my $dependency (@{ $control->{"depends"} }) {
263 next if $tags->{$dependency};
264 $tags->{$dependency} = 1;
265 build_upgrade_order($dependency, $order, $tag);
268 push @{ $order }, $name;
276 eval { require "lx-erp.conf"; };
279 $locale = Locale->new("de", "login");
285 GetOptions("list" => \$opt_list,
286 "tree" => \$opt_tree,
287 "rtree" => \$opt_rtree,
288 "nodeps" => \$opt_nodeps,
289 "graphviz:s" => \$opt_graphviz,
290 "user=s" => \$opt_user,
291 "apply=s" => \$opt_apply,
292 "help" => \$opt_help,
299 $controls = parse_dbupdate_controls($form, "Pg");
313 if (defined $opt_graphviz) {
314 dump_graphviz($opt_graphviz);
322 my $file_name = "users/${opt_user}.conf";
324 eval { require($file_name); };
325 $form->error("File '$file_name' was not found") if $@;
326 $locale = new Locale($myconfig{countrycode}, "all");
327 $user = new User("users/members", $opt_user);
328 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
332 $form->error("--apply used but no configuration file given with --user.") if (!$user);
333 apply_upgrade($opt_apply);