Bug 11389: reenable Pg as a DB scheme that Koha can connect to
authorGalen Charlton <gmc@esilibrary.com>
Thu, 12 Dec 2013 23:55:14 +0000 (23:55 +0000)
committerGalen Charlton <gmc@esilibrary.com>
Mon, 13 Jan 2014 20:56:14 +0000 (20:56 +0000)
This patch restores the ability to request a DBI database handle
or a DBIx::Class schema object connected to a PostgreSQL database.

To address the concerns raised in bug 7188, only "mysql" and "Pg"
are recognized as valid DB schemes.  If anything else is passed
to C4::Context::db_scheme2dbi or set as the db_scheme in the Koha
configuration file, the DBD driver to load is assumed to be "mysql".

Note that this patch drops any pretense of Oracle support.

To test:

[1] Apply patch, and verify that the database-dependent tests
    pass when run against a MySQL Koha database.
[2] To test against PostgreSQL, create a Pg database and
    edit koha-conf.xml to set db_scheme to Pg (and adjust
    the other DB connection parameters appropriately).  The
    following tests should pass, at minimum:

    t/Context.t
    t/db_dependent/Koha_Database.t

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Works as described, some additional notes:

- Installed Postgres following
  http://wiki.ubuntuusers.de/PostgreSQL
- Created a database user koha
- Created a database koha
- Changed the koha-conf.xml file
    <db_scheme>Pg</db_scheme>
    <database>koha</database>
    <hostname>localhost</hostname>
    <port>5432</port>
    <user>koha</user>
    <pass>xxxx</pass>
- Installed libdbd-pg-perl
- Ran the web installer until step 3 everything looked ok
  Step 3 complains:
    Password for user koha: psql: fe_sendauth: no password supplied
- Both t/Context.t and t/db_dependent/Koha_Database.t pass

Signed-off-by: Galen Charlton <gmc@esilibrary.com>

C4/Context.pm
Koha/Database.pm
etc/koha-conf.xml
t/Context.t

index cee4a0f..3914420 100644 (file)
@@ -293,20 +293,20 @@ sub memcached {
     }
 }
 
-# db_scheme2dbi
-# Translates the full text name of a database into de appropiate dbi name
-# 
+=head2 db_schema2dbi
+
+    my $dbd_driver_name = C4::Context::db_schema2dbi($scheme);
+
+This routines translates a database type to part of the name
+of the appropriate DBD driver to use when establishing a new
+database connection.  It recognizes 'mysql' and 'Pg'; if any
+other scheme is supplied it defaults to 'mysql'.
+
+=cut
+
 sub db_scheme2dbi {
-    my $name = shift;
-    # for instance, we support only mysql, so don't care checking
-    return "mysql";
-    for ($name) {
-# FIXME - Should have other databases. 
-        if (/mysql/) { return("mysql"); }
-        if (/Postgres|Pg|PostgresSQL/) { return("Pg"); }
-        if (/oracle/) { return("Oracle"); }
-    }
-    return;         # Just in case
+    my $scheme = shift // '';
+    return $scheme eq 'Pg' ? $scheme : 'mysql';
 }
 
 sub import {
@@ -794,12 +794,7 @@ sub _new_dbh
 
     ## $context
     ## correct name for db_schme        
-    my $db_driver;
-    if ($context->config("db_scheme")){
-        $db_driver=db_scheme2dbi($context->config("db_scheme"));
-    }else{
-        $db_driver="mysql";
-    }
+    my $db_driver = db_scheme2dbi($context->config("db_scheme"));
 
     my $db_name   = $context->config("database");
     my $db_host   = $context->config("hostname");
index 4c6cae6..12758bf 100644 (file)
@@ -45,14 +45,8 @@ __PACKAGE__->mk_accessors(qw( ));
 # database connection from the data given in the current context, and
 # returns it.
 sub _new_schema {
-    my $db_driver;
     my $context = C4::Context->new();
-    if ( $context->config("db_scheme") ) {
-        $db_driver = $context->db_scheme2dbi( $context->config("db_scheme") );
-    }
-    else {
-        $db_driver = "mysql";
-    }
+    my $db_driver = C4::Context::db_scheme2dbi($context->config("db_scheme"));
 
     my $db_name   = $context->config("database");
     my $db_host   = $context->config("hostname");
index d42f041..1da5378 100644 (file)
@@ -264,6 +264,7 @@ __PAZPAR2_TOGGLE_XML_POST__
 
 <!-- ADDITIONAL KOHA CONFIGURATION DIRECTIVE -->
 <!-- db_scheme should follow the DBD driver name --> 
+<!-- the DBD drivers supported by Koha are mysql and Pg -->
 <!-- port info: mysql:3306 Pg:5432 (5433 on Debian) -->
 <config>
  <db_scheme>__DB_TYPE__</db_scheme>
index 9c561cb..446e254 100755 (executable)
@@ -2,7 +2,7 @@
 
 use Modern::Perl;
 use DBI;
-use Test::More tests => 6;
+use Test::More tests => 10;
 use Test::MockModule;
 
 BEGIN {
@@ -30,3 +30,8 @@ $userenv->{flags} = 421;
 $is_super_librarian = eval{ C4::Context::IsSuperLibrarian() };
 is ( $@, q||, "IsSuperLibrarian does not log an error if userenv is defined" );
 is ( $is_super_librarian, 1, "With flag=1, it is a super librarian" );
+
+is(C4::Context::db_scheme2dbi('mysql'), 'mysql', 'ask for mysql, get mysql');
+is(C4::Context::db_scheme2dbi('Pg'),    'Pg',    'ask for Pg, get Pg');
+is(C4::Context::db_scheme2dbi('xxx'),   'mysql', 'ask for unsupported DBMS, get mysql');
+is(C4::Context::db_scheme2dbi(),        'mysql', 'ask for nothing, get mysql');