src/Manager.php
protected function executeMigration($targetRevision, $environment, $direction)
    {
        $this->eventDispatcher->dispatch('start');
        
        // get last applied migration
        $latestRevisionId = $this->getLatestAppliedRevisionId($environment);
        
        // get list of migrations
        $availableRevisions = $this->getAvailableRevisions();
        
        // execute
        if ($direction === 1) {
            $this->eventDispatcher->dispatch('before_migrate');
            
            ksort($availableRevisions);

            foreach ($availableRevisions as $revision) {
                if ($revision->getId() <= $latestRevisionId) {
                    continue;
                }
                
                $event = new ApplyRevisionEvent();
                $event->setRevision($revision);
                
                $this->eventDispatcher->dispatch('before_migrate_revision', $event);

                $revisionPath = $this->getMigrationsDir() . '/' . $revision->getFilename();
                require_once $revisionPath;

                $className = $revision->getName();
                
                $migration = new $className(
                    $this->getClient($environment)
                );

                $migration->setEnvironment($environment);

                $migration->up();
                
                $this->logUp($revision->getId(), $environment);
                
                $this->eventDispatcher->dispatch('migrate_revision', $event);
                
                if ($targetRevision && in_array($targetRevision, array($revision->getId(), $revision->getName()))) {
                    break;
                }
            }
            
            $this->eventDispatcher->dispatch('migrate');
        } else {
            $this->eventDispatcher->dispatch('before_rollback');
            
            // check if nothing to revert
            if (!$latestRevisionId) {
                return;
            }
            
            krsort($availableRevisions);

            foreach ($availableRevisions as $revision) {
                if ($revision->getId() > $latestRevisionId) {
                    continue;
                }
                
                if ($targetRevision && in_array($targetRevision, array($revision->getId(), $revision->getName()))) {
                    break;
                }
                
                $event = new ApplyRevisionEvent();
                $event->setRevision($revision);
                
                $this->eventDispatcher->dispatch('before_rollback_revision', $event);

                $revisionPath = $this->getMigrationsDir() . '/' . $revision->getFilename();
                require_once $revisionPath;

                $className = $revision->getName();
                
                $migration = new $className($this->getClient($environment));
                $migration->setEnvironment($environment);
                $migration->down();
                
                $this->logDown($revision->getId(), $environment);
                
                $this->eventDispatcher->dispatch('rollback_revision', $event);
                
                if (!$targetRevision) {
                    break;
                }
            }
            
            $this->eventDispatcher->dispatch('rollback');
        }
        
        $this->eventDispatcher->dispatch('stop');
        
        // clear cached applied revisions
        unset($this->appliedRevisions[$environment]);
    }
protected function executeMigration($targetRevision, $environment, $direction)
    {
        $this->eventDispatcher->dispatch('start');
        
        // get last applied migration
        $latestRevisionId = $this->getLatestAppliedRevisionId($environment);
        
        // get list of migrations
        $availableRevisions = $this->getAvailableRevisions();
        
        // execute
        if ($direction === 1) {
            $this->eventDispatcher->dispatch('before_migrate');
            
            ksort($availableRevisions);

            foreach ($availableRevisions as $revision) {
                if ($revision->getId() <= $latestRevisionId) {
                    continue;
                }
                
                $event = new ApplyRevisionEvent();
                $event->setRevision($revision);
                
                $this->eventDispatcher->dispatch('before_migrate_revision', $event);

                $revisionPath = $this->getMigrationsDir() . '/' . $revision->getFilename();
                require_once $revisionPath;

                $className = $revision->getName();
                
                $migration = new $className(
                    $this->getClient($environment)
                );

                $migration->setEnvironment($environment);

                $migration->up();
                
                $this->logUp($revision->getId(), $environment);
                
                $this->eventDispatcher->dispatch('migrate_revision', $event);
                
                if ($targetRevision && in_array($targetRevision, array($revision->getId(), $revision->getName()))) {
                    break;
                }
            }
            
            $this->eventDispatcher->dispatch('migrate');
        } else {
            $this->eventDispatcher->dispatch('before_rollback');
            
            // check if nothing to revert
            if (!$latestRevisionId) {
                return;
            }
            
            krsort($availableRevisions);

            foreach ($availableRevisions as $revision) {
                if ($revision->getId() > $latestRevisionId) {
                    continue;
                }
                
                if ($targetRevision && in_array($targetRevision, array($revision->getId(), $revision->getName()))) {
                    break;
                }
                
                $event = new ApplyRevisionEvent();
                $event->setRevision($revision);
                
                $this->eventDispatcher->dispatch('before_rollback_revision', $event);

                $revisionPath = $this->getMigrationsDir() . '/' . $revision->getFilename();
                require_once $revisionPath;

                $className = $revision->getName();
                
                $migration = new $className($this->getClient($environment));
                $migration->setEnvironment($environment);
                $migration->down();
                
                $this->logDown($revision->getId(), $environment);
                
                $this->eventDispatcher->dispatch('rollback_revision', $event);
                
                if (!$targetRevision) {
                    break;
                }
            }
            
            $this->eventDispatcher->dispatch('rollback');
        }
        
        $this->eventDispatcher->dispatch('stop');
        
        // clear cached applied revisions
        unset($this->appliedRevisions[$environment]);
    }
@param string $targetRevision
@param string $environment
@param string $direction

@throws \Sokil\Mongo\Exception
@throws \Sokil\Mongo\Exception\WriteException