Bugzilla – Attachment 174403 Details for
Bug 36039
The output of audit_database.pl should be accessible through the UI
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36039: Move audit_database script logic to a dedicated module
Bug-36039-Move-auditdatabase-script-logic-to-a-ded.patch (text/plain), 8.43 KB, created by
Pedro Amorim
on 2024-11-12 11:56:24 UTC
(
hide
)
Description:
Bug 36039: Move audit_database script logic to a dedicated module
Filename:
MIME Type:
Creator:
Pedro Amorim
Created:
2024-11-12 11:56:24 UTC
Size:
8.43 KB
patch
obsolete
>From c761514307425673de42282873f9034dea6a0606 Mon Sep 17 00:00:00 2001 >From: Pedro Amorim <pedro.amorim@ptfs-europe.com> >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: ><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> >--- > 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 0000000000..520387bccc >--- /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 <http://www.gnu.org/licenses>. >+ >+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 ac287c2571..cd624305c3 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.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36039
:
161892
|
162462
|
162463
|
166097
|
166098
|
166099
|
166155
|
166156
|
166157
|
169225
|
169226
|
169227
| 174403 |
174404
|
174405