Skip to content

Commit b7c7075

Browse files
ammoptLMSCloudPaulD
authored andcommitted
Bug 36039: Move audit_database script logic to a dedicated module
Test plan #1 ensure same script functionality (same as bug 34064): 0. Apply patch 1. vi ./installer/data/mysql/kohastructure.sql 2. Comment out some columns, change NULL status, or whatever you like 3. perl misc/maintenance/audit_database.pl \ --filename /kohadevbox/koha/installer/data/mysql/kohastructure.sql 4. Note that the output includes SQL commands to change the database to match the new kohastructure.sql 5a. Try using koha-foreach and note that the database name appears above the database comparison 5b. koha-foreach "perl misc/maintenance/audit_database.pl \ --filename /kohadevbox/koha/installer/data/mysql/kohastructure.sql" Test plan #2: 1. After doing test plan #1, access the new 'database audit' tab, visit: <staff_url>/cgi-bin/koha/about.pl?tab=database 2. Ensure the output is the same as test plan #1 Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: JesseM <jesse@bywatersolutions.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de> (cherry picked from commit 09bb854) Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
1 parent 36bf932 commit b7c7075

File tree

2 files changed

+162
-70
lines changed

2 files changed

+162
-70
lines changed

Koha/Database/Auditor.pm

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package Koha::Database::Auditor;
2+
3+
# This file is part of Koha.
4+
#
5+
# Koha is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Koha is distributed in the hope that it will be useful, but
11+
# WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17+
18+
use Modern::Perl;
19+
20+
use SQL::Translator;
21+
use SQL::Translator::Diff;
22+
23+
use C4::Context;
24+
25+
=head1 NAME
26+
27+
Koha::Database::Auditor
28+
29+
=head1 SYNOPSIS
30+
31+
use Koha::Database::Auditor;
32+
33+
=head1 API
34+
35+
=head2 Methods
36+
37+
=head3 new
38+
39+
Constructor for creating a new object with specified parameters
40+
41+
=cut
42+
43+
sub new {
44+
my ( $class, $params ) = @_;
45+
my $self = bless $params // {}, $class;
46+
$self->{filename} = $params->{filename} // './installer/data/mysql/kohastructure.sql';
47+
$self->{is_cli} = $params->{is_cli};
48+
return $self;
49+
}
50+
51+
=head3 run
52+
53+
Run the database audit with the given arguments, including the filename
54+
and whether it is a command-line interface(CLI).
55+
Returns $diff only if $is_cli is false. Else it just prints $diff.
56+
57+
=cut
58+
59+
sub run {
60+
my ( $self, $args ) = @_;
61+
62+
if ( !-f $self->{filename} ) {
63+
unless ( $self->{is_cli} ) {
64+
return 'Database schema file not found';
65+
}
66+
die("Filename '$self->{filename}' does not exist\n");
67+
}
68+
69+
my $sql_schema = $self->_get_kohastructure();
70+
my $db_schema = $self->_get_db();
71+
72+
if ( $sql_schema && $db_schema ) {
73+
my $diff = SQL::Translator::Diff->new(
74+
{
75+
output_db => 'MySQL',
76+
source_schema => $db_schema,
77+
target_schema => $sql_schema,
78+
}
79+
)->compute_differences->produce_diff_sql;
80+
81+
return $diff unless $self->{is_cli};
82+
print $diff . $self->get_warning;
83+
}
84+
}
85+
86+
=head3 get_warning
87+
88+
Retrieve a warning message.
89+
90+
=cut
91+
92+
sub get_warning {
93+
my ($self) = @_;
94+
95+
return
96+
"\n"
97+
. "WARNING!!!\n"
98+
. "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n"
99+
. "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n" . "\n";
100+
}
101+
102+
=head3 _get_db
103+
104+
Retrieves the database schema, removes auto-increment from the schema, and returns the modified schema.
105+
106+
=cut
107+
108+
sub _get_db {
109+
my ($self) = @_;
110+
111+
my $database_name = C4::Context->config("database");
112+
print "Parsing schema for database '$database_name'\n" if $self->{is_cli};
113+
my $dbh = C4::Context->dbh;
114+
my $parser = SQL::Translator->new(
115+
parser => 'DBI',
116+
parser_args => {
117+
dbh => $dbh,
118+
},
119+
);
120+
my $schema = $parser->translate();
121+
122+
#NOTE: Hack the schema to remove autoincrement
123+
#Otherwise, that difference will cause options for all tables to be reset unnecessarily
124+
my @tables = $schema->get_tables();
125+
foreach my $table (@tables) {
126+
my @new_options = ();
127+
my $replace_options = 0;
128+
my $options = $table->{options};
129+
foreach my $hashref (@$options) {
130+
if ( $hashref->{AUTO_INCREMENT} ) {
131+
$replace_options = 1;
132+
} else {
133+
push( @new_options, $hashref );
134+
}
135+
}
136+
if ($replace_options) {
137+
@{ $table->{options} } = @new_options;
138+
}
139+
}
140+
return $schema;
141+
}
142+
143+
=head3 _get_kohastructure
144+
145+
Retrieves and returns the schema using SQL::Translator for the given file
146+
147+
=cut
148+
149+
sub _get_kohastructure {
150+
my ($self) = @_;
151+
152+
print "Parsing schema for file $self->{filename} \n" if $self->{is_cli};
153+
my $translator = SQL::Translator->new();
154+
$translator->parser("MySQL");
155+
my $schema = $translator->translate( $self->{filename} );
156+
return $schema;
157+
}
158+
159+
1;

misc/maintenance/audit_database.pl

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,14 @@
11
#!/usr/bin/perl
22

33
use Modern::Perl;
4-
use SQL::Translator;
5-
use SQL::Translator::Diff;
64
use Getopt::Long;
75

8-
use C4::Context;
6+
use Koha::Database::Auditor;
97

10-
my $filename = "./installer/data/mysql/kohastructure.sql";
8+
my $filename;
119

1210
GetOptions(
1311
"filename=s" => \$filename,
1412
) or die("Error in command line arguments\n");
1513

16-
if ( !-f $filename ) {
17-
die("Filename '$filename' does not exist\n");
18-
}
19-
20-
my $sql_schema = get_kohastructure( { filename => $filename, } );
21-
my $db_schema = get_db();
22-
23-
if ( $sql_schema && $db_schema ) {
24-
my $diff = SQL::Translator::Diff->new(
25-
{
26-
output_db => 'MySQL',
27-
source_schema => $db_schema,
28-
target_schema => $sql_schema,
29-
}
30-
)->compute_differences->produce_diff_sql;
31-
32-
print $diff;
33-
print "\n";
34-
print "WARNING!!!\n";
35-
print "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n";
36-
print "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n";
37-
print "\n";
38-
}
39-
40-
sub get_db {
41-
my $database_name = C4::Context->config("database");
42-
print "Parsing schema for database '$database_name'\n";
43-
my $dbh = C4::Context->dbh;
44-
my $parser = SQL::Translator->new(
45-
parser => 'DBI',
46-
parser_args => {
47-
dbh => $dbh,
48-
},
49-
);
50-
my $schema = $parser->translate();
51-
52-
#NOTE: Hack the schema to remove autoincrement
53-
#Otherwise, that difference will cause options for all tables to be reset unnecessarily
54-
my @tables = $schema->get_tables();
55-
foreach my $table (@tables) {
56-
my @new_options = ();
57-
my $replace_options = 0;
58-
my $options = $table->{options};
59-
foreach my $hashref (@$options) {
60-
if ( $hashref->{AUTO_INCREMENT} ) {
61-
$replace_options = 1;
62-
} else {
63-
push( @new_options, $hashref );
64-
}
65-
}
66-
if ($replace_options) {
67-
@{ $table->{options} } = @new_options;
68-
}
69-
}
70-
return $schema;
71-
}
72-
73-
sub get_kohastructure {
74-
my ($args) = @_;
75-
my $filename = $args->{filename};
76-
print "Parsing schema for file '$filename'\n";
77-
my $translator = SQL::Translator->new();
78-
$translator->parser("MySQL");
79-
my $schema = $translator->translate($filename);
80-
return $schema;
81-
}
14+
Koha::Database::Auditor->new( { filename => $filename, is_cli => 1 } )->run;

0 commit comments

Comments
 (0)