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';
29 SL::LxOfficeConf->read;
30 our $lxdebug = LXDebug->new();
44 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
45 my ($opt_user, $opt_apply, $opt_applied, $opt_unapplied, $opt_format, $opt_test_utf8);
46 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword);
48 our (%myconfig, $form, $user, $auth, $locale, $controls, $dbupgrader);
51 my $help_text = <<"END_HELP"
52 dbupgrade2_tool.pl [options]
54 A validation and information tool for the database upgrade scripts
55 in \'sql/Pg-upgrade2\'.
57 At startup dbupgrade2_tool.pl will always check the consistency
58 of all database upgrade scripts (e.g. circular references, invalid
59 formats, missing meta information). You can but don\'t have to specifiy
63 --list Lists all database upgrade tags
64 --tree Lists all database upgrades in tree form
65 --rtree Lists all database upgrades in reverse tree form
66 --graphviz[=file] Create a Postscript document showing a tree of
67 all database upgrades and their dependencies.
68 If no file name is given then the output is
69 written to \'db_dependencies.png\'.
70 --format=... Format for the graphviz output. Defaults to
71 \'png\'. All values that the command \'dot\' accepts
72 for it\'s option \'-T\' are acceptable.
73 --nodeps List all database upgrades that no other upgrade
75 --apply=tag Applies the database upgrades \'tag\' and all
76 upgrades it depends on. If \'--apply\' is used
77 then the option \'--user\' must be used as well.
78 --applied List the applied database upgrades for the
79 database that the user given with \'--user\' uses.
80 --unapplied List the database upgrades that haven\'t been applied
81 yet to the database that the user given with
83 --test-utf8 Tests a PostgreSQL cluster for proper UTF-8 support.
84 You have to specify the database to test with the
85 parameters --dbname, --dbhost, --dbport, --dbuser
87 --help Show this help and exit.
90 --user=name The name of the user configuration to use for
91 database connectivity.
92 --dbname=name Database connection options for the UTF-8
93 --dbhost=host handling test.
109 sub calc_rev_depends {
110 map { $_->{rev_depends} = []; } values %{ $controls };
112 foreach my $control (values %{ $controls }) {
113 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
118 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
120 print "LIST VIEW\n\n" .
121 "number tag depth priority\n";
124 foreach (@sorted_controls) {
125 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
133 my ($tag, $depth) = @_;
135 print " " x $depth . $tag . "\n";
137 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
138 dump_node($dep_tag, $depth + 1);
143 print "TREE VIEW\n\n";
147 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
149 foreach my $control (@sorted_controls) {
150 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
156 sub dump_node_reverse {
157 my ($tag, $depth) = @_;
159 print " " x $depth . $tag . "\n";
161 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
162 dump_node_reverse($dep_tag, $depth + 1);
166 sub dump_tree_reverse {
167 print "REVERSE TREE VIEW\n\n";
171 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
173 foreach my $control (@sorted_controls) {
174 last if ($control->{depth} > 1);
175 dump_node_reverse($control->{tag}, "");
184 my $format = $params{format} || "png";
185 my $file_name = $params{file_name} || "db_dependencies.${format}";
187 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
188 print "Output will be written to '${file_name}'\n";
192 my $dot = "|dot -T${format} ";
193 open OUT, "${dot}> \"${file_name}\"" || die;
196 "digraph db_dependencies {\n" .
197 "graph [size=\"16.53,11.69!\"];\n" .
198 "node [shape=box style=filled fillcolor=white];\n";
201 foreach my $c (values %{ $controls }) {
202 $ranks{$c->{depth}} ||= [];
204 my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
205 (map "node [fillcolor=$_] ", qw(lightgray white));
207 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
210 foreach (sort keys %ranks) {
211 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
214 foreach my $c (values %{ $controls }) {
215 print OUT qq|"$c->{tag}";\n|;
217 foreach my $d (@{ $c->{depends} }) {
218 print OUT qq|"$c->{tag}" -> "$d";\n|;
229 print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
230 join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
237 my (@order, %tags, @all_tags);
239 if ($name eq "ALL") {
241 @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
244 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
248 foreach my $tag (@all_tags) {
249 build_upgrade_order($tag, \@order, \%tags);
252 my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
254 my $dbh = $form->dbconnect_noauto(\%myconfig);
256 $dbh->{PrintWarn} = 0;
257 $dbh->{PrintError} = 0;
259 $user->create_schema_info_table($form, $dbh);
261 my $query = qq|SELECT tag FROM schema_info|;
262 my $sth = $dbh->prepare($query);
263 $sth->execute() || $form->dberror($query);
264 while (my ($tag) = $sth->fetchrow_array()) {
265 $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
269 @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
270 if (!@upgradescripts) {
271 print "The upgrade has already been applied.\n";
275 foreach my $control (@upgradescripts) {
276 $control->{file} =~ /\.(sql|pl)$/;
280 print "Applying upgrade $control->{file}\n";
282 if ($file_type eq "sql") {
283 $dbupgrader->process_query($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
285 $dbupgrader->process_perl_script($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
292 sub dump_sql_result {
293 my ($results, $column_order) = @_;
295 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
297 foreach my $row (@{ $results }) {
298 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
302 if ($column_order && scalar @{ $column_order }) {
303 @sorted_names = @{ $column_order };
305 @sorted_names = sort keys %column_lengths;
308 my $format = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
310 printf $format, @sorted_names;
311 print join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
313 foreach my $row (@{ $results }) {
314 printf $format, map { $row->{$_} } @sorted_names;
316 printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
322 my $dbh = $form->dbconnect_noauto(\%myconfig);
324 $dbh->{PrintWarn} = 0;
325 $dbh->{PrintError} = 0;
327 $user->create_schema_info_table($form, $dbh);
329 my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
330 my $sth = $dbh->prepare($query);
331 $sth->execute() || $form->dberror($query);
332 while (my $ref = $sth->fetchrow_hashref()) {
339 if (!scalar @results) {
340 print "No database upgrades have been applied yet.\n";
342 dump_sql_result(\@results, [qw(tag login itime)]);
349 my $dbh = $form->dbconnect_noauto(\%myconfig);
351 $dbh->{PrintWarn} = 0;
352 $dbh->{PrintError} = 0;
354 my @unapplied = $dbupgrader->unapplied_upgrade_scripts($dbh);
358 if (!scalar @unapplied) {
359 print "All database upgrades have been applied.\n";
361 print map { $_->{tag} . "\n" } @unapplied;
365 sub build_upgrade_order {
370 my $control = $controls->{$name};
372 foreach my $dependency (@{ $control->{depends} }) {
373 next if $tags->{$dependency};
374 $tags->{$dependency} = 1;
375 build_upgrade_order($dependency, $order, $tags);
378 push @{ $order }, $name;
386 $locale = Locale->new;
393 GetOptions("list" => \$opt_list,
394 "tree" => \$opt_tree,
395 "rtree" => \$opt_rtree,
396 "nodeps" => \$opt_nodeps,
397 "graphviz:s" => \$opt_graphviz,
398 "format:s" => \$opt_format,
399 "user=s" => \$opt_user,
400 "apply=s" => \$opt_apply,
401 "applied" => \$opt_applied,
402 "unapplied" => \$opt_unapplied,
403 "test-utf8" => \$opt_test_utf8,
404 "dbhost:s" => \$opt_dbhost,
405 "dbport:s" => \$opt_dbport,
406 "dbname:s" => \$opt_dbname,
407 "dbuser:s" => \$opt_dbuser,
408 "dbpassword:s" => \$opt_dbpassword,
409 "help" => \$opt_help,
412 show_help() if ($opt_help);
414 $dbupgrader = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg');
415 $controls = $dbupgrader->parse_dbupdate_controls->{all_controls};
417 dump_list() if ($opt_list);
418 dump_tree() if ($opt_tree);
419 dump_tree_reverse() if ($opt_rtree);
420 dump_graphviz('file_name' => $opt_graphviz,
421 'format' => $opt_format) if (defined $opt_graphviz);
422 dump_nodeps() if ($opt_nodeps);
425 $auth = SL::Auth->new();
426 if (!$auth->session_tables_present()) {
427 $form->error("The session and user management tables are not present in the " .
428 "authentication database. Please use the administration web interface " .
429 "and to create them.");
432 %myconfig = $auth->read_user($opt_user);
434 if (!$myconfig{login}) {
435 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
438 $locale = new Locale($myconfig{countrycode}, "all");
439 $user = new User($opt_user);
441 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
445 $form->error("--apply used but no user name given with --user.") if (!$user);
446 apply_upgrade($opt_apply);
450 $form->error("--applied used but no user name given with --user.") if (!$user);
454 if ($opt_unapplied) {
455 $form->error("--unapplied used but no user name given with --user.") if (!$user);
459 if ($opt_test_utf8) {
460 $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
462 my $umlaut_upper = 'Ä';
464 my $dbconnect = "dbi:Pg:dbname=${opt_dbname}";
465 $dbconnect .= ";host=${opt_dbhost}" if ($opt_dbhost);
466 $dbconnect .= ";port=${opt_dbport}" if ($opt_dbport);
468 my $dbh = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
470 $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
472 my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
476 if ($umlaut_lower eq 'ä') {
477 print "UTF-8 test was successful.\n";
478 } elsif ($umlaut_lower eq 'Ä') {
479 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";
481 print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";