/** * Find the last morph function in the previous migration files * * @param VersionItem $version * @param string $tableName * @return null|\Phalcon\Mvc\Model\Migration * * @throws Exception */ private static function createPrevClassWithMorphMethod(VersionItem $version, $tableName) { $prevVersions = array(); $iterator = new \DirectoryIterator(self::$_migrationPath); foreach ($iterator as $fileinfo) { if ($fileinfo->isDir() && preg_match('/[a-z0-9](\\.[a-z0-9]+)+/', $fileinfo->getFilename(), $matches)) { $prevVersion = new VersionItem($matches[0], 3); if ($prevVersion->getStamp() <= $version->getStamp()) { $prevVersions[] = $prevVersion; } } } $prevVersions = VersionItem::sortDesc($prevVersions); foreach ($prevVersions as $prevVersion) { $migration = self::createClass($prevVersion, $tableName); if (!is_null($migration) && method_exists($migration, 'morph')) { return $migration; } } return null; }
/** * Find the last morph function in the previous migration files * * @param VersionItem $toVersion * @param string $tableName * @return null|\Phalcon\Mvc\Model\Migration * * @throws Exception */ private static function createPrevClassWithMorphMethod(VersionItem $toVersion, $tableName) { $prevVersions = array(); $versions = self::scanForVersions(self::$_migrationPath); foreach ($versions as $prevVersion) { if ($prevVersion->getStamp() <= $toVersion->getStamp()) { $prevVersions[] = $prevVersion; } } $prevVersions = VersionItem::sortDesc($prevVersions); foreach ($prevVersions as $prevVersion) { $migration = self::createClass($prevVersion, $tableName); if (is_object($migration) && method_exists($migration, 'morph')) { return $migration; } } return null; }
/** * 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.'); } $config = $options['config']; if (!$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']; } // read all versions $versions = array(); $iterator = new \DirectoryIterator($migrationsDir); foreach ($iterator as $fileinfo) { if ($fileinfo->isDir() && 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); } // set default final version if ($finalVersion === null) { $finalVersion = VersionItem::maximum($versions); } // read current version if (is_file($path . '.phalcon')) { unlink($path . '.phalcon'); mkdir($path . '.phalcon'); } $migrationFid = $path . '.phalcon/migration-version'; $initialVersion = new VersionItem(file_exists($migrationFid) ? file_get_contents($migrationFid) : null); if ($initialVersion->getStamp() == $finalVersion->getStamp()) { return; // nothing to do } // init ModelMigration if (!isset($config->database)) { throw new ScriptException('Cannot load database configuration'); } ModelMigration::setup($config->database); ModelMigration::setMigrationPath($migrationsDir); // run migration $versionsBetween = VersionItem::between($initialVersion, $finalVersion, $versions); foreach ($versionsBetween as $k => $version) { /** @var \Phalcon\Version\Item $version */ if ($tableName == 'all') { $iterator = new \DirectoryIterator($migrationsDir . '/' . $version); foreach ($iterator as $fileinfo) { if (!$fileinfo->isFile() || !preg_match('/\\.php$/i', $fileinfo->getFilename())) { continue; } ModelMigration::migrate($initialVersion, $version, $fileinfo->getBasename('.php')); } } else { ModelMigration::migrate($initialVersion, $version, $tableName); } file_put_contents($migrationFid, (string) $version); print Color::success('Version ' . $version . ' was successfully migrated'); $initialVersion = $version; } }
/** * 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.'); } $config = $options['config']; if (!$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'); } $migrationFid = $path . '.phalcon/migration-version'; $initialVersion = new VersionItem(file_exists($migrationFid) ? file_get_contents($migrationFid) : null); if ($initialVersion->getStamp() == $finalVersion->getStamp()) { return; // nothing to do } // init ModelMigration if (!isset($config->database)) { throw new ScriptException('Cannot load database configuration'); } ModelMigration::setup($config->database); ModelMigration::setMigrationPath($migrationsDir); $direction = ModelMigration::DIRECTION_FORWARD; if ($finalVersion->getStamp() < $initialVersion->getStamp()) { $direction = ModelMigration::DIRECTION_BACK; } // run migration $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); } file_put_contents($migrationFid, (string) $version); print Color::success('Version ' . $version . ' was successfully migrated'); $initialVersion = $version; } }