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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 # find Koha's Perl modules
23 # test carefully before changing this
25 my $lib = "$FindBin::Bin/../kohalib.pl";
26 eval { require $lib };
37 my ($doit, $alterengine, $help);
38 my $result = GetOptions
(
40 'alterengine' => \
$alterengine,
46 pod2usage
( -verbose
=> 2 );
51 sub fix_mysql_constraints
{
54 # Get all current DB constraints
55 my $dbh = C4
::Context
->dbh;
56 $dbh->{RaiseError
} = 1;
57 $dbh->{ShowErrorStatement
} = 1;
58 my $database = C4
::Context
->config('database');
59 my %db_constraint = map { $_->[0] => undef } @
{$dbh->selectall_arrayref(
60 "SELECT CONSTRAINT_NAME
61 FROM information_schema.table_constraints
62 WHERE constraint_schema = '$database'
63 AND CONSTRAINT_TYPE != 'PRIMARY KEY' ")};
65 my $base_dir = C4
::Context
->config('intranetdir');
66 open my $fh, "<", "$base_dir/installer/data/mysql/kohastructure.sql"
67 or die "Unable to open kohastructure.sql file";
71 # FIXME: This hide problem. But if you run this script, it means that you
72 # have already identified issues with your Koha DB integrity, and will fix
73 # any necessary tables requiring records deleting.
74 $dbh->do("SET FOREIGN_KEY_CHECKS=0");
77 if ( $line =~ /CREATE TABLE (.*?) / ) {
79 $table_name =~ s/\`//g;
84 unless ( $line =~ /CONSTRAINT /i ) {
88 my $constraint = $line;
90 while ( $constraint !~ /,/ ) {
92 last CONTRAINT_LOOP
if $line =~ /ENGINE/i;
96 $constraint =~ s/^ *//;
97 $constraint =~ s/\n//g;
98 $constraint =~ s/ *$//;
99 $constraint =~ s/,$//;
100 my ($name) = $constraint =~ /CONSTRAINT (.*?) /;
102 unless ( exists($db_constraint{$name}) ) {
103 if ( $alterengine && !$engine_altered ) {
104 my $sql = "ALTER TABLE $table_name ENGINE = 'InnoDB'";
108 $dbh->do($sql) if $doit;
115 my $sql = "ALTER TABLE $table_name ADD $constraint";
119 $dbh->do($sql) if $doit;
125 $line = <$fh> if $line =~ /CONSTRAINT/i;
132 fix_mysql_constraints
($doit);
136 fix_mysql_constraints.pl
140 fix_mysql_constraints.pl --help
141 fix_mysql_constraints.pl
142 fix_mysql_constraints.pl --doit
148 Alter tables to add missing constraints. Prior to altering tables, it may be
149 necessary to alter tables storage engine from MyISAM to InnoDB.
159 Alter tables effectively, otherwise just display the ALTER TABLE directives.
161 =item B<--alterengine>
163 Prior to add missing constraints, alter table engine to InnoDB.