Bug 32334: Add sync_db_comments script
[koha.git] / misc / maintenance / sync_db_comments.pl
1 #!/usr/bin/perl
2
3 # This script helps you synchronize database comments between DB and schema.
4
5 # This file is part of Koha.
6 #
7 # Copyright 2022 Rijksmuseum, Koha development team
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use Data::Dumper qw(Dumper);
24 use Getopt::Long qw( GetOptions );
25 use Pod::Usage qw( pod2usage );
26
27 use C4::Context;
28 use Koha::Database::Commenter;
29
30 my $cmd_args = {};
31 GetOptions(
32   'clear'      => \$cmd_args->{clear},
33   'database:s' => \$cmd_args->{database},
34   'dry_run'    => \$cmd_args->{dry_run},
35   'help|h'     => \$cmd_args->{help},
36   'renumber'   => \$cmd_args->{renumber},
37   'reset'      => \$cmd_args->{reset},
38   'table:s'    => \$cmd_args->{table},
39   'verbose|v'  => \$cmd_args->{verbose},
40 );
41
42 my $commenter = Koha::Database::Commenter->new({ database => delete $cmd_args->{database}, dbh => C4::Context->dbh });
43 if( $cmd_args->{help} ) {
44     pod2usage( -verbose => 2 );
45 } elsif( delete $cmd_args->{clear} ) { # clear overrules reset
46     alert_dry_run( $cmd_args->{dry_run} );
47     $commenter->clear( $cmd_args );
48 } elsif( delete $cmd_args->{reset} ) { # reset overrules renumber
49     alert_dry_run( $cmd_args->{dry_run} );
50     $commenter->reset_to_schema( $cmd_args );
51 } elsif( delete $cmd_args->{renumber} ) {
52     alert_dry_run( $cmd_args->{dry_run} );
53     $commenter->renumber( $cmd_args );
54 } else {
55     pod2usage( -verbose => 1 );
56 }
57
58 sub alert_dry_run { print "-- DRY RUN\n" if $_[0]; }
59
60 __END__
61
62 =pod
63
64 =head1 NAME
65
66 misc/maintenance/sync_db_comments.pl
67
68 =head1 SYNOPSIS
69
70     perl sync_db_comments.pl [-h] [-v] [-database DB_NAME] [-table TABLE_NAME] [-dry_run] [-clear|-reset|-renumber]
71
72 =head1 DESCRIPTION
73
74     Synchronize column comments in database with Koha schema. Allows you
75     to clear comments too. Operates additionally on specific tables only.
76     And provides a dry run mode that prints sql statements.
77
78     Warning: According to good practice, make a backup of your database
79     before running this script.
80
81     Some examples:
82
83     misc/maintance/sync_db_comments.pl -help
84     Usage statement.
85
86     misc/maintance/sync_db_comments.pl -clear -verbose
87     Clear all column comments in database.
88     The verbose flag shows all issued ALTER TABLE statements.
89
90     misc/maintance/sync_db_comments.pl -reset -database mydb -table items
91     Only resets comments in items table.
92     Operates on specific database instead of the one from $KOHA_CONF.
93
94     misc/maintance/sync_db_comments.pl -renumber -dry_run
95     Renumbers all comments like Comment_1,2,..
96     Added for testing purposes. Not meant to run on production.
97     The dry_run flag allows you to see what would be done.
98
99 =head1 ADDITIONAL COMMENTS
100
101     This script may prove helpful to track synchronization issues between
102     Koha schema and actual database structure due to inconsistencies in
103     database revisions. It reduces the noise from missing column comments
104     when running script update_dbix_class_files.pl.
105
106     The script is just a wrapper around the module Koha::Database::Commenter.
107     A test script is provided in t/db_dependent/Koha/Database/Commenter.t.
108
109     The flags -clear, -reset and -renumber are mutually exclusive. Clear ignores
110     the other two, reset ignores renumber.
111
112     The renumber option has been helpful in verifying that the alter table
113     operations work on the complete Koha database. It is not recommended to run
114     it in production (obviously).
115
116 =head1 AUTHOR
117
118     Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
119
120 =cut