Koha/misc/maintenance/sync_db_comments.pl
Marcel de Rooy be83a52298
Bug 32334: Add sync_db_comments script
Test plan:
[1] Backup your database, if not done already.
[2] Check output of dry_run when clearing a table:
    misc/maintenance/sync_db_comments.pl -clear -table items -dry
[3] Save output of misc/devel/update_dbix_class_files before changing
    comments in order to compare later. (Commit your changes.)
    You may not have changes after running (at least on a fresh
    database). That's fine.
[4] Clear all comments:
    misc/maintenance/sync_db_comments.pl -clear
[5] Renumber all comments:
    misc/maintenance/sync_db_comments.pl -renum
[6] Reset all comments to schema. Make sure that script finds your
    structure in installer/data/mysql folder.
    misc/maintenance/sync_db_comments.pl -reset
[7] Run update_dbix_class_files again and inspect changes as compared
    to previous run.
    Can you explain them? You should only see changes related to
    column comments. If you did not have changes in step 3, you
    should not have them here too.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-04-12 11:21:57 -03:00

120 lines
4 KiB
Perl
Executable file

#!/usr/bin/perl
# This script helps you synchronize database comments between DB and schema.
# This file is part of Koha.
#
# Copyright 2022 Rijksmuseum, Koha development team
#
# 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 Data::Dumper qw(Dumper);
use Getopt::Long qw( GetOptions );
use Pod::Usage qw( pod2usage );
use C4::Context;
use Koha::Database::Commenter;
my $cmd_args = {};
GetOptions(
'clear' => \$cmd_args->{clear},
'database:s' => \$cmd_args->{database},
'dry_run' => \$cmd_args->{dry_run},
'help|h' => \$cmd_args->{help},
'renumber' => \$cmd_args->{renumber},
'reset' => \$cmd_args->{reset},
'table:s' => \$cmd_args->{table},
'verbose|v' => \$cmd_args->{verbose},
);
my $commenter = Koha::Database::Commenter->new({ database => delete $cmd_args->{database}, dbh => C4::Context->dbh });
if( $cmd_args->{help} ) {
pod2usage( -verbose => 2 );
} elsif( delete $cmd_args->{clear} ) { # clear overrules reset
alert_dry_run( $cmd_args->{dry_run} );
$commenter->clear( $cmd_args );
} elsif( delete $cmd_args->{reset} ) { # reset overrules renumber
alert_dry_run( $cmd_args->{dry_run} );
$commenter->reset_to_schema( $cmd_args );
} elsif( delete $cmd_args->{renumber} ) {
alert_dry_run( $cmd_args->{dry_run} );
$commenter->renumber( $cmd_args );
} else {
pod2usage( -verbose => 1 );
}
sub alert_dry_run { print "-- DRY RUN\n" if $_[0]; }
__END__
=pod
=head1 NAME
misc/maintenance/sync_db_comments.pl
=head1 SYNOPSIS
perl sync_db_comments.pl [-h] [-v] [-database DB_NAME] [-table TABLE_NAME] [-dry_run] [-clear|-reset|-renumber]
=head1 DESCRIPTION
Synchronize column comments in database with Koha schema. Allows you
to clear comments too. Operates additionally on specific tables only.
And provides a dry run mode that prints sql statements.
Warning: According to good practice, make a backup of your database
before running this script.
Some examples:
misc/maintance/sync_db_comments.pl -help
Usage statement.
misc/maintance/sync_db_comments.pl -clear -verbose
Clear all column comments in database.
The verbose flag shows all issued ALTER TABLE statements.
misc/maintance/sync_db_comments.pl -reset -database mydb -table items
Only resets comments in items table.
Operates on specific database instead of the one from $KOHA_CONF.
misc/maintance/sync_db_comments.pl -renumber -dry_run
Renumbers all comments like Comment_1,2,..
Added for testing purposes. Not meant to run on production.
The dry_run flag allows you to see what would be done.
=head1 ADDITIONAL COMMENTS
This script may prove helpful to track synchronization issues between
Koha schema and actual database structure due to inconsistencies in
database revisions. It reduces the noise from missing column comments
when running script update_dbix_class_files.pl.
The script is just a wrapper around the module Koha::Database::Commenter.
A test script is provided in t/db_dependent/Koha/Database/Commenter.t.
The flags -clear, -reset and -renumber are mutually exclusive. Clear ignores
the other two, reset ignores renumber.
The renumber option has been helpful in verifying that the alter table
operations work on the complete Koha database. It is not recommended to run
it in production (obviously).
=head1 AUTHOR
Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
=cut