Bug 22472: Make column_exists early return if the table does not exist
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 7 Mar 2019 12:14:25 +0000 (09:14 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 11 Apr 2019 13:34:10 +0000 (13:34 +0000)
On the way we move TableExists to C4::Installer, where it belongs to.

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/Installer.pm
installer/data/mysql/updatedatabase.pl

index a1c4714..4d57715 100644 (file)
@@ -30,7 +30,7 @@ use vars qw(@ISA @EXPORT);
 BEGIN {
     require Exporter;
     @ISA = qw( Exporter );
-    push @EXPORT, qw( foreign_key_exists index_exists column_exists );
+    push @EXPORT, qw( foreign_key_exists index_exists column_exists TableExists);
 };
 
 =head1 NAME
@@ -527,6 +527,7 @@ sub index_exists {
 
 sub column_exists {
     my ( $table_name, $column_name ) = @_;
+    return unless TableExists($table_name);
     my $dbh = C4::Context->dbh;
     my ($exists) = $dbh->selectrow_array(
         qq|
@@ -537,6 +538,19 @@ sub column_exists {
     return $exists;
 }
 
+sub TableExists { # Could be renamed table_exists for consistency
+    my $table = shift;
+    eval {
+                my $dbh = C4::Context->dbh;
+                local $dbh->{PrintError} = 0;
+                local $dbh->{RaiseError} = 1;
+                $dbh->do(qq{SELECT * FROM $table WHERE 1 = 0 });
+            };
+    return 1 unless $@;
+    return 0;
+}
+
+
 =head1 AUTHOR
 
 C4::Installer is a refactoring of logic originally from installer/installer.pl, which was
index eec844b..06f3337 100755 (executable)
@@ -17967,21 +17967,6 @@ foreach my $file ( sort readdir $dirh ) {
 
 =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