From 5920ca6fa02d65e2a2b9076d08444a4cd6a157a9 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Thu, 12 Dec 2013 23:55:14 +0000 Subject: [PATCH] Bug 11389: reenable Pg as a DB scheme that Koha can connect to 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 Signed-off-by: Chris Cormack Signed-off-by: Katrin Fischer 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 Pg koha localhost 5432 koha xxxx - 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 --- C4/Context.pm | 33 ++++++++++++++------------------- Koha/Database.pm | 8 +------- etc/koha-conf.xml | 1 + t/Context.t | 7 ++++++- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index cee4a0f3ec..3914420ad8 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -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"); diff --git a/Koha/Database.pm b/Koha/Database.pm index 4c6cae6f4e..12758bf8fa 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -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"); diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml index d42f041f4f..1da53780c1 100644 --- a/etc/koha-conf.xml +++ b/etc/koha-conf.xml @@ -264,6 +264,7 @@ __PAZPAR2_TOGGLE_XML_POST__ + __DB_TYPE__ diff --git a/t/Context.t b/t/Context.t index 9c561cb215..446e2544f1 100755 --- a/t/Context.t +++ b/t/Context.t @@ -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'); -- 2.39.5