/**
  * Run migrations
  *
  * @param array $options
  *
  * @throws Exception
  * @throws ModelException
  * @throws ScriptException
  * @throws \Exception
  */
 public static function run(array $options)
 {
     $path = $options['directory'];
     $migrationsDir = $options['migrationsDir'];
     $config = $options['config'];
     $version = null;
     if (isset($options['version']) && $options['version'] !== null) {
         $version = new VersionItem($options['version']);
     }
     if (isset($options['tableName'])) {
         $tableName = $options['tableName'];
     } else {
         $tableName = 'all';
     }
     if (!file_exists($migrationsDir)) {
         throw new ModelException('Migrations directory could not found');
     }
     $versions = array();
     $iterator = new \DirectoryIterator($migrationsDir);
     foreach ($iterator as $fileinfo) {
         if ($fileinfo->isDir()) {
             if (preg_match('/[a-z0-9](\\.[a-z0-9]+)+/', $fileinfo->getFilename(), $matches)) {
                 $versions[] = new VersionItem($matches[0], 3);
             }
         }
     }
     if (count($versions) == 0) {
         throw new ModelException('Migrations were not found at ' . $migrationsDir);
     } else {
         if ($version === null) {
             $version = VersionItem::maximum($versions);
         }
     }
     if (is_file($path . '.phalcon')) {
         unlink($path . '.phalcon');
         mkdir($path . '.phalcon');
     }
     $migrationFid = $path . '.phalcon/migration-version';
     if (file_exists($migrationFid)) {
         $fromVersion = trim(file_get_contents($migrationFid));
     } else {
         $fromVersion = null;
     }
     if (isset($config->database)) {
         ModelMigration::setup($config->database);
     } else {
         throw new \Exception("Cannot load database configuration");
     }
     ModelMigration::setMigrationPath($migrationsDir . '/' . $version . '/');
     $versionsBetween = VersionItem::between($fromVersion, $version, $versions);
     // get rid of the current version, we don't want migrations to run for our
     // existing version.
     if (isset($versionsBetween[0]) && (string) $versionsBetween[0] == $fromVersion) {
         unset($versionsBetween[0]);
     }
     foreach ($versionsBetween as $version) {
         if ($tableName == 'all') {
             $iterator = new \DirectoryIterator($migrationsDir . '/' . $version);
             foreach ($iterator as $fileinfo) {
                 if ($fileinfo->isFile()) {
                     if (preg_match('/\\.php$/', $fileinfo->getFilename())) {
                         ModelMigration::migrateFile((string) $version, $migrationsDir . '/' . $version . '/' . $fileinfo->getFilename());
                     }
                 }
             }
         } else {
             $migrationPath = $migrationsDir . '/' . $version . '/' . $tableName . '.php';
             if (!file_exists($migrationPath)) {
                 throw new ScriptException('Migration class was not found ' . $migrationPath);
             }
             ModelMigration::migrateFile((string) $version, $migrationPath);
         }
         print Color::success('Version ' . $version . ' was successfully migrated') . PHP_EOL;
     }
     file_put_contents($migrationFid, (string) $version);
 }