641645894cbfc5b220047961c6160bba8c9ab5a6
[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 Getopt::Long qw( GetOptions );
24 use Pod::Usage qw( pod2usage );
25
26 use C4::Context;
27 use Koha::Database::Commenter;
28
29 sub alert_dry_run { print "-- DRY RUN\n" if $_[0]->{dry_run}; }
30
31 my $cmd_args = {};
32 GetOptions(
33   'clear'      => \$cmd_args->{clear},
34   'commit|c'   => \$cmd_args->{commit},
35   'database:s' => \$cmd_args->{database},
36   'help|h'     => \$cmd_args->{help},
37   'renumber'   => \$cmd_args->{renumber},
38   'reset'      => \$cmd_args->{reset},
39   'schema:s'   => \$cmd_args->{schema_file},
40   'table:s'    => \$cmd_args->{table},
41   'verbose|v'  => \$cmd_args->{verbose},
42 );
43 $cmd_args->{dry_run} = !$cmd_args->{commit};
44
45 my $commenter = Koha::Database::Commenter->new({
46     database => delete $cmd_args->{database}, dbh => C4::Context->dbh, schema_file => delete $cmd_args->{schema_file},
47 });
48 my $messages = $cmd_args->{verbose} || $cmd_args->{dry_run} ? [] : undef;
49 if( $cmd_args->{help} ) {
50     pod2usage( -verbose => 2 );
51 } elsif( ($cmd_args->{clear}||0) + ($cmd_args->{renumber}||0) + ($cmd_args->{reset}||0) > 1 ) {
52     print "You cannot pass the clear, renumber and reset flags together\n";
53 } elsif( delete $cmd_args->{clear} ) {
54     alert_dry_run( $cmd_args );
55     $commenter->clear( $cmd_args, $messages );
56 } elsif( delete $cmd_args->{reset} ) {
57     alert_dry_run( $cmd_args );
58     $commenter->reset_to_schema( $cmd_args, $messages );
59 } elsif( delete $cmd_args->{renumber} ) {
60     alert_dry_run( $cmd_args );
61     $commenter->renumber( $cmd_args, $messages );
62 } else {
63     pod2usage( -verbose => 1 );
64 }
65 print join("\n", @$messages), "\n" if $messages && @$messages;
66
67 __END__
68
69 =pod
70
71 =head1 NAME
72
73 misc/maintenance/sync_db_comments.pl
74
75 =head1 SYNOPSIS
76
77     perl sync_db_comments.pl [-h] [-v] [-schema FILE ] [-database DB_NAME] [-table TABLE_NAME] [-commit] [-clear|-reset|-renumber]
78
79 =head1 DESCRIPTION
80
81     Synchronize column comments in database with Koha schema. Allows you
82     to clear comments too. Operates additionally on specific tables only.
83     And provides a dry run mode that prints sql statements.
84
85     Warning: According to good practice, make a backup of your database
86     before running this script.
87
88     Some examples:
89
90     misc/maintenance/sync_db_comments.pl -help
91     Usage statement.
92
93     misc/maintenance/sync_db_comments.pl -clear -commit -verbose
94     Clear all column comments in database.
95     The verbose flag shows all issued ALTER TABLE statements.
96
97     misc/maintenance/sync_db_comments.pl -reset -commit -database mydb -table items -schema newstructure.sql
98     Only resets comments in items table.
99     Operates on specific database instead of the one from $KOHA_CONF.
100     Reads the schema from the specified file instead of default one.
101
102     misc/maintenance/sync_db_comments.pl -renumber
103     Renumbers all comments like Comment_1,2,..
104     Added for testing purposes. Not meant to run on production.
105     Omitting the commit flag allows you to see what would be done (dry run).
106
107 =head1 ADDITIONAL COMMENTS
108
109     This script may prove helpful to track synchronization issues between
110     Koha schema and actual database structure due to inconsistencies in
111     database revisions. It reduces the noise from missing column comments
112     when running script update_dbix_class_files.pl.
113
114     The script is just a wrapper around the module Koha::Database::Commenter.
115     A test script is provided in t/db_dependent/Koha/Database/Commenter.t.
116
117     The flags -clear, -reset and -renumber are mutually exclusive.
118
119     The renumber option has been helpful in verifying that the alter table
120     operations work on the complete Koha database. It is not recommended to run
121     it in production (obviously).
122
123 =head1 AUTHOR
124
125     Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
126
127 =cut