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.
18 use English '-no_match_vars';
27 our $lxdebug = LXDebug->new();
41 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
42 my ($opt_user, $opt_apply, $opt_applied, $opt_format, $opt_test_utf8);
43 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword);
45 our (%myconfig, $form, $user, $auth, $locale, $controls, $dbupgrader);
48 my $help_text = <<"END_HELP"
49 dbupgrade2_tool.pl [options]
51 A validation and information tool for the database upgrade scripts
52 in \'sql/Pg-upgrade2\'.
54 At startup dbupgrade2_tool.pl will always check the consistency
55 of all database upgrade scripts (e.g. circular references, invalid
56 formats, missing meta information). You can but don\'t have to specifiy
60 --list Lists all database upgrade tags
61 --tree Lists all database upgrades in tree form
62 --rtree Lists all database upgrades in reverse tree form
63 --graphviz[=file] Create a Postscript document showing a tree of
64 all database upgrades and their dependencies.
65 If no file name is given then the output is
66 written to \'db_dependencies.png\'.
67 --format=... Format for the graphviz output. Defaults to
68 \'png\'. All values that the command \'dot\' accepts
69 for it\'s option \'-T\' are acceptable.
70 --nodeps List all database upgrades that no other upgrade
72 --apply=tag Applies the database upgrades \'tag\' and all
73 upgrades it depends on. If \'--apply\' is used
74 then the option \'--user\' must be used as well.
75 --applied List the applied database upgrades for the
76 database that the user given with \'--user\' uses.
77 --test-utf8 Tests a PostgreSQL cluster for proper UTF-8 support.
78 You have to specify the database to test with the
79 parameters --dbname, --dbhost, --dbport, --dbuser
81 --help Show this help and exit.
84 --user=name The name of the user configuration to use for
85 database connectivity.
86 --dbname=name Database connection options for the UTF-8
87 --dbhost=host handling test.
103 sub calc_rev_depends {
104 map { $_->{rev_depends} = []; } values %{ $controls };
106 foreach my $control (values %{ $controls }) {
107 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
112 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
114 print "LIST VIEW\n\n" .
115 "number tag depth priority\n";
118 foreach (@sorted_controls) {
119 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
127 my ($tag, $depth) = @_;
129 print " " x $depth . $tag . "\n";
131 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
132 dump_node($dep_tag, $depth + 1);
137 print "TREE VIEW\n\n";
141 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
143 foreach my $control (@sorted_controls) {
144 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
150 sub dump_node_reverse {
151 my ($tag, $depth) = @_;
153 print " " x $depth . $tag . "\n";
155 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
156 dump_node_reverse($dep_tag, $depth + 1);
160 sub dump_tree_reverse {
161 print "REVERSE TREE VIEW\n\n";
165 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
167 foreach my $control (@sorted_controls) {
168 last if ($control->{depth} > 1);
169 dump_node_reverse($control->{tag}, "");
178 my $format = $params{format} || "png";
179 my $file_name = $params{file_name} || "db_dependencies.${format}";
181 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
182 print "Output will be written to '${file_name}'\n";
186 my $dot = "|dot -T${format} ";
187 open OUT, "${dot}> \"${file_name}\"" || die;
190 "digraph db_dependencies {\n" .
191 "graph [size=\"16.53,11.69!\"];\n" .
192 "node [shape=box style=filled fillcolor=white];\n";
195 foreach my $c (values %{ $controls }) {
196 $ranks{$c->{depth}} ||= [];
198 my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
199 (map "node [fillcolor=$_] ", qw(lightgray white));
201 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
204 foreach (sort keys %ranks) {
205 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
208 foreach my $c (values %{ $controls }) {
209 print OUT qq|"$c->{tag}";\n|;
211 foreach my $d (@{ $c->{depends} }) {
212 print OUT qq|"$c->{tag}" -> "$d";\n|;
223 print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
224 join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
231 my (@order, %tags, @all_tags);
233 if ($name eq "ALL") {
235 @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
238 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
242 foreach my $tag (@all_tags) {
243 build_upgrade_order($tag, \@order, \%tags);
246 my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
248 my $dbh = $form->dbconnect_noauto(\%myconfig);
250 $dbh->{PrintWarn} = 0;
251 $dbh->{PrintError} = 0;
253 $user->create_schema_info_table($form, $dbh);
255 my $query = qq|SELECT tag FROM schema_info|;
256 my $sth = $dbh->prepare($query);
257 $sth->execute() || $form->dberror($query);
258 while (my ($tag) = $sth->fetchrow_array()) {
259 $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
263 @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
264 if (!@upgradescripts) {
265 print "The upgrade has already been applied.\n";
269 foreach my $control (@upgradescripts) {
270 $control->{file} =~ /\.(sql|pl)$/;
274 print "Applying upgrade $control->{file}\n";
276 if ($file_type eq "sql") {
277 $dbupgrader->process_query($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
279 $dbupgrader->process_perl_script($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
286 sub dump_sql_result {
287 my ($results, $column_order) = @_;
289 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
291 foreach my $row (@{ $results }) {
292 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
296 if ($column_order && scalar @{ $column_order }) {
297 @sorted_names = @{ $column_order };
299 @sorted_names = sort keys %column_lengths;
302 my $format = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
304 printf $format, @sorted_names;
305 print join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
307 foreach my $row (@{ $results }) {
308 printf $format, map { $row->{$_} } @sorted_names;
310 printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
316 my $dbh = $form->dbconnect_noauto(\%myconfig);
318 $dbh->{PrintWarn} = 0;
319 $dbh->{PrintError} = 0;
321 $user->create_schema_info_table($form, $dbh);
323 my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
324 my $sth = $dbh->prepare($query);
325 $sth->execute() || $form->dberror($query);
326 while (my $ref = $sth->fetchrow_hashref()) {
333 if (!scalar @results) {
334 print "No database upgrades have been applied yet.\n";
336 dump_sql_result(\@results, [qw(tag login itime)]);
340 sub build_upgrade_order {
345 my $control = $controls->{$name};
347 foreach my $dependency (@{ $control->{depends} }) {
348 next if $tags->{$dependency};
349 $tags->{$dependency} = 1;
350 build_upgrade_order($dependency, $order, $tags);
353 push @{ $order }, $name;
361 eval { require "config/lx-erp.conf"; };
362 eval { require "config/lx-erp-local.conf"; } if (-f "config/lx-erp-local.conf");
364 $locale = Locale->new($::language);
371 GetOptions("list" => \$opt_list,
372 "tree" => \$opt_tree,
373 "rtree" => \$opt_rtree,
374 "nodeps" => \$opt_nodeps,
375 "graphviz:s" => \$opt_graphviz,
376 "format:s" => \$opt_format,
377 "user=s" => \$opt_user,
378 "apply=s" => \$opt_apply,
379 "applied" => \$opt_applied,
380 "test-utf8" => \$opt_test_utf8,
381 "dbhost:s" => \$opt_dbhost,
382 "dbport:s" => \$opt_dbport,
383 "dbname:s" => \$opt_dbname,
384 "dbuser:s" => \$opt_dbuser,
385 "dbpassword:s" => \$opt_dbpassword,
386 "help" => \$opt_help,
389 show_help() if ($opt_help);
391 $dbupgrader = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg');
392 $controls = $dbupgrader->parse_dbupdate_controls->{all_controls};
394 dump_list() if ($opt_list);
395 dump_tree() if ($opt_tree);
396 dump_tree_reverse() if ($opt_rtree);
397 dump_graphviz('file_name' => $opt_graphviz,
398 'format' => $opt_format) if (defined $opt_graphviz);
399 dump_nodeps() if ($opt_nodeps);
402 $auth = SL::Auth->new();
403 if (!$auth->session_tables_present()) {
404 $form->error("The session and user management tables are not present in the " .
405 "authentication database. Please use the administration web interface " .
406 "and to create them.");
409 %myconfig = $auth->read_user($opt_user);
411 if (!$myconfig{login}) {
412 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
415 $locale = new Locale($myconfig{countrycode}, "all");
416 $user = new User($opt_user);
418 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
422 $form->error("--apply used but no user name given with --user.") if (!$user);
423 apply_upgrade($opt_apply);
427 $form->error("--applied used but no user name given with --user.") if (!$user);
431 if ($opt_test_utf8) {
432 $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
434 my $umlaut_upper = 'Ä';
436 my $dbconnect = "dbi:Pg:dbname=${opt_dbname}";
437 $dbconnect .= ";host=${opt_dbhost}" if ($opt_dbhost);
438 $dbconnect .= ";port=${opt_dbport}" if ($opt_dbport);
440 my $dbh = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
442 $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
444 my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
448 if ($umlaut_lower eq 'ä') {
449 print "UTF-8 test was successful.\n";
450 } elsif ($umlaut_lower eq 'Ä') {
451 print "UTF-8 test was NOT successful: Umlauts are not modified (this might be partially ok, but you should probably not use UTF-8 on this cluster).\n";
453 print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";