/**
  * Run migrations
  *
  * @param array $options
  *
  * @throws Exception
  * @throws ModelException
  * @throws ScriptException
  */
 public static function run(array $options)
 {
     $path = $options['directory'];
     $migrationsDir = $options['migrationsDir'];
     if (!file_exists($migrationsDir)) {
         throw new ModelException('Migrations directory could not found.');
     }
     self::$_config = $options['config'];
     if (!self::$_config instanceof Config) {
         throw new ModelException('Internal error. Config should be instance of \\Phalcon\\Config');
     }
     $finalVersion = null;
     if (isset($options['version']) && $options['version'] !== null) {
         $finalVersion = new VersionItem($options['version']);
     }
     $tableName = 'all';
     if (isset($options['tableName'])) {
         $tableName = $options['tableName'];
     }
     $versions = ModelMigration::scanForVersions($migrationsDir);
     if (!count($versions)) {
         throw new ModelException("Migrations were not found at {$migrationsDir}");
     }
     // set default final version
     if (!$finalVersion) {
         $finalVersion = VersionItem::maximum($versions);
     }
     // read current version
     if (is_file($path . '.phalcon')) {
         unlink($path . '.phalcon');
         mkdir($path . '.phalcon');
         chmod($path . '.phalcon', 0775);
     }
     self::$_migrationFid = $path . '.phalcon/migration-version';
     // init ModelMigration
     if (!is_null(self::$_config->get('database'))) {
         throw new ScriptException('Cannot load database configuration');
     }
     ModelMigration::setup(self::$_config->get('database'));
     ModelMigration::setMigrationPath($migrationsDir);
     $initialVersion = self::getCurrentVersion();
     if ($initialVersion->getStamp() == $finalVersion->getStamp()) {
         return;
         // nothing to do
     }
     $direction = ModelMigration::DIRECTION_FORWARD;
     if ($finalVersion->getStamp() < $initialVersion->getStamp()) {
         $direction = ModelMigration::DIRECTION_BACK;
     }
     // run migration
     $lastGoodVersion = $initialVersion;
     $versionsBetween = VersionItem::between($initialVersion, $finalVersion, $versions);
     foreach ($versionsBetween as $version) {
         if ($tableName == 'all') {
             $iterator = new DirectoryIterator($migrationsDir . DIRECTORY_SEPARATOR . $version);
             foreach ($iterator as $fileInfo) {
                 if (!$fileInfo->isFile() || 0 !== strcasecmp($fileInfo->getExtension(), 'php')) {
                     continue;
                 }
                 ModelMigration::migrate($initialVersion, $version, $fileInfo->getBasename('.php'), $direction);
             }
         } else {
             ModelMigration::migrate($initialVersion, $version, $tableName, $direction);
         }
         self::setCurrentVersion((string) $lastGoodVersion, (string) $version);
         $lastGoodVersion = $version;
         print Color::success('Version ' . $version . ' was successfully migrated');
         $initialVersion = $version;
     }
 }
 /**
  * List migrations along with statuses
  *
  * @param array $options
  *
  * @throws Exception
  * @throws ModelException
  * @throws ScriptException
  *
  */
 public static function listAll(array $options)
 {
     // Define versioning type to be used
     if (true === $options['tsBased']) {
         VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED);
     } else {
         VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
     }
     $migrationsDir = rtrim($options['migrationsDir'], '/');
     if (!file_exists($migrationsDir)) {
         throw new ModelException('Migrations directory was not found.');
     }
     /** @var Config $config */
     $config = $options['config'];
     if (!$config instanceof Config) {
         throw new ModelException('Internal error. Config should be an instance of \\Phalcon\\Config');
     }
     // Init ModelMigration
     if (!isset($config->database)) {
         throw new ScriptException('Cannot load database configuration');
     }
     $finalVersion = null;
     if (isset($options['version']) && $options['version'] !== null) {
         $finalVersion = VersionCollection::createItem($options['version']);
     }
     $versionItems = ModelMigration::scanForVersions($migrationsDir);
     if (!isset($versionItems[0])) {
         throw new ModelException('Migrations were not found at ' . $migrationsDir);
     }
     // Set default final version
     if ($finalVersion === null) {
         $finalVersion = VersionCollection::maximum($versionItems);
     }
     ModelMigration::setup($config->database);
     ModelMigration::setMigrationPath($migrationsDir);
     self::connectionSetup($options);
     $initialVersion = self::getCurrentVersion($options);
     $completedVersions = self::getCompletedVersions($options);
     if ($finalVersion->getStamp() < $initialVersion->getStamp()) {
         $direction = ModelMigration::DIRECTION_BACK;
         $versionItems = VersionCollection::sortDesc($versionItems);
     } else {
         $direction = ModelMigration::DIRECTION_FORWARD;
         $versionItems = VersionCollection::sortAsc($versionItems);
     }
     foreach ($versionItems as $versionItem) {
         if (ModelMigration::DIRECTION_FORWARD === $direction && isset($completedVersions[(string) $versionItem])) {
             print Color::success('Version ' . (string) $versionItem . ' was already applied');
             continue;
         } elseif (ModelMigration::DIRECTION_BACK === $direction && !isset($completedVersions[(string) $versionItem])) {
             print Color::success('Version ' . (string) $versionItem . ' was already rolled back');
             continue;
         }
         if (ModelMigration::DIRECTION_FORWARD === $direction) {
             print Color::error('Version ' . (string) $versionItem . ' was not applied');
             continue;
         } elseif (ModelMigration::DIRECTION_BACK === $direction) {
             print Color::error('Version ' . (string) $versionItem . ' was not rolled back');
             continue;
         }
     }
 }