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, $opt_format);
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
44 in \'sql/Pg-upgrade2\'.
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.png\'.
59 --format=... Format for the graphviz output. Defaults to
60 \'png\'. All values that the command \'dot\' accepts
61 for it\'s option \'-T\' are acceptable.
62 --nodeps List all database upgrades that no other upgrade
64 --apply=tag Applies the database upgrades \'tag\' and all
65 upgrades it depends on. If \'--apply\' is used
66 then the option \'--user\' must be used as well.
67 --applied List the applied database upgrades for the
68 database that the user given with \'--user\' uses.
69 --help Show this help and exit.
72 --user=name The name of the user configuration to use for
73 database connectivity.
85 sub calc_rev_depends {
86 map { $_->{rev_depends} = []; } values %{ $controls };
88 foreach my $control (values %{ $controls }) {
89 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
94 my @sorted_controls = sort_dbupdate_controls($controls);
96 print "LIST VIEW\n\n" .
97 "number tag depth priority\n";
100 foreach (@sorted_controls) {
101 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
109 my ($tag, $depth) = @_;
111 print " " x $depth . $tag . "\n";
113 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
114 dump_node($dep_tag, $depth + 1);
119 print "TREE VIEW\n\n";
123 my @sorted_controls = sort_dbupdate_controls($controls);
125 foreach my $control (@sorted_controls) {
126 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
132 sub dump_node_reverse {
133 my ($tag, $depth) = @_;
135 print " " x $depth . $tag . "\n";
137 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
138 dump_node_reverse($dep_tag, $depth + 1);
142 sub dump_tree_reverse {
143 print "REVERSE TREE VIEW\n\n";
147 my @sorted_controls = sort_dbupdate_controls($controls);
149 foreach my $control (@sorted_controls) {
150 last if ($control->{depth} > 1);
151 dump_node_reverse($control->{tag}, "");
160 my $format = $params{format} || "png";
161 my $file_name = $params{file_name} || "db_dependencies.${format}";
163 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
164 print "Output will be written to '${file_name}'\n";
168 $dot = "|dot -T${format} ";
169 open OUT, "${dot}> \"${file_name}\"" || die;
172 "digraph db_dependencies {\n" .
173 "graph [size=\"16.53,11.69!\"];\n" .
174 "node [shape=box style=filled fillcolor=white];\n";
177 foreach my $c (values %{ $controls }) {
178 $ranks{$c->{depth}} ||= [];
180 my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if (!scalar @{ $c->{rev_depends} });
182 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
185 foreach (sort keys %ranks) {
186 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
189 foreach my $c (values %{ $controls }) {
190 print OUT "$c->{tag};\n";
192 foreach my $d (@{ $c->{depends} }) {
193 print OUT "$c->{tag} -> $d;\n";
204 print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
205 join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
212 my (@order, %tags, @all_tags);
214 if ($name eq "ALL") {
216 @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
219 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
223 foreach my $tag (@all_tags) {
224 build_upgrade_order($tag, \@order, \%tags);
227 my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
229 my $dbh = $form->dbconnect_noauto(\%myconfig);
231 $dbh->{PrintWarn} = 0;
232 $dbh->{PrintError} = 0;
234 $user->create_schema_info_table($form, $dbh);
236 my $query = qq|SELECT tag FROM schema_info|;
237 $sth = $dbh->prepare($query);
238 $sth->execute() || $form->dberror($query);
239 while (($tag) = $sth->fetchrow_array()) {
240 $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
244 @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
245 if (!@upgradescripts) {
246 print "The upgrade has already been applied.\n";
250 foreach my $control (@upgradescripts) {
251 $control->{file} =~ /\.(sql|pl)$/;
255 print "Applying upgrade $control->{file}\n";
257 if ($file_type eq "sql") {
258 $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
260 $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
267 sub dump_sql_result {
268 my ($results, $column_order) = @_;
270 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
272 foreach my $row (@{ $results }) {
273 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
277 if ($column_order && scalar @{ $column_order }) {
278 @sorted_names = @{ $column_order };
280 @sorted_names = sort keys %column_lengths;
283 my $format = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
285 printf $format, @sorted_names;
286 print join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
288 foreach my $row (@{ $results }) {
289 printf $format, map { $row->{$_} } @sorted_names;
291 printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
297 my $dbh = $form->dbconnect_noauto(\%myconfig);
299 $dbh->{PrintWarn} = 0;
300 $dbh->{PrintError} = 0;
302 $user->create_schema_info_table($form, $dbh);
304 my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
305 $sth = $dbh->prepare($query);
306 $sth->execute() || $form->dberror($query);
307 while (my $ref = $sth->fetchrow_hashref()) {
314 if (!scalar @results) {
315 print "No database upgrades have been applied yet.\n";
317 dump_sql_result(\@results, [qw(tag login itime)]);
321 sub build_upgrade_order {
326 my $control = $controls->{$name};
328 foreach my $dependency (@{ $control->{depends} }) {
329 next if $tags->{$dependency};
330 $tags->{$dependency} = 1;
331 build_upgrade_order($dependency, $order, $tag);
334 push @{ $order }, $name;
342 eval { require "config/lx-erp.conf"; };
343 eval { require "config/lx-erp-local.conf"; } if (-f "config/lx-erp-local.conf");
346 $locale = Locale->new("de", "login");
352 GetOptions("list" => \$opt_list,
353 "tree" => \$opt_tree,
354 "rtree" => \$opt_rtree,
355 "nodeps" => \$opt_nodeps,
356 "graphviz:s" => \$opt_graphviz,
357 "format:s" => \$opt_format,
358 "user=s" => \$opt_user,
359 "apply=s" => \$opt_apply,
360 "applied" => \$opt_applied,
361 "help" => \$opt_help,
364 show_help() if ($opt_help);
366 $controls = parse_dbupdate_controls($form, "Pg");
368 dump_list() if ($opt_list);
369 dump_tree() if ($opt_tree);
370 dump_tree_reverse() if ($opt_rtree);
371 dump_graphviz('file_name' => $opt_graphviz,
372 'format' => $opt_format) if (defined $opt_graphviz);
373 dump_nodeps() if ($opt_nodeps);
376 $auth = SL::Auth->new();
377 if (!$auth->session_tables_present()) {
378 $form->error("The session and user management tables are not present in the " .
379 "authentication database. Please use the administration web interface " .
380 "and to create them.");
383 %myconfig = $auth->read_user($opt_user);
385 if (!$myconfig{login}) {
386 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
389 $locale = new Locale($myconfig{countrycode}, "all");
390 $user = new User($opt_user);
392 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
396 $form->error("--apply used but no user name given with --user.") if (!$user);
397 apply_upgrade($opt_apply);
401 $form->error("--applied used but no user name given with --user.") if (!$user);