/** * prepare to migrate * * @param string $destination The version desired * @param string $direction Up or Down * * @return void */ protected function _prepareToMigrate($destination, $direction) { $this->_logger->debug(__METHOD__ . ' Start'); $this->_logger->debug('Destination: ' . $destination . ' - direction: ' . $direction); try { $this->_return .= $this->_prefixText . "\tMigrating " . strtoupper($direction); if (!is_null($destination)) { $this->_return .= " to: {$destination}\n"; } else { $this->_return .= ":\n"; } $migrations = $this->_migratorUtil->getRunnableMigrations($this->_migrationDir, $direction, $destination); if (count($migrations) == 0) { $msg = 'No relevant migrations to run. Exiting...'; $this->_logger->info($msg); $this->_return .= $this->_prefixText . "\n" . trim($this->_prefixText . " {$msg}\n"); return; } $this->_runMigrations($migrations, $direction); } catch (Exception $ex) { $this->_logger->err('Exception: ' . $ex->getMessage()); throw $ex; } $this->_logger->debug(__METHOD__ . ' End'); }
/** * Primary task entry point * * @param array $args Arguments to task * * @return string */ public function execute($args) { $return = 'Started: ' . date('Y-m-d g:ia T') . "\n\n" . "[db:status]:\n"; require_once 'Phigrate/Util/Migrator.php'; $util = new Phigrate_Util_Migrator($this->_adapter); $migrations = $util->getExecutedMigrations(); $this->_logger->debug('Migrations: ' . var_export($migrations, true)); $files = $util->getMigrationFiles($this->_migrationDir, 'up'); $this->_logger->debug('Files: ' . var_export($files, true)); $applied = array(); $notApplied = array(); foreach ($files as $file) { $key = array_search($file['version'], $migrations); require_once $this->_migrationDir . '/' . $file['file']; $class = new $file['class']($this->_adapter); // Get comment of migration limited to 50 caracters $comment = $class->getComment(); if (!empty($comment)) { // if comment is more than 50 caracters, cut string if (strlen($comment) > 50) { $comment = substr($comment, 0, 50) . '...'; } $comment = self::PURPLE_COLOR . ' (' . $comment . ')'; } unset($class); if (false !== $key) { $applied[] = self::GREEN_COLOR . $file['class'] . ' [ ' . $file['version'] . ' ]' . $comment . self::END_COLOR; unset($migrations[$key]); } else { $notApplied[] = self::ORANGE_COLOR . $file['class'] . ' [ ' . $file['version'] . ' ]' . $comment . self::END_COLOR; } } if (count($applied) > 0) { $return .= $this->_displayMigrations($applied, 'APPLIED'); } if (count($notApplied) > 0) { $return .= $this->_displayMigrations($notApplied, 'NOT APPLIED'); } if (count($migrations) > 0) { foreach ($migrations as $key => $migration) { $migrations[$key] = self::RED_COLOR . '??? [ ' . $migration . ' ]' . self::END_COLOR; } $return .= $this->_displayMigrations($migrations, 'APPLIED WITHOUT MIGRATION FILE'); } $return .= "\n\nFinished: " . date('Y-m-d g:ia T') . "\n\n"; return $return; }
public function testGetMigrationFiles() { require_once 'Phigrate/Exception/InvalidMigrationDir.php'; try { $directory = '/tmp/migrate'; $this->object->getMigrationFiles($directory, 'up'); $this->fail('The directory of migration files is incorrect!'); } catch (Phigrate_Exception_InvalidMigrationDir $e) { $msg = 'Phigrate_Util_Migrator - (' . $directory . ') is not a directory.'; $this->assertEquals($msg, $e->getMessage()); } $actualFiles = $this->object->getMigrationFiles('/tmp', 'up'); $this->assertEmpty($actualFiles); }