Bug 21797: Update two-column templates with Bootstrap grid: Acquisitions part 5
[koha.git] / misc / maintenance / fix_mysql_constraints.pl
blob01472242d8f1307b04787b3951cf40a969bb2a59
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 BEGIN {
22 # find Koha's Perl modules
23 # test carefully before changing this
24 use FindBin;
25 my $lib = "$FindBin::Bin/../kohalib.pl";
26 eval { require $lib };
29 use Getopt::Long;
30 use Pod::Usage;
31 use YAML;
32 use Try::Tiny;
33 use C4::Context;
36 my ($doit, $alterengine, $help);
37 my $result = GetOptions(
38 'doit' => \$doit,
39 'alterengine' => \$alterengine,
40 'help|h' => \$help,
44 sub usage {
45 pod2usage( -verbose => 2 );
46 exit;
50 sub fix_mysql_constraints {
51 my ($doit) = @_;
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";
68 my $table_name;
69 my $engine_altered;
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");
74 my $line = <$fh>;
75 while ( $line ) {
76 if ( $line =~ /CREATE TABLE (.*?) / ) {
77 $table_name = $1;
78 $table_name =~ s/\`//g;
79 $engine_altered = 0;
80 $line = <$fh>;
81 next;
83 unless ( $line =~ /CONSTRAINT /i ) {
84 $line = <$fh>;
85 next;
87 my $constraint = $line;
88 CONTRAINT_LOOP:
89 while ( $constraint !~ /,/ ) {
90 $line = <$fh>;
91 last CONTRAINT_LOOP if $line =~ /ENGINE/i;
92 $line =~ s/^ */ /;
93 $constraint .= $line;
95 $constraint =~ s/^ *//;
96 $constraint =~ s/\n//g;
97 $constraint =~ s/ *$//;
98 $constraint =~ s/,$//;
99 my ($name) = $constraint =~ /CONSTRAINT (.*?) /;
100 $name =~ s/\`//g;
101 unless ( exists($db_constraint{$name}) ) {
102 if ( $alterengine && !$engine_altered ) {
103 my $sql = "ALTER TABLE $table_name ENGINE = 'InnoDB'";
104 say $sql;
105 if ( $doit ) {
106 try {
107 $dbh->do($sql) if $doit;
108 $engine_altered = 1;
109 } catch {
110 say "Error: $_;";
114 my $sql = "ALTER TABLE $table_name ADD $constraint";
115 say $sql;
116 if ( $doit ) {
117 try {
118 $dbh->do($sql) if $doit;
119 } catch {
120 say "Error: $_";
124 $line = <$fh> if $line =~ /CONSTRAINT/i;
129 usage() if $help;
131 fix_mysql_constraints($doit);
133 =head1 NAME
135 fix_mysql_constraints.pl
137 =head1 SYNOPSIS
139 fix_mysql_constraints.pl --help
140 fix_mysql_constraints.pl
141 fix_mysql_constraints.pl --doit
143 =head1 DESCRIPTION
145 See bug #8915
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.
150 =over 8
152 =item B<--help>
154 Prints this help
156 =item B<--doit>
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.
164 =back
166 =cut