diff --git a/mongodb.drush.inc b/mongodb.drush.inc index 840d36b..4b54825 100644 --- a/mongodb.drush.inc +++ b/mongodb.drush.inc @@ -11,12 +11,12 @@ * Implementation of hook_drush_command(). */ function mongodb_drush_command() { -// $options['--alias'] = 'The alias defined in the variable mongodb_collections.'; + $options['--select'] = 'When connecting to replica set select which server to connect to.'; $items['mongodb-connect'] = array( 'description' => 'A string for connecting to the mongodb.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, -// 'options' => $options, + 'options' => $options, 'arguments' => array( 'alias' => 'The connection', ), @@ -25,7 +25,7 @@ function mongodb_drush_command() { $items['mongodb-cli'] = array( 'description' => "Open a mongodb command-line interface using Drupal's credentials.", 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, -// 'options' => $options, + 'options' => $options, 'examples' => array( '`drush mongodb-connect`' => 'Connect to the mongodb.', ), @@ -44,7 +44,6 @@ function mongodb_drush_command() { 'alias' => 'The connection', ), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, -// 'options' => $options, ); $items['mongodb-query'] = array( @@ -57,9 +56,7 @@ function mongodb_drush_command() { 'alias' => 'The connection', 'query' => 'A mongodb query. Mandatory.', ), -// 'options' => array( -// '--extra' => 'Add custom options to the mongodb command.', -// ) + $options, + 'options' => $options, 'aliases' => array('mdbq'), ); @@ -122,12 +119,45 @@ function _drush_mongodb_connect($alias) { $alias = 'default'; } $connection = $connections[$alias]; - $host = $connection['host']; + // If we are dealing with connection to replica set by default connect + // to primary server which might not always be in connections configuration. + // With --select option you can specify exactly which server in replica set + // to connect to. + if (isset($connection['connection_options']['replicaSet'])) { + $select = drush_get_option(array('select'), FALSE); + $mongo_connection = new Mongo('mongodb://' . $connection['host'], $connection['connection_options']); + $members = $mongo_connection->getHosts(); + $options = array(); + foreach ($members as $key => $member) { + $options[$key] = $key; + if ($member['state'] == MONGODB_REPLICA_SET_PRIMARY) { + $options[$key] .= " PRIMARY"; + $primary_member = $key; + } + } + if ($select) { + $host = drush_choice($options); + } + else { + $host = $primary_member; + } + if ($host != $primary_member) { + $slave_connection = TRUE; + } + } + else { + $host = $connection['host']; + } $db = $connection['db']; - + $query = $host; $query .= '/' . $db; - + // If we have slave ok configured in settings or user selected secondary member + // lets notify users how to send queries against secondary member. + if (!empty($connection['slave_ok']) || $slave_connection) { + drush_print_r('It looks like you are connecting to secondary member don\'t forget to run rs.slaveOk() to be able to query it.'); + } + $command = 'mongo ' . $query; return $command; } diff --git a/mongodb.module b/mongodb.module index 69cf3b1..5788ead 100644 --- a/mongodb.module +++ b/mongodb.module @@ -1,5 +1,7 @@