From fd3e467473005d784de90facf60399561a41d49b Mon Sep 17 00:00:00 2001
From: Tom Swiggers <tom.swiggers@inuits.be>
Date: Tue, 8 Mar 2011 13:44:52 +0100
Subject: [PATCH] Issue #1085000 by tomswiggers: extra parameters for updatedb

---
 commands/core/core.drush.inc      |   31 +++++++++++++++++++++++++++++--
 commands/core/drupal/update_6.inc |   18 ++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/commands/core/core.drush.inc b/commands/core/core.drush.inc
index 6f61797..1bbc3b5 100644
--- a/commands/core/core.drush.inc
+++ b/commands/core/core.drush.inc
@@ -61,6 +61,10 @@ function core_drush_command() {
     'description' => 'Apply any database updates required (as with running update.php).',
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
     'aliases' => array('updb'),
+    'options' => array(
+      'module' => 'Module name',
+      'number' => 'Schema version number'
+    )
   );
   $items['core-status'] = array(
     'description' => 'Provides a birds-eye view of the current Drupal installation, if any.',
@@ -275,14 +279,37 @@ function core_drush_engine_drupal() {
  * Command handler. Execute update.php code from drush.
  */
 function drush_core_updatedb() {
+  $update_main = TRUE;
+  $args = func_get_args();
+
   if (drush_get_context('DRUSH_SIMULATE')) {
     drush_log(dt('updatedb command does not support --simulate option.'), 'ok');
     return TRUE;
   }
 
   drush_include_engine('drupal', 'update', drush_drupal_major_version());
-  if (update_main() === FALSE) {
-    return FALSE;
+
+  if (is_array($args) && !empty($args) && count($args) >= 2) {
+
+    if (drush_drupal_major_version() != 6) {
+      drush_log(dt('Not supported for other version than 6'), 'error');
+      return FALSE;
+    }
+
+    drush_log(dt('Perform update @number for module @module.', array('@module' => $args[0], '@number' => $args[1])), 'ok');
+
+    $context = array('results' => array($args[0] => array('#abort' => FALSE)));
+
+    update_main_prepare();
+    _update_do_one($args[0], $args[1], $context);
+    $update_main = FALSE;
+  }
+
+  if ($update_main) {
+
+    if (update_main() === FALSE) {
+      return FALSE;
+    }
   }
 
   if (drush_drupal_major_version() <= 6) {
diff --git a/commands/core/drupal/update_6.inc b/commands/core/drupal/update_6.inc
index f3bcc50..42937b3 100644
--- a/commands/core/drupal/update_6.inc
+++ b/commands/core/drupal/update_6.inc
@@ -478,12 +478,14 @@ function _update_do_one($module, $number, &$context) {
   }
 
   $function = $module .'_update_'. $number;
-  drush_log("Executing $function", 'success');
 
   if (function_exists($function)) {
+    drush_log("Executing $function", 'success');
     $ret = $function($context['sandbox']);
     $context['results'][$module] = $ret;
     _drush_log_update_sql($ret);
+  } else {
+    drush_log("Function $function doesn't exists.", 'error');
   }
 
   if (isset($ret['#finished'])) {
@@ -491,7 +493,7 @@ function _update_do_one($module, $number, &$context) {
     unset($ret['#finished']);
   }
 
-  if ($context['finished'] == 1 && empty($context['results'][$module]['#abort'])) {
+  if ($context['finished'] == 1 && empty($context['results'][$module]['#abort']) && $number > _get_installed_schema_version($module)) {
     drupal_set_installed_schema_version($module, $number);
   }
 
@@ -502,3 +504,15 @@ function _update_batch_command($id) {
   drush_batch_command($id);
 }
 
+/**
+ * Get the installed schema version for a given module
+ */
+function _get_installed_schema_version($name) {
+  $record = db_fetch_object(db_query("SELECT schema_version FROM {system} WHERE name = '%s'", $name));
+
+  if ($record->schema_version) {
+    return $record->schema_version;
+  } else {
+    return -1;
+  }
+}
-- 
1.7.1

