Database::openConnection

Created on 28 November 2012, about 12 years ago
Updated 16 August 2023, over 1 year ago

This is half bug/half feature request.

I am creating a module for fine tuning master/slave database interaction. Basically writes and reads go through different connections as long as there is not a transaction happening. I know the pitfalls that ensue, we are carefully considering those in the process.

Now to the issue: In /includes/database/database.inc, class Database, the method Database::openConnection attempts to open a DatabaseConnection file in order to load said class and then instantiate it, Here's the current code (please bear with me):

...
$driver_class = 'DatabaseConnection_' . $driver;
require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
...

Regardless of my ultimate goal, I propose for us to change the code to this:

...
$driver_class = 'DatabaseConnection_' . $driver;

if (file_exists(DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc')) {
  require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
} else if (!class_exists($driver_class)) {
  /* issue warning, or error message and stop loading the page */
}

$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
...

I have two reasons for doing this:

  1. I think it is better practice to stop loading the page instead of failing when the settings.php database configuration contains a reference to a db driver that is invalid (i.e. doesn't have a corresponding DatabaseConnection_{driver_name}.ini file in the /includes/database/ directory
  2. It allows for people who want to provide new database drivers to do so through a module, loading their sources in hook_boot and adding the connection info programmatically (through Database::addConnectionInfo(...) for example). It seems a bit of a waste that a class might be defined and not accessible in openConnection. My sample code is pretty simple and I hope it can serve as a starting point for this issue.

If we like this approach I can go and propose the patch for it, along with a sample module to test it and show how to exploit the possibilities brought by it.

Feature request
Status

Postponed: needs info

Version

9.5

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024