3 # This script helps you synchronize database comments between DB and schema.
5 # This file is part of Koha.
7 # Copyright 2022 Rijksmuseum, Koha development team
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.
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.
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>.
23 use Getopt::Long qw( GetOptions );
24 use Pod::Usage qw( pod2usage );
27 use Koha::Database::Commenter;
29 sub alert_dry_run { print "-- DRY RUN\n" if $_[0]->{dry_run}; }
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},
43 $cmd_args->{dry_run} = !$cmd_args->{commit};
45 my $commenter = Koha::Database::Commenter->new({
46 database => delete $cmd_args->{database}, dbh => C4::Context->dbh, schema_file => delete $cmd_args->{schema_file},
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 );
63 pod2usage( -verbose => 1 );
65 print join("\n", @$messages), "\n" if $messages && @$messages;
73 misc/maintenance/sync_db_comments.pl
77 perl sync_db_comments.pl [-h] [-v] [-schema FILE ] [-database DB_NAME] [-table TABLE_NAME] [-commit] [-clear|-reset|-renumber]
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.
85 Warning: According to good practice, make a backup of your database
86 before running this script.
90 misc/maintenance/sync_db_comments.pl -help
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.
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.
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).
107 =head1 ADDITIONAL COMMENTS
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.
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.
117 The flags -clear, -reset and -renumber are mutually exclusive.
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).
125 Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands