4 if (! -d "bin" || ! -d "SL") {
5 print("This tool must be run from the kivitendo 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, $opt_create, $opt_type);
47 my ($opt_description, $opt_encoding, @opt_depends);
49 our (%myconfig, $form, $user, $auth, $locale, $controls, $dbupgrader);
52 my $help_text = <<"END_HELP"
53 dbupgrade2_tool.pl [options]
55 A validation and information tool for the database upgrade scripts
56 in \'sql/Pg-upgrade2\'.
58 At startup dbupgrade2_tool.pl will always check the consistency
59 of all database upgrade scripts (e.g. circular references, invalid
60 formats, missing meta information). You can but don\'t have to specifiy
64 --list Lists all database upgrade tags
65 --tree Lists all database upgrades in tree form
66 --rtree Lists all database upgrades in reverse tree form
67 --graphviz[=file] Create a Postscript document showing a tree of
68 all database upgrades and their dependencies.
69 If no file name is given then the output is
70 written to \'db_dependencies.png\'.
71 --format=... Format for the graphviz output. Defaults to
72 \'png\'. All values that the command \'dot\' accepts
73 for it\'s option \'-T\' are acceptable.
74 --nodeps List all database upgrades that no other upgrade
76 --create=tag Creates a new upgrade with the supplied tag. This
77 action accepts several optional other options. See
78 the option section for those. After creating the
79 upgrade file your \$EDITOR will be called with it.
80 --apply=tag Applies the database upgrades \'tag\' and all
81 upgrades it depends on. If \'--apply\' is used
82 then the option \'--user\' must be used as well.
83 --applied List the applied database upgrades for the
84 database that the user given with \'--user\' uses.
85 --unapplied List the database upgrades that haven\'t been applied
86 yet to the database that the user given with
88 --test-utf8 Tests a PostgreSQL cluster for proper UTF-8 support.
89 You have to specify the database to test with the
90 parameters --dbname, --dbhost, --dbport, --dbuser
92 --help Show this help and exit.
95 --user=name The name of the user configuration to use for
96 database connectivity.
97 --dbname=name Database connection options for the UTF-8
98 --dbhost=host handling test.
103 Options for --create:
104 --type \'sql\' or \'pl\'. Defaults to sql.
105 --description The description field of the generated upgrade.
106 --encoding Encoding used for the file. Defaults to \'utf8\'.
107 Note: Your editor will not be told to open the file in
109 --depends Tags of upgrades which this upgrade depends upon.
110 Defaults to the latest stable release upgrade.
111 Multiple values possible.
124 sub calc_rev_depends {
125 map { $_->{rev_depends} = []; } values %{ $controls };
127 foreach my $control (values %{ $controls }) {
128 map { push @{ $controls->{$_}->{rev_depends} }, $control->{tag} } @{ $control->{depends} };
133 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
135 print "LIST VIEW\n\n" .
136 "number tag depth priority\n";
139 foreach (@sorted_controls) {
140 print "$i $_->{tag} $_->{depth} $_->{priority}\n";
148 my ($tag, $depth) = @_;
150 print " " x $depth . $tag . "\n";
152 foreach my $dep_tag (@{ $controls->{$tag}->{depends} }) {
153 dump_node($dep_tag, $depth + 1);
158 print "TREE VIEW\n\n";
162 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
164 foreach my $control (@sorted_controls) {
165 dump_node($control->{tag}, "") unless (@{ $control->{rev_depends} });
171 sub dump_node_reverse {
172 my ($tag, $depth) = @_;
174 print " " x $depth . $tag . "\n";
176 foreach my $dep_tag (@{ $controls->{$tag}->{rev_depends} }) {
177 dump_node_reverse($dep_tag, $depth + 1);
181 sub dump_tree_reverse {
182 print "REVERSE TREE VIEW\n\n";
186 my @sorted_controls = $dbupgrader->sort_dbupdate_controls;
188 foreach my $control (@sorted_controls) {
189 last if ($control->{depth} > 1);
190 dump_node_reverse($control->{tag}, "");
199 my $format = $params{format} || "png";
200 my $file_name = $params{file_name} || "db_dependencies.${format}";
202 print "GRAPHVIZ OUTPUT -- format: ${format}\n\n";
203 print "Output will be written to '${file_name}'\n";
207 my $dot = "|dot -T${format} ";
208 open OUT, "${dot}> \"${file_name}\"" || die;
211 "digraph db_dependencies {\n" .
212 "graph [size=\"16.53,11.69!\"];\n" .
213 "node [shape=box style=filled fillcolor=white];\n";
216 foreach my $c (values %{ $controls }) {
217 $ranks{$c->{depth}} ||= [];
219 my ($pre, $post) = @{ $c->{rev_depends} } ? ('')x2 :
220 (map "node [fillcolor=$_] ", qw(lightgray white));
222 push @{ $ranks{$c->{"depth"}} }, qq|${pre}"$c->{tag}"; ${post}|;
225 foreach (sort keys %ranks) {
226 print OUT "{ rank = same; ", join("", @{ $ranks{$_} }), " }\n";
229 foreach my $c (values %{ $controls }) {
230 print OUT qq|"$c->{tag}";\n|;
232 foreach my $d (@{ $c->{depends} }) {
233 print OUT qq|"$c->{tag}" -> "$d";\n|;
244 print "SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
245 join("\n", map { $_->{tag} } grep { !scalar @{ $_->{rev_depends} } } values %{ $controls }) .
252 my $filename = $params{filename};
253 my $dbupgrader = $params{dbupgrader};
254 my $type = $params{type} || 'sql';
255 my $description = $params{description} || '';
256 my $encoding = $params{encoding} || 'utf-8';
257 my @depends = @{ $params{depends} };
260 my @releases = grep { /^release_/ } keys %$controls;
261 @depends = ((sort @releases)[-1]);
265 if ($type eq 'sql') {
267 } elsif ($type eq 'pl') {
270 die 'Error: No --type was given but is required for --create.';
272 die 'Error: Unknown --type. Try "sql" or "pl".';
275 my $full_filename = $dbupgrader->path . '/' . $filename . '.' . $type;
277 die "file '$full_filename' already exists, aborting" if -f $full_filename;
280 open my $fh, ">:encoding($encoding)", $full_filename or die "can't open $full_filename";
281 print $fh "$comment \@tag: $filename\n";
282 print $fh "$comment \@description: $description\n";
283 print $fh "$comment \@depends: @depends\n";
284 print $fh "$comment \@encoding: $encoding\n";
287 print "File $full_filename created.\n";
289 system("\$EDITOR $full_filename");
296 my (@order, %tags, @all_tags);
298 if ($name eq "ALL") {
300 @all_tags = map { $_->{tag} } grep { !@{$_->{rev_depends}} } values %{ $controls };
303 $form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name});
307 foreach my $tag (@all_tags) {
308 build_upgrade_order($tag, \@order, \%tags);
311 my @upgradescripts = map { $controls->{$_}->{applied} = 0; $controls->{$_} } @order;
313 my $dbh = $form->dbconnect_noauto(\%myconfig);
315 $dbh->{PrintWarn} = 0;
316 $dbh->{PrintError} = 0;
318 $user->create_schema_info_table($form, $dbh);
320 my $query = qq|SELECT tag FROM schema_info|;
321 my $sth = $dbh->prepare($query);
322 $sth->execute() || $form->dberror($query);
323 while (my ($tag) = $sth->fetchrow_array()) {
324 $controls->{$tag}->{applied} = 1 if defined $controls->{$tag};
328 @upgradescripts = sort { $a->{priority} <=> $b->{priority} } grep { !$_->{applied} } @upgradescripts;
329 if (!@upgradescripts) {
330 print "The upgrade has already been applied.\n";
334 foreach my $control (@upgradescripts) {
335 $control->{file} =~ /\.(sql|pl)$/;
339 print "Applying upgrade $control->{file}\n";
341 if ($file_type eq "sql") {
342 $dbupgrader->process_query($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
344 $dbupgrader->process_perl_script($dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control);
351 sub dump_sql_result {
352 my ($results, $column_order) = @_;
354 my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
356 foreach my $row (@{ $results }) {
357 map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
361 if ($column_order && scalar @{ $column_order }) {
362 @sorted_names = @{ $column_order };
364 @sorted_names = sort keys %column_lengths;
367 my $format = join('|', map { '%-' . $column_lengths{$_} . 's' } @sorted_names) . "\n";
369 printf $format, @sorted_names;
370 print join('+', map { '-' x $column_lengths{$_} } @sorted_names) . "\n";
372 foreach my $row (@{ $results }) {
373 printf $format, map { $row->{$_} } @sorted_names;
375 printf "(\%d row\%s)\n", scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '';
381 my $dbh = $form->dbconnect_noauto(\%myconfig);
383 $dbh->{PrintWarn} = 0;
384 $dbh->{PrintError} = 0;
386 $user->create_schema_info_table($form, $dbh);
388 my $query = qq|SELECT tag, login, itime FROM schema_info ORDER BY itime|;
389 my $sth = $dbh->prepare($query);
390 $sth->execute() || $form->dberror($query);
391 while (my $ref = $sth->fetchrow_hashref()) {
398 if (!scalar @results) {
399 print "No database upgrades have been applied yet.\n";
401 dump_sql_result(\@results, [qw(tag login itime)]);
408 my $dbh = $form->dbconnect_noauto(\%myconfig);
410 $dbh->{PrintWarn} = 0;
411 $dbh->{PrintError} = 0;
413 my @unapplied = $dbupgrader->unapplied_upgrade_scripts($dbh);
417 if (!scalar @unapplied) {
418 print "All database upgrades have been applied.\n";
420 print map { $_->{tag} . "\n" } @unapplied;
424 sub build_upgrade_order {
429 my $control = $controls->{$name};
431 foreach my $dependency (@{ $control->{depends} }) {
432 next if $tags->{$dependency};
433 $tags->{$dependency} = 1;
434 build_upgrade_order($dependency, $order, $tags);
437 push @{ $order }, $name;
445 $locale = Locale->new;
452 GetOptions("list" => \$opt_list,
453 "tree" => \$opt_tree,
454 "rtree" => \$opt_rtree,
455 "nodeps" => \$opt_nodeps,
456 "graphviz:s" => \$opt_graphviz,
457 "format:s" => \$opt_format,
458 "user=s" => \$opt_user,
459 "apply=s" => \$opt_apply,
460 "applied" => \$opt_applied,
461 "create=s" => \$opt_create,
462 "type=s" => \$opt_type,
463 "encoding=s" => \$opt_encoding,
464 "description=s" => \$opt_description,
465 "depends=s" => \@opt_depends,
466 "unapplied" => \$opt_unapplied,
467 "test-utf8" => \$opt_test_utf8,
468 "dbhost:s" => \$opt_dbhost,
469 "dbport:s" => \$opt_dbport,
470 "dbname:s" => \$opt_dbname,
471 "dbuser:s" => \$opt_dbuser,
472 "dbpassword:s" => \$opt_dbpassword,
473 "help" => \$opt_help,
476 show_help() if ($opt_help);
478 $dbupgrader = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg');
479 $controls = $dbupgrader->parse_dbupdate_controls->{all_controls};
481 dump_list() if ($opt_list);
482 dump_tree() if ($opt_tree);
483 dump_tree_reverse() if ($opt_rtree);
484 dump_graphviz('file_name' => $opt_graphviz,
485 'format' => $opt_format) if (defined $opt_graphviz);
486 dump_nodeps() if ($opt_nodeps);
487 create_upgrade(filename => $opt_create,
488 dbupgrader => $dbupgrader,
490 description => $opt_description,
491 encoding => $opt_encoding,
492 depends => \@opt_depends) if ($opt_create);
495 $auth = SL::Auth->new();
496 if (!$auth->session_tables_present()) {
497 $form->error("The session and user management tables are not present in the " .
498 "authentication database. Please use the administration web interface " .
499 "and to create them.");
502 %myconfig = $auth->read_user(login => $opt_user);
504 if (!$myconfig{login}) {
505 $form->error($form->format_string("The user '#1' does not exist.", $opt_user));
508 $locale = new Locale($myconfig{countrycode}, "all");
509 $user = new User(login => $opt_user);
511 map { $form->{$_} = $myconfig{$_} } keys %myconfig;
515 $form->error("--apply used but no user name given with --user.") if (!$user);
516 apply_upgrade($opt_apply);
520 $form->error("--applied used but no user name given with --user.") if (!$user);
524 if ($opt_unapplied) {
525 $form->error("--unapplied used but no user name given with --user.") if (!$user);
530 if ($opt_test_utf8) {
531 $form->error("--test-utf8 used but no database name given with --dbname.") if (!$opt_dbname);
533 my $umlaut_upper = 'Ä';
535 my $dbconnect = "dbi:Pg:dbname=${opt_dbname}";
536 $dbconnect .= ";host=${opt_dbhost}" if ($opt_dbhost);
537 $dbconnect .= ";port=${opt_dbport}" if ($opt_dbport);
539 my $dbh = DBI->connect($dbconnect, $opt_dbuser, $opt_dbpassword, { pg_enable_utf8 => 1 });
541 $form->error("UTF-8 test: Database connect failed (" . $DBI::errstr . ")") if (!$dbh);
543 my ($umlaut_lower) = $dbh->selectrow_array(qq|SELECT lower(?)|, undef, $umlaut_upper);
547 if ($umlaut_lower eq 'ä') {
548 print "UTF-8 test was successful.\n";
549 } elsif ($umlaut_lower eq 'Ä') {
550 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";
552 print "UTF-8 test was NOT successful: Umlauts are destroyed. Do not use UTF-8 on this cluster.\n";