This patch adds a function TableExists which checks for the existence
of a given table and returns 1 or 0 accordingly. This allows us to
check for the existence of a given table when doing operations on
tables which may not exist in a given database which, in turn, will
reduce the number of red errors which show up after an upgrade.

An example of its use is included in this patch.
---
 installer/data/mysql/updatedatabase.pl |   45 ++++++++++++++++++++++----------
 1 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/installer/data/mysql/updatedatabase.pl 
b/installer/data/mysql/updatedatabase.pl
index 503c8ee..0b1c16e 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -4893,19 +4893,21 @@ if ( C4::Context->preference("Version") < 
TransformToNum($DBversion) ) {
 
 $DBversion = "3.07.00.025";
 if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
-    $dbh->do( q|DROP TABLE bibliocoverimage;| );
-    $dbh->do(
-        q|CREATE TABLE biblioimages (
-          imagenumber int(11) NOT NULL AUTO_INCREMENT,
-          biblionumber int(11) NOT NULL,
-          mimetype varchar(15) NOT NULL,
-          imagefile mediumblob NOT NULL,
-          thumbnail mediumblob NOT NULL,
-          PRIMARY KEY (imagenumber),
-          CONSTRAINT bibliocoverimage_fk1 FOREIGN KEY (biblionumber) 
REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE
-          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;|
-    );
-    print "Upgrade to $DBversion done (Correct table name for local cover 
images [please disregard the following error messages: \"Unknown table 
'bibliocoverimage'...\" and \"Table 'biblioimages' already exists...\"])\n";
+    if (TableExists('bibliocoverimage')) {
+        $dbh->do( q|DROP TABLE bibliocoverimage;| );
+        $dbh->do(
+            q|CREATE TABLE biblioimages (
+              imagenumber int(11) NOT NULL AUTO_INCREMENT,
+              biblionumber int(11) NOT NULL,
+              mimetype varchar(15) NOT NULL,
+              imagefile mediumblob NOT NULL,
+              thumbnail mediumblob NOT NULL,
+              PRIMARY KEY (imagenumber),
+              CONSTRAINT bibliocoverimage_fk1 FOREIGN KEY (biblionumber) 
REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE
+              ) ENGINE=InnoDB DEFAULT CHARSET=utf8;|
+        );
+    }
+    print "Upgrade to $DBversion done (Correct table name for local cover 
images if needed. )\n";
     SetVersion($DBversion);
 }
 
@@ -5135,7 +5137,7 @@ if ( C4::Context->preference("Version") < 
TransformToNum($DBversion) ) {
         $dbh->do("UPDATE systempreferences SET type='Free', 
value=\"$valueOPACXSLTDetailsDisplay\" WHERE 
variable='OPACXSLTDetailsDisplay'");
         $dbh->do("UPDATE systempreferences SET type='Free', 
value=\"$valueOPACXSLTResultsDisplay\" WHERE 
variable='OPACXSLTResultsDisplay'");
     }
-    print "XSLT systempreference takes a path to file rather than YesNo\n";
+    print "Upgrade to $DBversion done (XSLT systempreference takes a path to 
file rather than YesNo)\n";
     SetVersion($DBversion);
 }
 
@@ -5148,6 +5150,21 @@ if ( C4::Context->preference("Version") < 
TransformToNum($DBversion) ) {
 
 =head1 FUNCTIONS
 
+=head2 TableExists($table)
+
+=cut
+
+sub TableExists {
+    my $table = shift;
+    eval {
+                local $dbh->{PrintError} = 0;
+                local $dbh->{RaiseError} = 1;
+                $dbh->do(qq{SELECT * FROM $table WHERE 1 = 0 });
+            };
+    return 1 unless $@;
+    return 0;
+}
+
 =head2 DropAllForeignKeys($table)
 
 Drop all foreign keys of the table $table
-- 
1.7.0.4

_______________________________________________
Koha-patches mailing list
[email protected]
http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-patches
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to