3 # Copyright (C) 2012 Tamil s.a.r.l.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 # find Koha's Perl modules
23 # test carefully before changing this
25 my $lib = "$FindBin::Bin/../kohalib.pl";
26 eval { require $lib };
36 my ($doit, $alterengine, $help);
37 my $result = GetOptions
(
39 'alterengine' => \
$alterengine,
45 pod2usage
( -verbose
=> 2 );
50 sub fix_mysql_constraints
{
53 # Get all current DB constraints
54 my $dbh = C4
::Context
->dbh;
55 $dbh->{RaiseError
} = 1;
56 $dbh->{ShowErrorStatement
} = 1;
57 my $database = C4
::Context
->config('database');
58 my %db_constraint = map { $_->[0] => undef } @
{$dbh->selectall_arrayref(
59 "SELECT CONSTRAINT_NAME
60 FROM information_schema.table_constraints
61 WHERE constraint_schema = '$database'
62 AND CONSTRAINT_TYPE != 'PRIMARY KEY' ")};
64 my $base_dir = C4
::Context
->config('intranetdir');
65 open my $fh, "<", "$base_dir/installer/data/mysql/kohastructure.sql"
66 or die "Unable to open kohastructure.sql file";
70 # FIXME: This hide problem. But if you run this script, it means that you
71 # have already identified issues with your Koha DB integrity, and will fix
72 # any necessary tables requiring records deleting.
73 $dbh->do("SET FOREIGN_KEY_CHECKS=0");
76 if ( $line =~ /CREATE TABLE (.*?) / ) {
78 $table_name =~ s/\`//g;
83 unless ( $line =~ /CONSTRAINT /i ) {
87 my $constraint = $line;
89 while ( $constraint !~ /,/ ) {
91 last CONTRAINT_LOOP
if $line =~ /ENGINE/i;
95 $constraint =~ s/^ *//;
96 $constraint =~ s/\n//g;
97 $constraint =~ s/ *$//;
98 $constraint =~ s/,$//;
99 my ($name) = $constraint =~ /CONSTRAINT (.*?) /;
101 unless ( exists($db_constraint{$name}) ) {
102 if ( $alterengine && !$engine_altered ) {
103 my $sql = "ALTER TABLE $table_name ENGINE = 'InnoDB'";
107 $dbh->do($sql) if $doit;
114 my $sql = "ALTER TABLE $table_name ADD $constraint";
118 $dbh->do($sql) if $doit;
124 $line = <$fh> if $line =~ /CONSTRAINT/i;
131 fix_mysql_constraints
($doit);
135 fix_mysql_constraints.pl
139 fix_mysql_constraints.pl --help
140 fix_mysql_constraints.pl
141 fix_mysql_constraints.pl --doit
147 Alter tables to add missing constraints. Prior to altering tables, it may be
148 necessary to alter tables storage engine from MyISAM to InnoDB.
158 Alter tables effectively, otherwise just display the ALTER TABLE directives.
160 =item B<--alterengine>
162 Prior to add missing constraints, alter table engine to InnoDB.