function drush_valid_db_credentials

8.0.x environment.inc drush_valid_db_credentials()
6.x environment.inc drush_valid_db_credentials()
7.x environment.inc drush_valid_db_credentials()
3.x environment.inc drush_valid_db_credentials()
4.x environment.inc drush_valid_db_credentials()
5.x environment.inc drush_valid_db_credentials()
master environment.inc drush_valid_db_credentials()

Tests the currently loaded database credentials to ensure a database connection can be made.

1 call to drush_valid_db_credentials()

File

includes/environment.inc, line 1187
Functions used by drush to query the environment and setting the current configuration.

Code

function drush_valid_db_credentials() {
  $creds = drush_get_context('DRUSH_DB_CREDENTIALS');
  // Do minimal checking that we have the necessary information.
  if (count($creds) == 0) {
    return FALSE;
  }
  $type = $creds['driver'];
  switch (drush_drupal_major_version()) {
    case 5:
    case 6:
      // Check availability of db extension in PHP and also Drupal support.
      if (file_exists('./includes/install.' . $type . '.inc')) {
        require_once './includes/install.' . $type . '.inc';
        $function = $type . '_is_available';
        if (!$function()) {
          drush_log(dt('!type extension for PHP is not installed. Check your php.ini to see how you can enable it.', array('!type' => $type)), 'bootstrap');
          return FALSE;
        }
      }
      else {
        drush_log(dt('!type database type is unsupported.', array('!type' => $type)), 'bootstrap');
        return FALSE;
      }
      // Verify connection settings.
      switch ($type) {
        case 'mysql':
          $hostspec = $creds['port'] ? $creds['host'] . ':' . $creds['port'] : $creds['host'];
          $connection = @mysql_connect($hostspec, $creds['user'], $creds['pass']);
          if (!$connection || !mysql_select_db($creds['name'])) {
            drush_log(mysql_error(), 'bootstrap');
            return FALSE;
          }
          break;
        case 'mysqli':
          $connection = mysqli_init();
          @mysqli_real_connect($connection, $creds['host'], $creds['user'], $creds['pass'], $creds['name'], (int) $creds['port']);
          if (mysqli_connect_errno() > 0) {
            drush_log(mysqli_connect_error(), 'bootstrap');
            return FALSE;
          }
          break;
        case 'pgsql':
          $conn_string = sprintf("host=%s user=%s password=%s dbname=%s", $creds['host'], $creds['user'], $creds['pass'], $creds['name']);
          if (isset($creds['port'])) {
            $conn_string .= ' port=' . $creds['port'];
          }
          // Copied from d6's database.pgsql.inc:
          // pg_last_error() does not return a useful error message for database
          // connection errors. We must turn on error tracking to get at a good error
          // message, which will be stored in $php_errormsg.
          $track_errors_previous = ini_get('track_errors');
          ini_set('track_errors', 1);
          $connection = @pg_connect($conn_string);
          if (!$connection) {
            require_once './includes/unicode.inc';
            drush_log(decode_entities($php_errormsg), 'bootstrap');
            // Restore error tracking setting
            ini_set('track_errors', $track_errors_previous);
            return FALSE;
          }
          // Restore error tracking setting
          ini_set('track_errors', $track_errors_previous);
          break;
      }
      break;
    case 7:
    default:
      // Drupal >=7 requires PDO and drush requires php 5.2, that ships with PDO
      // but it may be compiled with --disable-pdo.
      if (!class_exists('PDO')) {
        drush_log(dt('PDO support is required.'), 'bootstrap');
        return FALSE;
      }
      // Check the database specific driver is available.
      if (!in_array($type, PDO::getAvailableDrivers())) {
        drush_log(dt('!type extension for PHP PDO is not installed. Check your php.ini to see how you can enable it.', array('!type' => $type)), 'bootstrap');
        return FALSE;
      }
      // Build the connection string.
      if ($type === 'sqlite') {
        $constr = 'sqlite:' . $creds['name'];
      }
      else {
        $constr = sprintf("%s:dbname=%s;host=%s", $type, $creds['name'], $creds['host']);
        if (!empty($creds['port'])) {
          $constr .= sprintf(";port=%d", $creds['port']);
        }
      }
      try {
        $db = new PDO($constr, $creds['user'], $creds['pass']);
        $db = null;
      }
      catch (PDOException $e) {
        drush_log($e->getMessage(), 'bootstrap');
        return FALSE;
      }
      break;
  }

  return TRUE;
}