Skip to content

Commit

Permalink
Merge pull request #1 from ServiceRunner/master
Browse files Browse the repository at this point in the history
Merge Forks and Bugfix
  • Loading branch information
omeryar committed Oct 16, 2013
2 parents afef7ea + efb536a commit 068be69
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "knodes/li3_ensureindex",
"type": "lithium-library",
"description": "",
"keywords": ["mongodb", "indexes", "li3", "lithium"],
"homepage": "https://github.com/Knodes/li3_ensureindex",
"authors": [
{
"name": "Chris Jones",
"email": "leeked@gmail.com",
"homepage": "https://github.com/leek"
},
{
"name": "Contributors",
"homepage": "https://github.com/Knodes/li3_ensureindex/contributors"
}
],
"require": {
"composer/installers": "*"
}
}
79 changes: 39 additions & 40 deletions extensions/command/EnsureIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,85 @@
namespace li3_ensureindex\extensions\command;

use Exception;
use lithium\core\Environment;
use lithium\core\Libraries;
use lithium\analysis\Inspector;

class EnsureIndexes extends \lithium\console\Command {

class EnsureIndexes extends \lithium\console\Command
{
public $model = null;

protected $defaults = array(
'unique' => false,
'dropDups' => false,
'unique' => false,
'dropDups' => false,
'background' => true,
'safe' => true,
'timeout' => 10000
'safe' => true,
'timeout' => 10000,
);

public function run() {
public function run()
{
$models = Libraries::locate('models', $this->model);

if( empty( $models ) ) {
throw new Exception( "could not locate model " . $this->model );
if (empty($models)) {
throw new Exception('Could not locate model: ' . $this->model);
}

if( !is_array( $models ) ) {
$models = array( $models );
if (!is_array($models)) {
$models = array($models);
}

$counters = array(
'models' => 0,
'indexes' => 0
'models' => 0,
'indexes' => 0,
);

foreach( $models as $model ) {
foreach ($models as $model) {
$model = str_replace("\\\\", "\\", $model);
if (property_exists($model, '_indexes')) {
$indexes = $model::$_indexes;
if (empty($indexes)) {
continue;
}

if( property_exists( $model, '_indexes' ) ) {
$db = $model::connection();

if ( !is_a( $db, 'lithium\data\source\MongoDb' ) ) {
throw new Exception( "This command works only with MongoDB, sorry" );
if (!is_a($db, 'lithium\data\source\MongoDb')) {
throw new Exception('This command works only with MongoDB');
}

$conn = $db->server->{$db->_config['database']};
$collection = $conn->{$model::meta('source')};

$indexes = $model::$_indexes;
if( empty( $indexes ) ) {
continue;
}
$db->connect();
$collection = $db->connection->{$model::meta('source')};

$counters['models']++;
$this->out( "{:heading}ensuring indexes for model $model{:end}" );
$this->out('{:heading}Ensuring indexes for model: ' . $model . '{:end}');

foreach( $indexes as $name => $index ) {
if( empty( $index['keys'] ) ) {
$this->out( "skipping index $name as it didn't define any keys" );
foreach ($indexes as $name => $index) {
if (empty($index['keys'])) {
$this->error(' * No keys defined for index: ' . $name);
continue;
}

$keys = $index['keys'];
unset( $index['keys'] );
if( !isset( $index['name'] ) ) {
unset($index['keys']);

if (!isset($index['name'])) {
$index['name'] = $name;
}

$options = $index + $this->defaults;

try {
$collection->ensureIndex( $keys, $options );
$collection->ensureIndex($keys, $options);
} catch (Exception $e) {
$this->error( "{:command}$name{:end} failed with: " . $e->getMessage() );
$this->error(' * Failed: {:command}' . $name . '{:end} with: ' . $e->getMessage());
continue;
}

$counters['indexes']++;
$this->out( "{:command}$name{:end} ensured" );

$this->out(' * Ensured: {:command}' . $name . '{:end}');
}

}
}

$this->header( "{:heading}done. ensured a total of {$counters['indexes']} indexes on {$counters['models']} models{:end}" );
$this->out('');
$this->header('{:heading}Done! Ensured a total of ' . $counters['indexes'] . ' indexes on ' . $counters['models'] . ' models{:end}');
}

}

0 comments on commit 068be69

Please sign in to comment.