From 7fbfa468c74665a2af5d18ff517948845f11483f Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 26 Feb 2024 16:37:11 +0000 Subject: [PATCH] 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: /cgi-bin/koha/about.pl?tab=database 2. Ensure the output is the same as test plan #1 Signed-off-by: Martin Renvoize Signed-off-by: JesseM Signed-off-by: Kyle M Hall --- Koha/Database/Auditor.pm | 159 +++++++++++++++++++++++++++++ misc/maintenance/audit_database.pl | 73 +------------ 2 files changed, 162 insertions(+), 70 deletions(-) create mode 100644 Koha/Database/Auditor.pm diff --git a/Koha/Database/Auditor.pm b/Koha/Database/Auditor.pm new file mode 100644 index 00000000000..520387bccce --- /dev/null +++ b/Koha/Database/Auditor.pm @@ -0,0 +1,159 @@ +package Koha::Database::Auditor; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use SQL::Translator; +use SQL::Translator::Diff; + +use C4::Context; + +=head1 NAME + +Koha::Database::Auditor + +=head1 SYNOPSIS + +use Koha::Database::Auditor; + +=head1 API + +=head2 Methods + +=head3 new + +Constructor for creating a new object with specified parameters + +=cut + +sub new { + my ( $class, $params ) = @_; + my $self = bless $params // {}, $class; + $self->{filename} = $params->{filename} // './installer/data/mysql/kohastructure.sql'; + $self->{is_cli} = $params->{is_cli}; + return $self; +} + +=head3 run + +Run the database audit with the given arguments, including the filename + and whether it is a command-line interface(CLI). +Returns $diff only if $is_cli is false. Else it just prints $diff. + +=cut + +sub run { + my ( $self, $args ) = @_; + + if ( !-f $self->{filename} ) { + unless ( $self->{is_cli} ) { + return 'Database schema file not found'; + } + die("Filename '$self->{filename}' does not exist\n"); + } + + my $sql_schema = $self->_get_kohastructure(); + my $db_schema = $self->_get_db(); + + if ( $sql_schema && $db_schema ) { + my $diff = SQL::Translator::Diff->new( + { + output_db => 'MySQL', + source_schema => $db_schema, + target_schema => $sql_schema, + } + )->compute_differences->produce_diff_sql; + + return $diff unless $self->{is_cli}; + print $diff . $self->get_warning; + } +} + +=head3 get_warning + +Retrieve a warning message. + +=cut + +sub get_warning { + my ($self) = @_; + + return + "\n" + . "WARNING!!!\n" + . "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n" + . "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n" . "\n"; +} + +=head3 _get_db + +Retrieves the database schema, removes auto-increment from the schema, and returns the modified schema. + +=cut + +sub _get_db { + my ($self) = @_; + + my $database_name = C4::Context->config("database"); + print "Parsing schema for database '$database_name'\n" if $self->{is_cli}; + my $dbh = C4::Context->dbh; + my $parser = SQL::Translator->new( + parser => 'DBI', + parser_args => { + dbh => $dbh, + }, + ); + my $schema = $parser->translate(); + + #NOTE: Hack the schema to remove autoincrement + #Otherwise, that difference will cause options for all tables to be reset unnecessarily + my @tables = $schema->get_tables(); + foreach my $table (@tables) { + my @new_options = (); + my $replace_options = 0; + my $options = $table->{options}; + foreach my $hashref (@$options) { + if ( $hashref->{AUTO_INCREMENT} ) { + $replace_options = 1; + } else { + push( @new_options, $hashref ); + } + } + if ($replace_options) { + @{ $table->{options} } = @new_options; + } + } + return $schema; +} + +=head3 _get_kohastructure + +Retrieves and returns the schema using SQL::Translator for the given file + +=cut + +sub _get_kohastructure { + my ($self) = @_; + + print "Parsing schema for file $self->{filename} \n" if $self->{is_cli}; + my $translator = SQL::Translator->new(); + $translator->parser("MySQL"); + my $schema = $translator->translate( $self->{filename} ); + return $schema; +} + +1; diff --git a/misc/maintenance/audit_database.pl b/misc/maintenance/audit_database.pl index ac287c25717..cd624305c3b 100755 --- a/misc/maintenance/audit_database.pl +++ b/misc/maintenance/audit_database.pl @@ -1,81 +1,14 @@ #!/usr/bin/perl use Modern::Perl; -use SQL::Translator; -use SQL::Translator::Diff; use Getopt::Long; -use C4::Context; +use Koha::Database::Auditor; -my $filename = "./installer/data/mysql/kohastructure.sql"; +my $filename; GetOptions( "filename=s" => \$filename, ) or die("Error in command line arguments\n"); -if ( !-f $filename ) { - die("Filename '$filename' does not exist\n"); -} - -my $sql_schema = get_kohastructure( { filename => $filename, } ); -my $db_schema = get_db(); - -if ( $sql_schema && $db_schema ) { - my $diff = SQL::Translator::Diff->new( - { - output_db => 'MySQL', - source_schema => $db_schema, - target_schema => $sql_schema, - } - )->compute_differences->produce_diff_sql; - - print $diff; - print "\n"; - print "WARNING!!!\n"; - print "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n"; - print "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n"; - print "\n"; -} - -sub get_db { - my $database_name = C4::Context->config("database"); - print "Parsing schema for database '$database_name'\n"; - my $dbh = C4::Context->dbh; - my $parser = SQL::Translator->new( - parser => 'DBI', - parser_args => { - dbh => $dbh, - }, - ); - my $schema = $parser->translate(); - - #NOTE: Hack the schema to remove autoincrement - #Otherwise, that difference will cause options for all tables to be reset unnecessarily - my @tables = $schema->get_tables(); - foreach my $table (@tables) { - my @new_options = (); - my $replace_options = 0; - my $options = $table->{options}; - foreach my $hashref (@$options) { - if ( $hashref->{AUTO_INCREMENT} ) { - $replace_options = 1; - } else { - push( @new_options, $hashref ); - } - } - if ($replace_options) { - @{ $table->{options} } = @new_options; - } - } - return $schema; -} - -sub get_kohastructure { - my ($args) = @_; - my $filename = $args->{filename}; - print "Parsing schema for file '$filename'\n"; - my $translator = SQL::Translator->new(); - $translator->parser("MySQL"); - my $schema = $translator->translate($filename); - return $schema; -} +Koha::Database::Auditor->new( { filename => $filename, is_cli => 1 } )->run; -- 2.30.2