Bug 11891 - Make Koha::Schema use C4::Context->dbh
authorKyle M Hall <kyle@bywatersolutions.com>
Tue, 4 Mar 2014 18:26:03 +0000 (13:26 -0500)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Thu, 19 Jun 2014 14:54:54 +0000 (11:54 -0300)
Right now there is no connection between the database handles used by
C4::Context::dbh and Koha::Datbase/Schema. This makes it impossible to
use transactions in unit tests to temporarily modify the database to
test subroutines that take advantage of Koha::Database. This patch fixes
that issue.

Test Plan:
1) Apply this patch
2) prove -v t/db_dependent/ILSDI_Services.t and
   prove -v t/db_dependent/Items.t and
   prove -v t/db_dependent/Circulation_issue.t should
   all start passing

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Koha/Database.pm
Koha/Schema.pm
Koha/Storage.pm [new file with mode: 0644]

index b259ca0..99888c6 100644 (file)
@@ -45,21 +45,7 @@ __PACKAGE__->mk_accessors(qw( ));
 # database connection from the data given in the current context, and
 # returns it.
 sub _new_schema {
-    my $context = C4::Context->new();
-    my $db_driver = C4::Context::db_scheme2dbi($context->config("db_scheme"));
-
-    my $db_name   = $context->config("database");
-    my $db_host   = $context->config("hostname");
-    my $db_port   = $context->config("port") || '';
-    my $db_user   = $context->config("user");
-    my $db_passwd = $context->config("pass");
-
-    my $db_opts = ($db_driver eq 'mysql') ? { mysql_enable_utf8 => 1 } :
-                  ($db_driver eq 'Pg')    ? { pg_enable_utf8    => 1 } :
-                                            { };
-    my $schema    = Koha::Schema->connect(
-        "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
-        $db_user, $db_passwd, $db_opts );
+    my $schema = Koha::Schema->connect( sub { C4::Context->dbh } );
     return $schema;
 }
 
index cffc631..111855c 100644 (file)
@@ -15,6 +15,6 @@ __PACKAGE__->load_namespaces;
 # Created by DBIx::Class::Schema::Loader v0.07025 @ 2013-10-14 20:56:21
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oDUxXckmfk6H9YCjW8PZTw
 
+__PACKAGE__->storage_type('Koha::Storage');
 
-# You can replace this text with custom content, and it will be preserved on regeneration
 1;
diff --git a/Koha/Storage.pm b/Koha/Storage.pm
new file mode 100644 (file)
index 0000000..599b6a1
--- /dev/null
@@ -0,0 +1,16 @@
+use utf8;
+package Koha::Storage;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Storage::DBI';
+sub DESTROY {
+    my $self = shift;
+
+    # destroy just the object if not native to this process/thread
+    $self->_preserve_foreign_dbh;
+
+    $self->_dbh(undef);
+}
+1;