public function run($userId, $mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = "Ensure DB Indexes\n";
     $numberOfIndexesCreated = 0;
     $website = Website::get();
     $onDevMachine = strpos($website->domain, 'dev.') !== false;
     $onLocalMachine = strrpos($website->domain, '.local') !== false;
     $message .= "\n-------------  Main Database:\n";
     $mainCollectionName = ProjectModelMongoMapper::instance()->getCollectionName();
     $mainIndexes = ProjectModelMongoMapper::instance()->INDEXES_REQUIRED;
     $mainIndexesToCreate = MongoStore::getIndexesNotSetInCollection(SF_DATABASE, $mainCollectionName, $mainIndexes);
     $numberOfIndexesCreated += count($mainIndexesToCreate);
     $message .= count($mainIndexesToCreate) . " main indexes created.\n";
     if (($onDevMachine || $onLocalMachine) && MongoStore::hasDB(SF_TEST_DATABASE)) {
         $message .= "\n-------------  Test Database:\n";
         $mainIndexesToCreate = MongoStore::getIndexesNotSetInCollection(SF_TEST_DATABASE, $mainCollectionName, $mainIndexes);
         $numberOfIndexesCreated += count($mainIndexesToCreate);
         $message .= count($mainIndexesToCreate) . " test indexes created.\n";
     }
     if (!$testMode) {
         MongoStore::ensureIndexesInCollection(SF_DATABASE, $mainCollectionName, $mainIndexes);
         if (($onDevMachine || $onLocalMachine) && MongoStore::hasDB(SF_TEST_DATABASE)) {
             MongoStore::ensureIndexesInCollection(SF_TEST_DATABASE, $mainCollectionName, $mainIndexes);
         }
     }
     // loop over every project
     $projectList = new ProjectListModel();
     $projectList->read();
     foreach ($projectList->entries as $projectParams) {
         $project = ProjectModel::getById($projectParams['id']);
         if ($project->appName == 'lexicon') {
             $message .= "\n-------------  {$project->projectName} project:\n";
             $lexiconCollectionName = LexEntryModel::mapper($project->databaseName())->getCollectionName();
             $lexiconIndexes = LexEntryModel::mapper($project->databaseName())->INDEXES_REQUIRED;
             $lexiconIndexesToCreate = MongoStore::getIndexesNotSetInCollection($project->databaseName(), $lexiconCollectionName, $lexiconIndexes);
             $numberOfIndexesCreated += count($lexiconIndexesToCreate);
             $optionListCollectionName = LexOptionListModel::mapper($project->databaseName())->getCollectionName();
             $optionListIndexes = LexOptionListModel::mapper($project->databaseName())->INDEXES_REQUIRED;
             $optionListIndexesToCreate = MongoStore::getIndexesNotSetInCollection($project->databaseName(), $optionListCollectionName, $optionListIndexes);
             $numberOfIndexesCreated += count($optionListIndexesToCreate);
             if (count($lexiconIndexesToCreate) + count($optionListIndexesToCreate) > 0) {
                 $message .= count($lexiconIndexesToCreate) . " lexicon indexes created.\n";
                 $message .= count($optionListIndexesToCreate) . " option list indexes created.\n";
             } else {
                 $message .= "No indexes needed creating.\n";
             }
             if (!$testMode) {
                 MongoStore::ensureIndexesInCollection($project->databaseName(), $lexiconCollectionName, $lexiconIndexes);
                 MongoStore::ensureIndexesInCollection($project->databaseName(), $optionListCollectionName, $optionListIndexes);
             }
         }
     }
     if ($numberOfIndexesCreated > 0) {
         $message .= "\nCreated {$numberOfIndexesCreated} DB Indexes.\n\n";
     } else {
         $message .= "\nAll indexes were present.\n\n";
     }
     return $message;
 }
 public function testMongoStoreEnsureIndexesInCollection_LexProject_Ok()
 {
     // setup
     self::$environ = new LexiconMongoTestEnvironment();
     self::$environ->clean();
     $user1Id = self::$environ->createUser("user1name", "User1 Name", "*****@*****.**");
     $user1 = new UserModel($user1Id);
     $srProject = null;
     $projectId = ProjectCommands::createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE, LexProjectModel::LEXICON_APP, $user1->id->asString(), self::$environ->website, $srProject);
     $project = new LexProjectModel($projectId);
     $databaseName = $project->databaseName();
     $collectionName = LexEntryModel::mapper($databaseName)->getCollectionName();
     $indexes = LexEntryModel::mapper($databaseName)->INDEXES_REQUIRED;
     foreach ($indexes as $index) {
         $this->assertTrue(MongoStore::isIndexIdenticalInCollection($index, $databaseName, $collectionName), 'index not in lexicon: ' . var_export($index, true));
     }
     $additionalIndex = ['key' => ['test' => 1]];
     $indexes[] = $additionalIndex;
     $this->assertFalse(MongoStore::isAllIndexFieldNamesInCollection($additionalIndex, $databaseName, $collectionName));
     MongoStore::ensureIndexesInCollection($databaseName, $collectionName, $indexes);
     foreach ($indexes as $index) {
         $this->assertTrue(MongoStore::isIndexIdenticalInCollection($index, $databaseName, $collectionName), 'index not in lexicon: ' . var_export($index, true));
     }
 }