From 1e6475427d9451a31ebf65611831b5e72af14444 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 14 Jun 2021 18:44:06 +0200 Subject: [PATCH] Bug 25078: Separate update "report" from its description Signed-off-by: Martin Renvoize --- C4/Installer.pm | 44 +++++++++-------------- installer/data/mysql/db_revs/210600000.pl | 12 +++++-- installer/data/mysql/db_revs/210600081.pl | 2 +- installer/data/mysql/db_revs/210600082.pl | 10 +++--- installer/data/mysql/db_revs/210600083.pl | 11 ++++-- installer/data/mysql/db_revs/210600090.pl | 5 +-- installer/data/mysql/db_revs/210600091.pl | 2 +- installer/data/mysql/db_revs/210600092.pl | 11 ++++-- installer/data/mysql/db_revs/210600094.pl | 5 +-- installer/data/mysql/db_revs/skeleton.pl | 7 ++-- 10 files changed, 63 insertions(+), 46 deletions(-) diff --git a/C4/Installer.pm b/C4/Installer.pm index cd563e2f27..063c135ebd 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -724,10 +724,12 @@ sub update { my $error; + my $out = ''; + open my $outfh, '>', \$out; try { $schema->txn_do( sub { - $db_rev->{up}->(); + $db_rev->{up}->({ dbh => $schema->storage->dbh, out => $outfh }); } ); } catch { @@ -736,13 +738,11 @@ sub update { my $db_entry = { bug_number => $db_rev->{bug_number}, - description => ( ref $db_rev->{description} eq 'CODE' ) - ? $db_rev->{description}->() - : $db_rev->{description}, + description => $db_rev->{description}, version => version_from_file($file), time => POSIX::strftime( "%H:%M:%S", localtime ), }; - $db_entry->{output} = output_version( { %$db_entry, done => !$error } ); + $db_entry->{output} = output_version( { %$db_entry, done => !$error, report => $out } ); if ( $error ) { push @errors, { %$db_entry, error => $error }; @@ -759,7 +759,8 @@ sub update { sub output_version { my ( $db_entry ) = @_; - my $descriptions = $db_entry->{description}; + my $description = $db_entry->{description}; + my $report = $db_entry->{report}; my $DBversion = $db_entry->{version}; my $bug_number = $db_entry->{bug_number}; my $time = $db_entry->{time}; @@ -769,31 +770,20 @@ sub output_version { : " failed" : ""; # For old versions, we don't know if we succeed or failed - unless ( ref($descriptions) ) { - $descriptions = [ $descriptions ]; + my @output; + + if ($bug_number) { + push @output, sprintf('Upgrade to %s %s [%s]: Bug %5s - %s', $DBversion, $done, $time, $bug_number, $description); + } else { + push @output, sprintf('Upgrade to %s %s [%s]: %s', $DBversion, $done, $time, $description); } - my $first = 1; - my @output; - for my $description ( @$descriptions ) { - if ( @$descriptions > 1 ) { - if ( $first ) { - unless ( $bug_number ) { - push @output, sprintf "Upgrade to %s%s [%s]:", $DBversion, $done, $time; - } else { - push @output, sprintf "Upgrade to %s%s [%s]: Bug %5s", $DBversion, $done, $time, $bug_number; - } - } - push @output, sprintf "\t\t\t\t\t\t - %s", $description; - } else { - unless ( $bug_number ) { - push @output, sprintf "Upgrade to %s%s [%s]: %s", $DBversion, $done, $time, $description; - } else { - push @output, sprintf "Upgrade to %s%s [%s]: Bug %5s - %s", $DBversion, $done, $time, $bug_number, $description; - } + if ($report) { + foreach my $line (split /\n/, $report) { + push @output, sprintf "\t\t\t\t\t\t - %s", $line; } - $first = 0; } + return \@output; } diff --git a/installer/data/mysql/db_revs/210600000.pl b/installer/data/mysql/db_revs/210600000.pl index fecc9f081f..2e120d4193 100644 --- a/installer/data/mysql/db_revs/210600000.pl +++ b/installer/data/mysql/db_revs/210600000.pl @@ -3,6 +3,14 @@ use utf8; { bug_number => undef, - description => ["🎵 Run, rabbit run. 🎶", "Dig that hole, forget the sun,", "And when at last the work is done", "Don't sit down it's time to dig another one."], - up => sub {}, + description => 'Increase DBRev for 21.06', + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + say $out '🎵 Run, rabbit run. 🎶'; + say $out 'Dig that hole, forget the sun,'; + say $out 'And when at last the work is done'; + say $out "Don't sit down it's time to dig another one."; + }, } diff --git a/installer/data/mysql/db_revs/210600081.pl b/installer/data/mysql/db_revs/210600081.pl index 70e2fef187..1bde087f04 100644 --- a/installer/data/mysql/db_revs/210600081.pl +++ b/installer/data/mysql/db_revs/210600081.pl @@ -2,6 +2,6 @@ use Modern::Perl; { bug_number => 42322, - description => "Single line description", + description => "Single line description, no report", up => sub {}, } diff --git a/installer/data/mysql/db_revs/210600082.pl b/installer/data/mysql/db_revs/210600082.pl index bd443a444d..bf77b47c2f 100644 --- a/installer/data/mysql/db_revs/210600082.pl +++ b/installer/data/mysql/db_revs/210600082.pl @@ -1,11 +1,13 @@ use Modern::Perl; -my $nb_patrons; { bug_number => 42422, - description => sub { ["Testing number of patrons: $nb_patrons"] }, + description => 'Testing number of patrons', up => sub { - my $dbh = C4::Context->dbh; - $nb_patrons = $dbh->selectrow_array(q{SELECT COUNT(*) FROM borrowers;}); + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + my $nb_patrons = $dbh->selectrow_array(q{SELECT COUNT(*) FROM borrowers;}); + say $out "Number of patrons: $nb_patrons"; }, } diff --git a/installer/data/mysql/db_revs/210600083.pl b/installer/data/mysql/db_revs/210600083.pl index b4e32550d7..b005a3e935 100644 --- a/installer/data/mysql/db_revs/210600083.pl +++ b/installer/data/mysql/db_revs/210600083.pl @@ -2,6 +2,13 @@ use Modern::Perl; { bug_number => 42323, - description => [qw(Multi lines description)], - up => sub {}, + description => 'Multi lines report', + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + say $out 'Multi'; + say $out 'lines'; + say $out 'report'; + }, } diff --git a/installer/data/mysql/db_revs/210600090.pl b/installer/data/mysql/db_revs/210600090.pl index 802b446c4f..4ea0431a05 100644 --- a/installer/data/mysql/db_revs/210600090.pl +++ b/installer/data/mysql/db_revs/210600090.pl @@ -2,9 +2,10 @@ use Modern::Perl; { bug_number => 42420, - description => ["Testing failure"], + description => 'Testing failure', up => sub { - my $dbh = C4::Context->dbh; + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; $dbh->do(q{ALTER TABLE Foo}); }, } diff --git a/installer/data/mysql/db_revs/210600091.pl b/installer/data/mysql/db_revs/210600091.pl index 1a34a20e2b..bf87ed8f0b 100644 --- a/installer/data/mysql/db_revs/210600091.pl +++ b/installer/data/mysql/db_revs/210600091.pl @@ -2,7 +2,7 @@ use Modern::Perl; { bug_number => 42421, - description => ["don't compile"], + description => "don't compile", up => sub { my $dbh = C4::NoContext->dbh; # NoContext does not exist $dbh->do(q{ALTER TABLE Foo}); diff --git a/installer/data/mysql/db_revs/210600092.pl b/installer/data/mysql/db_revs/210600092.pl index 9add21534c..80911a7a38 100644 --- a/installer/data/mysql/db_revs/210600092.pl +++ b/installer/data/mysql/db_revs/210600092.pl @@ -2,9 +2,16 @@ use Modern::Perl; { bug_number => 42423, - description => ["Testing", "multi lines", " failure"], + description => 'Testing multi lines failure', up => sub { - my $dbh = C4::Context->dbh; + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + say $out 'Testing'; + say $out 'multi lines'; + $dbh->do(q{ALTER TABLE Foo}); + + say $out 'failure'; # this won't be printed }, } diff --git a/installer/data/mysql/db_revs/210600094.pl b/installer/data/mysql/db_revs/210600094.pl index 01aa992c29..ac25455d21 100644 --- a/installer/data/mysql/db_revs/210600094.pl +++ b/installer/data/mysql/db_revs/210600094.pl @@ -2,9 +2,10 @@ use Modern::Perl; { bug_number => 42424, - description => ["Testing transaction"], + description => 'Testing transaction', up => sub { - my $dbh = C4::Context->dbh; + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; $dbh->do(q{INSERT INTO cities(city_name) VALUES ('new_city');}); $dbh->do(q{INSERT INTO cities(city_XX) VALUES ('new_XX');}); # This will fail }, diff --git a/installer/data/mysql/db_revs/skeleton.pl b/installer/data/mysql/db_revs/skeleton.pl index 76b4991ad2..082f47e024 100644 --- a/installer/data/mysql/db_revs/skeleton.pl +++ b/installer/data/mysql/db_revs/skeleton.pl @@ -3,11 +3,12 @@ use Modern::Perl; { bug_number => "BUG_NUMBER", description => "A single line description", - # description => ["Multi", "lines", "description"], - # description => sub { return ["Your dynamic description"] }, up => sub { - my $dbh = C4::Context->dbh; + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; # Do you stuffs here $dbh->do(q{}); + # Print useful stuff here + say $out "Update is going well so far"; }, } -- 2.20.1