From 5262611fe5da71680f7731bd1308f3a3bddd86b7 Mon Sep 17 00:00:00 2001
From: Dave Hall <dave.hall@skwashd.com>
Date: Wed, 11 Jan 2012 18:19:33 +1100
Subject: [PATCH 66/66] Issue #1335626 by skwashd: Prefer db-url/database
 values from aliases if they exist

---
 includes/bootstrap.inc |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 052a6cd..6bb790c 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -815,6 +815,8 @@ function _drush_bootstrap_drupal_site() {
 function _drush_bootstrap_drupal_configuration() {
   global $conf;
 
+  static $first_pass = TRUE;
+
   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
 
   // Unset drupal error handler and restore drush's one.
@@ -836,9 +838,22 @@ function _drush_bootstrap_drupal_configuration() {
   $conf = is_array($conf) ? array_merge($conf, $override) : $conf;
 
   // Populate the DRUSH_DB_CREDENTIALS with the fields loaded from the configuration.
+  $db_spec = array();
+  if ($first_pass) {
+    $first_pass = FALSE;
+    $db_spec = _drush_sql_get_db_spec();
+  }
   $creds = array();
   switch (drush_drupal_major_version()) {
      case 6:
+       if (!empty($db_spec)) {
+         $db_url = "{$db_spec['driver']}://{$db_spec['username']}:{$db_spec['password']}@{$db_spec['host']}";
+         if (!empty($db_spec['port'])) {
+           $db_url .= ":{$db_spec['port']}";
+         }
+         $db_url .= "/{$db_spec['database']}";
+         $GLOBALS['db_url'] = $db_url;
+       }
        if (!empty($GLOBALS['db_url'])) {
          $url = $GLOBALS['db_url'];
          if (is_array($url)) {
@@ -856,6 +871,9 @@ function _drush_bootstrap_drupal_configuration() {
        break;
      case 7:
      default:
+       if (!empty($db_spec)) {
+         $GLOBALS['databases']['default']['default'] = $db_spec;
+       }
        if (!empty($GLOBALS['databases']['default']['default'])) {
          $conn = $GLOBALS['databases']['default']['default'];
          // Fill in defaults to prevent notices.
-- 
1.7.5.4

