? includes/table.inc
Index: commands/core/upgrade.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/core/upgrade.drush.inc,v
retrieving revision 1.24
diff -u -p -r1.24 upgrade.drush.inc
--- commands/core/upgrade.drush.inc	31 Dec 2010 01:10:37 -0000	1.24
+++ commands/core/upgrade.drush.inc	26 Jan 2011 00:35:12 -0000
@@ -49,15 +49,19 @@ function upgrade_drush_help($section) {
 
 function drush_upgrade_site_upgrade_validate($target_key = NULL) {
   if (empty($target_key)) {
-    return drush_set_error(dt('Missing argument: target'));
+    return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Missing argument: target'));
   }
 
   if (!$target_alias = drush_sitealias_get_record($target_key)) {
-    return drush_set_error('Site alias not found: @target-key. See example.drushrc.inc.', array('@target-key' => $target_key));
+    return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Site alias not found: @target-key. See example.drushrc.php.', array('@target-key' => $target_key)));
   }
 
   if (!file_exists(dirname($target_alias['root']))) {
-    drush_set_error('Site alias root not found: @root. See example.drushrc.inc.', array('@root' => dirname($target_alias['root'])));
+    drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Parent directory for site alias root not found: @root; this folder must exist before running site-upgrade. See example.drushrc.php.', array('@root' => dirname($target_alias['root']))));
+  }
+
+  if (realpath($target_alias['root']) == realpath(DRUPAL_ROOT)) {
+    drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Target site alias must have a different Drupal root directory than the source site.  Both are at @root.', array('@root' => $target_alias['root'])));
   }
 }
 
@@ -68,6 +72,10 @@ function drush_upgrade_site_upgrade($tar
   $source_version = drush_drupal_major_version();
   $target_version = $source_version + 1;
   $target_alias = drush_sitealias_get_record($target_key);
+  if (empty($target_alias)) {
+    return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt("Could not find target site for upgrade: !target", array("!target" => $target_key)));
+  }
+
   $destination_core = $target_alias['root'];
 
   // Fetch target core and place as per target alias root.
@@ -83,6 +91,11 @@ function drush_upgrade_site_upgrade($tar
     if (drush_get_error()) return -1; // Early exit if we see an error.
   }
 
+  // Check and see if there is a Drupal site at the target
+  if (!file_exists($destination_core . '/includes/bootstrap.inc')) {
+    return drush_set_error('DRUSH_UPGRADE_NO_DRUPAL', dt('Drupal could not be downloaded to the target directory, @root.  Move existing content out of the way first.', array('@root' => $target_alias['root'])));
+  }
+  
   // Get enabled projects and their paths.
   // TODO: D5 compatibility.
   _update_cache_clear();
@@ -128,7 +141,7 @@ function drush_upgrade_site_upgrade($tar
   }
 
   // Append new $db_url with new DB name in target's settings.php.
-  drush_upgrade_fix_db_url($target_alias, $settings_destination);
+  drush_upgrade_fix_db_url($target_alias, $settings_destination, $target_version);
 
   // Copy source database to target database. The source DB is not changed.
   // Always set 'common' at minimum. Sites that want other can create other key in drushrc.php.
@@ -152,21 +165,32 @@ function drush_upgrade_site_upgrade($tar
 }
 
 // Replace db_url with DB name from target. updatedb will later append a DBTNG compatible version.
-function drush_upgrade_fix_db_url($target_alias, $settings_destination) {
+function drush_upgrade_fix_db_url($target_alias, $settings_destination, $target_version) {
   $old_url = $GLOBALS['db_url'];
   if (is_array($old_url)) {
     $old_url = $old_url['default'];
   }
+  $old_databases = $GLOBALS['databases'];
+  if (empty($old_databases)) {
+    $old_databases = drush_sitealias_convert_db_from_db_url($old_url);
+  }
+  
   $target_alias_databases = sitealias_get_databases_from_record($target_alias);
-  $new_url = substr($old_url, 0, strrpos(trim($old_url), '/')) . '/'. $target_alias_databases['default']['default']['database'];
+  drush_print_r($target_alias_databases);
+  $database_name = $target_alias_databases['default']['default']['database'];
+  if (empty($database_name)) {
+    $database_name = str_replace("@", "", $target_alias['name']) . "db";
+    drush_log(dt("No database name specified; defaulting to %dbname", array("%dbname" => $database_name)), 'warning');
+  }
 
   $append = "\n# Added by drush site-upgrade.";
-  if (drush_drupal_major_version() <= 6) {
+  if ($target_version <= 6) {
+    $new_url = substr($old_url, 0, strrpos(trim($old_url), '/')) . '/'. $database_name;
     $append .= "\n" . '$db_url = \'' . $new_url . '\';';
   }
   else {
-    $databases = $GLOBALS['databases'];
-    $databases['default']['default']['database'] = $target_alias_databases['default']['default']['database'];
+    $databases = $old_databases;
+    $databases['default']['default']['database'] = $database_name;
     $append .= "\n" . '$databases = ' . var_export($databases, TRUE) . ';';
   }
   drush_op('file_put_contents', $settings_destination, $append, FILE_APPEND);
Index: includes/sitealias.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/sitealias.inc,v
retrieving revision 1.76
diff -u -p -r1.76 sitealias.inc
--- includes/sitealias.inc	25 Jan 2011 21:50:09 -0000	1.76
+++ includes/sitealias.inc	26 Jan 2011 00:35:12 -0000
@@ -639,7 +639,9 @@ function drush_sitealias_add_db_settings
   if (!isset($alias_record['db-url']) && !isset($alias_record['databases']) && !isset($alias_record['site-list'])) {
     $values = drush_invoke_sitealias_args($alias_record, "sql-conf", array(), array('all' => TRUE));
     if (isset($values['object'])) {
-      $alias_record['databases'] = $values['object'];
+      if (!empty($values['object'])) {
+        $alias_record['databases'] = $values['object'];
+      }
       $altered_record = TRUE;
       // If there are any special settings in the '@self' record returned by drush_invoke_sitealias_args,
       // then add those into our altered record as well
@@ -650,10 +652,10 @@ function drush_sitealias_add_db_settings
       if (array_key_exists('name', $alias_record)) {
         $all_site_aliases =& drush_get_context('site-aliases');
         $all_site_aliases[$alias_record['name']] = $alias_record;
-	// Check and see if this record is a copy of 'self'
-	if (($alias_record['name'] != 'self') && array_key_exists('@self', $all_site_aliases) && ($all_site_aliases['@self']['name'] == $alias_record['name'])) {
+        // Check and see if this record is a copy of 'self'
+        if (($alias_record['name'] != 'self') && array_key_exists('@self', $all_site_aliases) && ($all_site_aliases['@self']['name'] == $alias_record['name'])) {
           $all_site_aliases['@self'] = $alias_record;
-	}
+        }
       }
     }
   }
@@ -1048,11 +1050,11 @@ function _drush_find_local_sites_at_root
     else {
       $bootstrap_files = drush_scan_directory($base_path, '/' . basename(DRUSH_DRUPAL_BOOTSTRAP) . '/' , array('.', '..', 'CVS'), 0, drush_get_option('search-depth', $search_depth) + 1, 'filename', 1);
       foreach ($bootstrap_files as $one_bootstrap => $info) {
-	$includes_dir = dirname($one_bootstrap);
-	if (basename($includes_dir) == basename(dirname(DRUSH_DRUPAL_BOOTSTRAP))) {
+        $includes_dir = dirname($one_bootstrap);
+        if (basename($includes_dir) == basename(dirname(DRUSH_DRUPAL_BOOTSTRAP))) {
           $drupal_root = dirname($includes_dir);
           $site_list = array_merge(_drush_find_local_sites_in_sites_folder($drupal_root), $site_list);
-	}
+        }
       }
     }
   }
