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.
17 use English '-no_match_vars';
27 read_config "config/lx_office.conf" => our %lx_office_conf;
28 our $lxdebug = LXDebug->new();
42 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
43 my ($opt_user, $opt_apply, $opt_applied, $opt_format, $opt_test_utf8);
44 my ($opt_dbhost, $opt_dbport, $opt_dbname, $opt_dbuser, $opt_dbpassword);
46 our (%myconfig, $form, $user, $auth, $locale, $controls);
49 my $help_text = <<"END_HELP"
50 dbupgrade2_tool.pl [options]
52 A validation and information tool for the database upgrade scripts
53 in \'sql/Pg-upgrade2\'.
55 At startup dbupgrade2_tool.pl will always check the consistency
56 of all database upgrade scripts (e.g. circular references, invalid
57 formats, missing meta information). You can but don\'t have to specifiy
61 --list Lists all database upgrade tags
62 --tree Lists all database upgrades in tree form
63 --rtree Lists all database upgrades in reverse tree form
64 --graphviz[=file] Create a Postscript document showing a tree of
65 all database upgrades and their dependencies.
66 If no file name is given then the output is
67 written to \'db_dependencies.png\'.
68 --format=... Format for the graphviz output. Defaults to
69 \'png\'. All values that the command \'dot\' accepts
70 for it\'s option \'-T\' are acceptable.
71 --nodeps List all database upgrades that no other upgrade
73 --apply=tag Applies the database upgrades \'tag\' and all
74 upgrades it depends on. If \'--apply\' is used
75 then the option \'--user\' must be used as well.
76 --applied List the applied database upgrades for the
77 database that the user given with \'--user\' uses.
78 --test-utf8 Tests a PostgreSQL cluster for proper UTF-8 support.
79 You have to specify the database to test with the
80 parameters --dbname, --dbhost, --dbport, --dbuser
82 --help Show this help and exit.
85 --user=name The name of the user configuration to use for
86 database connectivity.
87 --dbname=name Database connection options for the UTF-8
88 --dbhost=host handling test.
104 sub calc_rev_depends {
105 map { $_->{rev_depends} = []; } values %{ $controls };
107 foreach my $control (values %{ $controls }) {
108 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
113 my @sorted_controls = sort_dbupdate_controls($controls);
115 print "LIST VIEW\n\n" .
116 "number tag depth priority\n";
119 foreach (@sorted_controls) {
120 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
128 my ($tag, $depth) = @_;
130 print " " x $depth . $tag . "\n";
132 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
133 dump_node($dep_tag, $depth + 1);
138 print "TREE VIEW\n\n";
142 my @sorted_controls = sort_dbupdate_controls($controls);
144 foreach my $control (@sorted_controls) {
145 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
151 sub dump_node_reverse {
152 my ($tag, $depth) = @_;
154 print " " x $depth . $tag . "\n";
156 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
157 dump_node_reverse($dep_tag, $depth + 1);
161 sub dump_tree_reverse {
162 print "REVERSE TREE VIEW\n\n";
166 my @sorted_controls = sort_dbupdate_controls($controls);
168 foreach my $control (@sorted_controls) {
169 last if ($control->{depth} > 1);
170 dump_node_reverse($control->{tag}, "");
179 my $format = $params{format} || "png";
180 my $file_name = $params{file_name} || "db_dependencies.${format}";
182 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
183 print "Output will be written to '${file_name}'\n";
187 my $dot = "|dot -T${format} ";
188 open OUT, "${dot}> \"${file_name}\"" || die;
191 "digraph db_dependencies {\n" .
192 "graph [size=\"16.53,11.69!\"];\n" .
193 "node [shape=box style=filled fillcolor=white];\n";
196 foreach my $c (values %{ $controls }) {
197 $ranks{$c->{depth}} ||= [];
199 my ($pre, $post) = ('node [fillcolor=lightgray] ', 'node [fillcolor=white] ') if (!scalar @{ $c->{rev_depends} });
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 "$c->{tag};\n";
211 foreach my $d (@{ $c->{depends} }) {
212 print OUT "$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 $user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
279 $user->process_perl_script($form, $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;
362 $locale = Locale->new("de");
368 GetOptions("list" => \$opt_list,
369 "tree" => \$opt_tree,
370 "rtree" => \$opt_rtree,
371 "nodeps" => \$opt_nodeps,
372 "graphviz:s" => \$opt_graphviz,
373 "format:s" => \$opt_format,
374 "user=s" => \$opt_user,
375 "apply=s" => \$opt_apply,
376 "applied" => \$opt_applied,
377 "test-utf8" => \$opt_test_utf8,
378 "dbhost:s" => \$opt_dbhost,
379 "dbport:s" => \$opt_dbport,
380 "dbname:s" => \$opt_dbname,
381 "dbuser:s" => \$opt_dbuser,
382 "dbpassword:s" => \$opt_dbpassword,
383 "help" => \$opt_help,
386 show_help() if ($opt_help);
388 $controls = parse_dbupdate_controls($form, "Pg");
390 dump_list() if ($opt_list);
391 dump_tree() if ($opt_tree);
392 dump_tree_reverse() if ($opt_rtree);
393 dump_graphviz('file_name' => $opt_graphviz,
394 'format' => $opt_format) if (defined $opt_graphviz);
395 dump_nodeps() if ($opt_nodeps);
398 $auth = SL::Auth->new();
399 if (!$auth->session_tables_present()) {
400 $form->error("The session and user management tables are not present in the " .
401 "authentication database. Please use the administration web interface " .
402 "and to create them.");
405 %myconfig = $auth->read_user($opt_user);
407 if (!$myconfig{login}) {
408 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
411 $locale = new Locale($myconfig{countrycode}, "all");
412 $user = new User($opt_user);
414 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
418 $form->error("--apply used but no user name given with --user.") if (!$user);
419 apply_upgrade($opt_apply);
423 $form->error("--applied used but no user name given with --user.") if (!$user);
427 if ($opt_test_utf8) {
428 $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
430 my $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, { pg_enable_utf8 => 1 });
438 $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
440 my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
444 if ($umlaut_lower eq 'ä') {
445 print "UTF-8 test was successful.\n";
446 } elsif ($umlaut_lower eq 'Ä') {
447 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";
449 print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";