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';
22 $lxdebug = LXDebug->new();
35 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
36 my ($opt_user, $opt_apply, $opt_applied, $opt_format, $opt_test_utf8);
37 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword);
39 our (%myconfig, $form, $user, $auth);
42 my $help_text = <<"END_HELP"
43 dbupgrade2_tool.pl [options]
45 A validation and information tool for the database upgrade scripts
46 in \'sql/Pg-upgrade2\'.
48 At startup dbupgrade2_tool.pl will always check the consistency
49 of all database upgrade scripts (e.g. circular references, invalid
50 formats, missing meta information). You can but don\'t have to specifiy
54 --list Lists all database upgrade tags
55 --tree Lists all database upgrades in tree form
56 --rtree Lists all database upgrades in reverse tree form
57 --graphviz[=file] Create a Postscript document showing a tree of
58 all database upgrades and their dependencies.
59 If no file name is given then the output is
60 written to \'db_dependencies.png\'.
61 --format=... Format for the graphviz output. Defaults to
62 \'png\'. All values that the command \'dot\' accepts
63 for it\'s option \'-T\' are acceptable.
64 --nodeps List all database upgrades that no other upgrade
66 --apply=tag Applies the database upgrades \'tag\' and all
67 upgrades it depends on. If \'--apply\' is used
68 then the option \'--user\' must be used as well.
69 --applied List the applied database upgrades for the
70 database that the user given with \'--user\' uses.
71 --test-utf8 Tests a PostgreSQL cluster for proper UTF-8 support.
72 You have to specify the database to test with the
73 parameters --dbname, --dbhost, --dbport, --dbuser
75 --help Show this help and exit.
78 --user=name The name of the user configuration to use for
79 database connectivity.
80 --dbname=name Database connection options for the UTF-8
81 --dbhost=host handling test.
97 sub calc_rev_depends {
98 map { $_->{rev_depends} = []; } values %{ $controls };
100 foreach my $control (values %{ $controls }) {
101 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
106 my @sorted_controls = sort_dbupdate_controls($controls);
108 print "LIST VIEW\n\n" .
109 "number tag depth priority\n";
112 foreach (@sorted_controls) {
113 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
121 my ($tag, $depth) = @_;
123 print " " x $depth . $tag . "\n";
125 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
126 dump_node($dep_tag, $depth + 1);
131 print "TREE VIEW\n\n";
135 my @sorted_controls = sort_dbupdate_controls($controls);
137 foreach my $control (@sorted_controls) {
138 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
144 sub dump_node_reverse {
145 my ($tag, $depth) = @_;
147 print " " x $depth . $tag . "\n";
149 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
150 dump_node_reverse($dep_tag, $depth + 1);
154 sub dump_tree_reverse {
155 print "REVERSE TREE VIEW\n\n";
159 my @sorted_controls = sort_dbupdate_controls($controls);
161 foreach my $control (@sorted_controls) {
162 last if ($control->{depth} > 1);
163 dump_node_reverse($control->{tag}, "");
172 my $format = $params{format} || "png";
173 my $file_name = $params{file_name} || "db_dependencies.${format}";
175 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
176 print "Output will be written to '${file_name}'\n";
180 $dot = "|dot -T${format} ";
181 open OUT, "${dot}> \"${file_name}\"" || die;
184 "digraph db_dependencies {\n" .
185 "graph [size=\"16.53,11.69!\"];\n" .
186 "node [shape=box style=filled fillcolor=white];\n";
189 foreach my $c (values %{ $controls }) {
190 $ranks{$c->{depth}} ||= [];
192 my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if (!scalar @{ $c->{rev_depends} });
194 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
197 foreach (sort keys %ranks) {
198 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
201 foreach my $c (values %{ $controls }) {
202 print OUT "$c->{tag};\n";
204 foreach my $d (@{ $c->{depends} }) {
205 print OUT "$c->{tag} -> $d;\n";
216 print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
217 join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
224 my (@order, %tags, @all_tags);
226 if ($name eq "ALL") {
228 @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
231 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
235 foreach my $tag (@all_tags) {
236 build_upgrade_order($tag, \@order, \%tags);
239 my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
241 my $dbh = $form->dbconnect_noauto(\%myconfig);
243 $dbh->{PrintWarn} = 0;
244 $dbh->{PrintError} = 0;
246 $user->create_schema_info_table($form, $dbh);
248 my $query = qq|SELECT tag FROM schema_info|;
249 $sth = $dbh->prepare($query);
250 $sth->execute() || $form->dberror($query);
251 while (($tag) = $sth->fetchrow_array()) {
252 $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
256 @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
257 if (!@upgradescripts) {
258 print "The upgrade has already been applied.\n";
262 foreach my $control (@upgradescripts) {
263 $control->{file} =~ /\.(sql|pl)$/;
267 print "Applying upgrade $control->{file}\n";
269 if ($file_type eq "sql") {
270 $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
272 $user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
279 sub dump_sql_result {
280 my ($results, $column_order) = @_;
282 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
284 foreach my $row (@{ $results }) {
285 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
289 if ($column_order && scalar @{ $column_order }) {
290 @sorted_names = @{ $column_order };
292 @sorted_names = sort keys %column_lengths;
295 my $format = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
297 printf $format, @sorted_names;
298 print join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
300 foreach my $row (@{ $results }) {
301 printf $format, map { $row->{$_} } @sorted_names;
303 printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
309 my $dbh = $form->dbconnect_noauto(\%myconfig);
311 $dbh->{PrintWarn} = 0;
312 $dbh->{PrintError} = 0;
314 $user->create_schema_info_table($form, $dbh);
316 my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
317 $sth = $dbh->prepare($query);
318 $sth->execute() || $form->dberror($query);
319 while (my $ref = $sth->fetchrow_hashref()) {
326 if (!scalar @results) {
327 print "No database upgrades have been applied yet.\n";
329 dump_sql_result(\@results, [qw(tag login itime)]);
333 sub build_upgrade_order {
338 my $control = $controls->{$name};
340 foreach my $dependency (@{ $control->{depends} }) {
341 next if $tags->{$dependency};
342 $tags->{$dependency} = 1;
343 build_upgrade_order($dependency, $order, $tag);
346 push @{ $order }, $name;
354 eval { require "config/lx-erp.conf"; };
355 eval { require "config/lx-erp-local.conf"; } if (-f "config/lx-erp-local.conf");
358 $locale = Locale->new("de", "login");
364 GetOptions("list" => \$opt_list,
365 "tree" => \$opt_tree,
366 "rtree" => \$opt_rtree,
367 "nodeps" => \$opt_nodeps,
368 "graphviz:s" => \$opt_graphviz,
369 "format:s" => \$opt_format,
370 "user=s" => \$opt_user,
371 "apply=s" => \$opt_apply,
372 "applied" => \$opt_applied,
373 "test-utf8" => \$opt_test_utf8,
374 "dbhost:s" => \$opt_dbhost,
375 "dbport:s" => \$opt_dbport,
376 "dbname:s" => \$opt_dbname,
377 "dbuser:s" => \$opt_dbuser,
378 "dbpassword:s" => \$opt_dbpassword,
379 "help" => \$opt_help,
382 show_help() if ($opt_help);
384 $controls = parse_dbupdate_controls($form, "Pg");
386 dump_list() if ($opt_list);
387 dump_tree() if ($opt_tree);
388 dump_tree_reverse() if ($opt_rtree);
389 dump_graphviz('file_name' => $opt_graphviz,
390 'format' => $opt_format) if (defined $opt_graphviz);
391 dump_nodeps() if ($opt_nodeps);
394 $auth = SL::Auth->new();
395 if (!$auth->session_tables_present()) {
396 $form->error("The session and user management tables are not present in the " .
397 "authentication database. Please use the administration web interface " .
398 "and to create them.");
401 %myconfig = $auth->read_user($opt_user);
403 if (!$myconfig{login}) {
404 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
407 $locale = new Locale($myconfig{countrycode}, "all");
408 $user = new User($opt_user);
410 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
414 $form->error("--apply used but no user name given with --user.") if (!$user);
415 apply_upgrade($opt_apply);
419 $form->error("--applied used but no user name given with --user.") if (!$user);
423 if ($opt_test_utf8) {
424 $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
426 my $iconv_to_utf8 = Text::Iconv->new("ISO-8859-15", "UTF-8");
427 my $iconv_from_utf8 = Text::Iconv->new("UTF-8", "ISO-8859-15");
429 my $umlaut_upper = 'Ä';
430 my $umlaut_upper_utf8 = $iconv_to_utf8->convert($umlaut_upper);
432 my $dbconnect = "dbi:Pg:dbname=${opt_dbname}";
433 $dbconnect .= ";host=${opt_dbhost}" if ($opt_dbhost);
434 $dbconnect .= ";port=${opt_dbport}" if ($opt_dbport);
436 my $dbh = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword);
438 $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
440 my ($umlaut_lower_utf8) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper_utf8);
444 my $umlaut_lower = $iconv_from_utf8->convert($umlaut_lower_utf8);
446 if ($umlaut_lower eq 'ä') {
447 print "UTF-8 test was successful.\n";
448 } elsif ($umlaut_lower eq 'Ä') {
449 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";
451 print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";