Browse Source

Finished console support.

Finished migrate command.
tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
af4455eb03
  1. 5
      framework/base/Controller.php
  2. 2
      framework/console/Request.php
  3. 51
      framework/console/controllers/HelpController.php
  4. 438
      framework/console/controllers/MigrateController.php
  5. 23
      framework/views/migration.php

5
framework/base/Controller.php

@ -317,6 +317,11 @@ class Controller extends Component
return $this->createView()->renderPartial($view, $params); return $this->createView()->renderPartial($view, $params);
} }
public function renderFile($file, $params = array())
{
return $this->createView()->renderFile($file, $params);
}
public function createView() public function createView()
{ {
return new View($this); return new View($this);

2
framework/console/Request.php

@ -15,7 +15,7 @@ namespace yii\console;
*/ */
class Request extends \yii\base\Request class Request extends \yii\base\Request
{ {
const ANONYMOUS_PARAMS = 'args'; const ANONYMOUS_PARAMS = '-args';
/** /**
* @var string the controller route specified by this request. If this is an empty string, * @var string the controller route specified by this request. If this is an empty string,

51
framework/console/controllers/HelpController.php

@ -179,17 +179,59 @@ class HelpController extends Controller
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
foreach ($actions as $action) { foreach ($actions as $action) {
if ($action === $controller->defaultAction) { if ($action === $controller->defaultAction) {
echo "* $prefix/$action (default)\n"; echo "* $prefix/$action (default)";
} else { } else {
echo "* $prefix/$action\n"; echo "* $prefix/$action";
} }
$summary = $this->getActionSummary($controller, $action);
if ($summary !== '') {
echo ': ' . $summary;
} }
echo "\n\nTo see the help of each sub-command, enter:\n"; echo "\n";
}
echo "\n\nTo see the detailed information about individual sub-commands, enter:\n";
echo "\n yiic help <sub-command>\n\n"; echo "\n yiic help <sub-command>\n\n";
} }
} }
/** /**
* Returns the short summary of the action.
* @param Controller $controller the controller instance
* @param string $actionID action ID
* @return string the summary about the action
*/
protected function getActionSummary($controller, $actionID)
{
$action = $controller->createAction($actionID);
if ($action === null) {
return '';
}
if ($action instanceof InlineAction) {
$reflection = new \ReflectionMethod($controller, $action->actionMethod);
} else {
$reflection = new \ReflectionClass($action);
}
$tags = $this->parseComment($reflection->getDocComment());
if ($tags['description'] !== '') {
$limit = 73 - strlen($action->getUniqueId());
if ($actionID === $controller->defaultAction) {
$limit -= 10;
}
if ($limit < 0) {
$limit = 50;
}
$description = $tags['description'];
if (($pos = strpos($tags['description'], "\n")) !== false) {
$description = substr($description, 0, $pos);
}
$text = substr($description, 0, $limit);
return strlen($description) > $limit ? $text . '...' : $text;
} else {
return '';
}
}
/**
* Displays the detailed information of a command action. * Displays the detailed information of a command action.
* @param Controller $controller the controller instance * @param Controller $controller the controller instance
* @param string $actionID action ID * @param string $actionID action ID
@ -200,7 +242,7 @@ class HelpController extends Controller
$action = $controller->createAction($actionID); $action = $controller->createAction($actionID);
if ($action === null) { if ($action === null) {
throw new Exception(Yii::t('yii', 'No help for unknown sub-command "{command}".', array( throw new Exception(Yii::t('yii', 'No help for unknown sub-command "{command}".', array(
'{command}' => $action->getUniqueId(), '{command}' => rtrim($controller->getUniqueId() . '/' . $actionID, '/'),
))); )));
} }
if ($action instanceof InlineAction) { if ($action instanceof InlineAction) {
@ -232,7 +274,6 @@ class HelpController extends Controller
echo "\n\n"; echo "\n\n";
if (!empty($required) || !empty($optional)) { if (!empty($required) || !empty($optional)) {
echo "\nARGUMENTS\n\n";
echo implode("\n\n", array_merge($required, $optional)) . "\n\n"; echo implode("\n\n", array_merge($required, $optional)) . "\n\n";
} }

438
framework/console/controllers/MigrateController.php

@ -14,45 +14,43 @@ use Yii;
use yii\console\Exception; use yii\console\Exception;
use yii\console\Controller; use yii\console\Controller;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query;
use yii\util\ArrayHelper;
/** /**
* This command provides support for database migrations. * This command manages application migrations.
* *
* EXAMPLES * A migration means a set of persistent changes to the application environment
* that is shared among different developers. For example, in an application
* backed by a database, a migration may refer to a set of changes to the database,
* such as creating a new table, adding a new table column.
* *
* - yiic migrate * This command provides support for tracking the migration history, upgrading
* Applies ALL new migrations. This is equivalent to 'yiic migrate up'. * or downloading with migrations, and creating new migration skeletons.
* *
* - yiic migrate create create_user_table * The migration history is stored in a database table named as [[migrationTable]].
* Creates a new migration named 'create_user_table'. * The table will be automatically created the first this command is executed.
* You may also manually create it with the following structure:
* *
* - yiic migrate up 3 * ~~~
* Applies the next 3 new migrations. * CREATE TABLE tbl_migration (
* * version varchar(255) PRIMARY KEY,
* - yiic migrate down * apply_time integer
* Reverts the last applied migration. * )
* * ~~~
* - yiic migrate down 3
* Reverts the last 3 applied migrations.
*
* - yiic migrate to 101129_185401
* Migrates up or down to version 101129_185401.
*
* - yiic migrate mark 101129_185401
* Modifies the migration history up or down to version 101129_185401.
* No actual migration will be performed.
* *
* - yiic migrate history * Below are some common usages of this command:
* Shows all previously applied migration information.
* *
* - yiic migrate history 10 * ~~~
* Shows the last 10 applied migrations. * # creates a new migration named 'create_user_table'
* yiic migrate/create create_user_table
* *
* - yiic migrate new * # applies ALL new migrations
* Shows all new migrations. * yiic migrate
* *
* - yiic migrate new 10 * # reverts the last applied migration
* Shows the next 10 migrations that have not been applied. * yiic migrate/down
* ~~~
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
@ -65,21 +63,16 @@ class MigrateController extends Controller
const BASE_MIGRATION = 'm000000_000000_base'; const BASE_MIGRATION = 'm000000_000000_base';
/** /**
* @var string the directory that stores the migrations. This can be either a path alias * @var string the default command action.
* or a directory. Defaults to '@application/migrations'. */
public $defaultAction = 'up';
/**
* @var string the directory storing the migration classes. This can be either
* a path alias or a directory.
*/ */
public $migrationPath = '@application/migrations'; public $migrationPath = '@application/migrations';
/** /**
* @var string the name of the table for keeping applied migration information. * @var string the name of the table for keeping applied migration information.
* This table will be automatically created if not exists.
* The table structure is as follows:
*
* ~~~
* CREATE TABLE tbl_migration (
* version varchar(255) PRIMARY KEY,
* apply_time integer
* )
* ~~~
*/ */
public $migrationTable = 'tbl_migration'; public $migrationTable = 'tbl_migration';
/** /**
@ -88,24 +81,30 @@ class MigrateController extends Controller
*/ */
public $connectionID = 'db'; public $connectionID = 'db';
/** /**
* @var string the path of the template file for generating new migrations. * @var string the template file for generating new migrations.
* This can be either a path alias (e.g. "@application/migrations/template.php") * This can be either a path alias (e.g. "@application/migrations/template.php")
* or a file path. If not set, an internal template will be used. * or a file path.
*/ */
public $templateFile; public $templateFile = '@yii/views/migration.php';
/**
* @var string the default command action.
*/
public $defaultAction = 'up';
/** /**
* @var boolean whether to execute the migration in an interactive mode. * @var boolean whether to execute the migration in an interactive mode.
*/ */
public $interactive = true; public $interactive = true;
/**
* @var Connection the DB connection used for storing migration history.
* @see connectionID
*/
public $db;
/**
* Returns the names of the global options for this command.
* @return array the names of the global options for this command.
*/
public function globalOptions() public function globalOptions()
{ {
return array('migrationPath', 'migrationTable', 'connectionID', 'templateFile', 'interactive'); return array('migrationPath', 'migrationTable', 'connectionID', 'templateFile', 'interactive');
} }
/** /**
* This method is invoked right before an action is to be executed (after all possible filters.) * This method is invoked right before an action is to be executed (after all possible filters.)
* It checks the existence of the [[migrationPath]]. * It checks the existence of the [[migrationPath]].
@ -121,8 +120,14 @@ class MigrateController extends Controller
throw new Exception("The migration directory \"{$this->migrationPath}\" does not exist."); throw new Exception("The migration directory \"{$this->migrationPath}\" does not exist.");
} }
$this->migrationPath = $path; $this->migrationPath = $path;
$this->db = Yii::$application->getComponent($this->connectionID);
if (!$this->db instanceof Connection) {
throw new Exception("Invalid DB connection \"{$this->connectionID}\".");
}
$version = Yii::getVersion(); $version = Yii::getVersion();
echo "\nYii Migration Tool (based on Yii v{$version})\n\n"; echo "Yii Migration Tool (based on Yii v{$version})\n\n";
return true; return true;
} else { } else {
return false; return false;
@ -130,17 +135,18 @@ class MigrateController extends Controller
} }
/** /**
* Upgrades the application by applying new migrations. For example, * Upgrades the application by applying new migrations.
* For example,
* *
* ~~~ * ~~~
* yiic migrate # apply all new migrations * yiic migrate # apply all new migrations
* yiic migrate 3 # apply the first 3 new migrations * yiic migrate 3 # apply the first 3 new migrations
* ~~~ * ~~~
* *
* @param array $args the number of new migrations to be applied. If not provided, * @param integer $limit the number of new migrations to be applied. If 0, it means
* all new migrations will be applied. * applying all available new migrations.
*/ */
public function actionUp($args) public function actionUp($limit = 0)
{ {
if (($migrations = $this->getNewMigrations()) === array()) { if (($migrations = $this->getNewMigrations()) === array()) {
echo "No new migration found. Your system is up-to-date.\n"; echo "No new migration found. Your system is up-to-date.\n";
@ -148,9 +154,9 @@ class MigrateController extends Controller
} }
$total = count($migrations); $total = count($migrations);
$step = isset($args[0]) ? (int)$args[0] : 0; $limit = (int)$limit;
if ($step > 0) { if ($limit > 0) {
$migrations = array_slice($migrations, 0, $step); $migrations = array_slice($migrations, 0, $limit);
} }
$n = count($migrations); $n = count($migrations);
@ -167,8 +173,8 @@ class MigrateController extends Controller
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) { foreach ($migrations as $migration) {
if ($this->migrateUp($migration) === false) { if (!$this->migrateUp($migration)) {
echo "\nMigration failed. The rest of the new migrations are canceled.\n"; echo "\nMigration failed. The rest of the migrations are canceled.\n";
return; return;
} }
} }
@ -177,25 +183,26 @@ class MigrateController extends Controller
} }
/** /**
* Downgrades the application by reverting old migrations. For example, * Downgrades the application by reverting old migrations.
* For example,
* *
* ~~~ * ~~~
* yiic migrate/down # revert the last migration * yiic migrate/down # revert the last migration
* yiic migrate/down 3 # revert the last 3 migrations * yiic migrate/down 3 # revert the last 3 migrations
* ~~~ * ~~~
* *
* @param array $args the number of migrations to be reverted. If not provided, * @param integer $limit the number of migrations to be reverted. Defaults to 1,
* the last applied migration will be reverted. * meaning the last applied migration will be reverted.
* @throws Exception if the number of the steps is less than 1. * @throws Exception if the number of the steps specified is less than 1.
*/ */
public function actionDown($args) public function actionDown($limit = 1)
{ {
$step = isset($args[0]) ? (int)$args[0] : 1; $limit = (int)$limit;
if ($step < 1) { if ($limit < 1) {
throw new Exception("The step parameter must be greater than 0."); throw new Exception("The step argument must be greater than 0.");
} }
if (($migrations = $this->getMigrationHistory($step)) === array()) { if (($migrations = $this->getMigrationHistory($limit)) === array()) {
echo "No migration has been done before.\n"; echo "No migration has been done before.\n";
return; return;
} }
@ -210,8 +217,8 @@ class MigrateController extends Controller
if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) { foreach ($migrations as $migration) {
if ($this->migrateDown($migration) === false) { if (!$this->migrateDown($migration)) {
echo "\nMigration failed. All later migrations are canceled.\n"; echo "\nMigration failed. The rest of the migrations are canceled.\n";
return; return;
} }
} }
@ -219,14 +226,29 @@ class MigrateController extends Controller
} }
} }
public function actionRedo($args) /**
* Redoes the last few migrations.
*
* This command will first revert the specified migrations, and then apply
* them again. For example,
*
* ~~~
* yiic migrate/redo # redo the last applied migration
* yiic migrate/redo 3 # redo the last 3 applied migrations
* ~~~
*
* @param integer $limit the number of migrations to be redone. Defaults to 1,
* meaning the last applied migration will be redone.
* @throws Exception if the number of the steps specified is less than 1.
*/
public function actionRedo($limit = 1)
{ {
$step = isset($args[0]) ? (int)$args[0] : 1; $limit = (int)$limit;
if ($step < 1) { if ($limit < 1) {
die("Error: The step parameter must be greater than 0.\n"); throw new Exception("The step argument must be greater than 0.");
} }
if (($migrations = $this->getMigrationHistory($step)) === array()) { if (($migrations = $this->getMigrationHistory($limit)) === array()) {
echo "No migration has been done before.\n"; echo "No migration has been done before.\n";
return; return;
} }
@ -241,14 +263,14 @@ class MigrateController extends Controller
if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) { foreach ($migrations as $migration) {
if ($this->migrateDown($migration) === false) { if (!$this->migrateDown($migration)) {
echo "\nMigration failed. All later migrations are canceled.\n"; echo "\nMigration failed. The rest of the migrations are canceled.\n";
return; return;
} }
} }
foreach (array_reverse($migrations) as $migration) { foreach (array_reverse($migrations) as $migration) {
if ($this->migrateUp($migration) === false) { if (!$this->migrateUp($migration)) {
echo "\nMigration failed. All later migrations are canceled.\n"; echo "\nMigration failed. The rest of the migrations migrations are canceled.\n";
return; return;
} }
} }
@ -256,26 +278,35 @@ class MigrateController extends Controller
} }
} }
public function actionTo($args) /**
* Upgrades or downgrades till the specified version of migration.
*
* This command will first revert the specified migrations, and then apply
* them again. For example,
*
* ~~~
* yiic migrate/to 101129_185401 # using timestamp
* yiic migrate/to m101129_185401_create_user_table # using full name
* ~~~
*
* @param string $version the version name that the application should be migrated to.
* This can be either the timestamp or the full name of the migration.
* @throws Exception if the version argument is invalid
*/
public function actionTo($version)
{ {
if (isset($args[0])) {
$version = $args[0];
} else {
throw new Exception("Please specify which version to migrate to.");
}
$originalVersion = $version; $originalVersion = $version;
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) { if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$version = 'm' . $matches[1]; $version = 'm' . $matches[1];
} else { } else {
throw new Exception("The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table)."); throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} }
// try migrate up // try migrate up
$migrations = $this->getNewMigrations(); $migrations = $this->getNewMigrations();
foreach ($migrations as $i => $migration) { foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) { if (strpos($migration, $version . '_') === 0) {
$this->actionUp(array($i + 1)); $this->actionUp($i + 1);
return; return;
} }
} }
@ -287,7 +318,7 @@ class MigrateController extends Controller
if ($i === 0) { if ($i === 0) {
echo "Already at '$originalVersion'. Nothing needs to be done.\n"; echo "Already at '$originalVersion'. Nothing needs to be done.\n";
} else { } else {
$this->actionDown(array($i)); $this->actionDown($i);
} }
return; return;
} }
@ -296,33 +327,40 @@ class MigrateController extends Controller
throw new Exception("Unable to find the version '$originalVersion'."); throw new Exception("Unable to find the version '$originalVersion'.");
} }
public function actionMark($args) /**
* Modifies the migration history to the specified version.
*
* No actual migration will be performed.
*
* ~~~
* yiic migrate/mark 101129_185401 # using timestamp
* yiic migrate/mark m101129_185401_create_user_table # using full name
* ~~~
*
* @param string $version the version at which the migration history should be marked.
* This can be either the timestamp or the full name of the migration.
* @throws Exception if the version argument is invalid or the version cannot be found.
*/
public function actionMark($version)
{ {
if (isset($args[0])) {
$version = $args[0];
} else {
throw new Exception('Please specify which version to mark to.');
}
$originalVersion = $version; $originalVersion = $version;
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) { if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$version = 'm' . $matches[1]; $version = 'm' . $matches[1];
} else { } else {
throw new Exception("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table)."); throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} }
$db = $this->getDb();
// try mark up // try mark up
$migrations = $this->getNewMigrations(); $migrations = $this->getNewMigrations();
foreach ($migrations as $i => $migration) { foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) { if (strpos($migration, $version . '_') === 0) {
if ($this->confirm("Set migration history at $originalVersion?")) { if ($this->confirm("Set migration history at $originalVersion?")) {
$command = $db->createCommand(); $command = $this->db->createCommand();
for ($j = 0; $j <= $i; ++$j) { for ($j = 0; $j <= $i; ++$j) {
$command->insert($this->migrationTable, array( $command->insert($this->migrationTable, array(
'version' => $migrations[$j], 'version' => $migrations[$j],
'apply_time' => time(), 'apply_time' => time(),
)); ))->execute();
} }
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n"; echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
} }
@ -338,9 +376,11 @@ class MigrateController extends Controller
echo "Already at '$originalVersion'. Nothing needs to be done.\n"; echo "Already at '$originalVersion'. Nothing needs to be done.\n";
} else { } else {
if ($this->confirm("Set migration history at $originalVersion?")) { if ($this->confirm("Set migration history at $originalVersion?")) {
$command = $db->createCommand(); $command = $this->db->createCommand();
for ($j = 0; $j < $i; ++$j) { for ($j = 0; $j < $i; ++$j) {
$command->delete($this->migrationTable, $db->quoteColumnName('version') . '=:version', array(':version' => $migrations[$j])); $command->delete($this->migrationTable, array(
'version' => $migrations[$j],
))->execute();
} }
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n"; echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
} }
@ -349,12 +389,27 @@ class MigrateController extends Controller
} }
} }
die("Error: Unable to find the version '$originalVersion'.\n"); throw new Exception("Unable to find the version '$originalVersion'.");
} }
public function actionHistory($args) /**
* Displays the migration history.
*
* This command will show the list of migrations that have been applied
* so far. For example,
*
* ~~~
* yiic migrate/history # showing the last 10 migrations
* yiic migrate/history 5 # showing the last 5 migrations
* yiic migrate/history 0 # showing the whole history
* ~~~
*
* @param integer $limit the maximum number of migrations to be displayed.
* If it is 0, the whole migration history will be displayed.
*/
public function actionHistory($limit = 10)
{ {
$limit = isset($args[0]) ? (int)$args[0] : -1; $limit = (int)$limit;
$migrations = $this->getMigrationHistory($limit); $migrations = $this->getMigrationHistory($limit);
if ($migrations === array()) { if ($migrations === array()) {
echo "No migration has been done before.\n"; echo "No migration has been done before.\n";
@ -371,9 +426,24 @@ class MigrateController extends Controller
} }
} }
public function actionNew($args) /**
* Displays the un-applied new migrations.
*
* This command will show the new migrations that have not been applied.
* For example,
*
* ~~~
* yiic migrate/new # showing the first 10 new migrations
* yiic migrate/new 5 # showing the first 5 new migrations
* yiic migrate/new 0 # showing all new migrations
* ~~~
*
* @param integer $limit the maximum number of new migrations to be displayed.
* If it is 0, all available new migrations will be displayed.
*/
public function actionNew($limit = 10)
{ {
$limit = isset($args[0]) ? (int)$args[0] : -1; $limit = (int)$limit;
$migrations = $this->getNewMigrations(); $migrations = $this->getNewMigrations();
if ($migrations === array()) { if ($migrations === array()) {
echo "No new migrations found. Your system is up-to-date.\n"; echo "No new migrations found. Your system is up-to-date.\n";
@ -394,47 +464,59 @@ class MigrateController extends Controller
/** /**
* Creates a new migration. * Creates a new migration.
* @param array $args the name of the new migration. *
* @throws Exception if the name of the new migration is not provided * This command creates a new migration using the available migration template.
* After using this command, developers should modify the created migration
* skeleton by filling up the actual migration logic.
*
* ~~~
* yiic migrate/create create_user_table
* ~~~
*
* @param string $name the name of the new migration. This should only contain
* letters, digits and/or underscores.
* @throws Exception if the name argument is invalid.
*/ */
public function actionCreate($args) public function actionCreate($name)
{ {
if (isset($args[0])) {
$name = $args[0];
} else {
throw new Exception('Please provide the name of the new migration.');
}
if (!preg_match('/^\w+$/', $name)) { if (!preg_match('/^\w+$/', $name)) {
die("Error: The name of the migration must contain letters, digits and/or underscore characters only.\n"); throw new Exception("The migration name should contain letters, digits and/or underscore characters only.");
} }
$name = 'm' . gmdate('ymd_His') . '_' . $name; $name = 'm' . gmdate('ymd_His') . '_' . $name;
$content = strtr($this->getTemplate(), array('{ClassName}' => $name));
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $name . '.php'; $file = $this->migrationPath . DIRECTORY_SEPARATOR . $name . '.php';
if ($this->confirm("Create new migration '$file'?")) { if ($this->confirm("Create new migration '$file'?")) {
$content = $this->renderFile(Yii::getAlias($this->templateFile), array(
'className' => $name,
));
file_put_contents($file, $content); file_put_contents($file, $content);
echo "New migration created successfully.\n"; echo "New migration created successfully.\n";
} }
} }
/**
* Upgrades with the specified migration class.
* @param string $class the migration class name
* @return boolean whether the migration is successful
*/
protected function migrateUp($class) protected function migrateUp($class)
{ {
if ($class === self::BASE_MIGRATION) { if ($class === self::BASE_MIGRATION) {
return; return true;
} }
echo "*** applying $class\n"; echo "*** applying $class\n";
$start = microtime(true); $start = microtime(true);
$migration = $this->instantiateMigration($class); $migration = $this->createMigration($class);
if ($migration->up() !== false) { if ($migration->up() !== false) {
$this->getDb()->createCommand()->insert($this->migrationTable, array( $this->db->createCommand()->insert($this->migrationTable, array(
'version' => $class, 'version' => $class,
'apply_time' => time(), 'apply_time' => time(),
)); ))->execute();
$time = microtime(true) - $start; $time = microtime(true) - $start;
echo "*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; echo "*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
return true;
} else { } else {
$time = microtime(true) - $start; $time = microtime(true) - $start;
echo "*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; echo "*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
@ -442,20 +524,27 @@ class MigrateController extends Controller
} }
} }
/**
* Downgrades with the specified migration class.
* @param string $class the migration class name
* @return boolean whether the migration is successful
*/
protected function migrateDown($class) protected function migrateDown($class)
{ {
if ($class === self::BASE_MIGRATION) { if ($class === self::BASE_MIGRATION) {
return; return true;
} }
echo "*** reverting $class\n"; echo "*** reverting $class\n";
$start = microtime(true); $start = microtime(true);
$migration = $this->instantiateMigration($class); $migration = $this->createMigration($class);
if ($migration->down() !== false) { if ($migration->down() !== false) {
$db = $this->getDb(); $this->db->createCommand()->delete($this->migrationTable, array(
$db->createCommand()->delete($this->migrationTable, $db->quoteColumnName('version') . '=:version', array(':version' => $class)); 'version' => $class,
))->execute();
$time = microtime(true) - $start; $time = microtime(true) - $start;
echo "*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; echo "*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
return true;
} else { } else {
$time = microtime(true) - $start; $time = microtime(true) - $start;
echo "*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; echo "*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
@ -463,62 +552,82 @@ class MigrateController extends Controller
} }
} }
protected function instantiateMigration($class) /**
* Creates a new migration instance.
* @param string $class the migration class name
* @return \yii\db\Migration the migration instance
*/
protected function createMigration($class)
{ {
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php'; $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php';
require_once($file); require_once($file);
$migration = new $class; return new $class(array(
$migration->db = $this->getDb(); 'db' => $this->db,
return $migration; ));
} }
/** /**
* @var CDbConnection * @return Connection the database connection that is used to store the migration history.
* @throws Exception if the database connection ID is invalid.
*/ */
private $_db;
protected function getDb() protected function getDb()
{ {
if ($this->_db !== null) { if ($this->db !== null) {
return $this->_db; return $this->db;
} else { } else {
if (($this->_db = Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) { $this->db = Yii::$application->getComponent($this->connectionID);
return $this->_db; if ($this->db instanceof Connection) {
return $this->db;
} else { } else {
throw new Exception("Invalid DB connection: {$this->connectionID}."); throw new Exception("Invalid DB connection: {$this->connectionID}.");
} }
} }
} }
/**
* Returns the migration history.
* @param integer $limit the maximum number of records in the history to be returned
* @return array the migration history
*/
protected function getMigrationHistory($limit) protected function getMigrationHistory($limit)
{ {
$db = $this->getDb(); if ($this->db->schema->getTableSchema($this->migrationTable) === null) {
if ($db->schema->getTable($this->migrationTable) === null) {
$this->createMigrationHistoryTable(); $this->createMigrationHistoryTable();
} }
return CHtml::listData($db->createCommand() $query = new Query;
->select('version, apply_time') $rows = $query->select(array('version', 'apply_time'))
->from($this->migrationTable) ->from($this->migrationTable)
->order('version DESC') ->orderBy('version DESC')
->limit($limit) ->limit($limit)
->queryAll(), 'version', 'apply_time'); ->createCommand()
->queryAll();
$history = ArrayHelper::map($rows, 'version', 'apply_time');
unset($history[self::BASE_MIGRATION]);
return $history;
} }
/**
* Creates the migration history table.
*/
protected function createMigrationHistoryTable() protected function createMigrationHistoryTable()
{ {
$db = $this->getDb();
echo 'Creating migration history table "' . $this->migrationTable . '"...'; echo 'Creating migration history table "' . $this->migrationTable . '"...';
$db->createCommand()->createTable($this->migrationTable, array( $this->db->createCommand()->createTable($this->migrationTable, array(
'version' => 'string NOT NULL PRIMARY KEY', 'version' => 'varchar(255) NOT NULL PRIMARY KEY',
'apply_time' => 'integer', 'apply_time' => 'integer',
)); ))->execute();
$db->createCommand()->insert($this->migrationTable, array( $this->db->createCommand()->insert($this->migrationTable, array(
'version' => self::BASE_MIGRATION, 'version' => self::BASE_MIGRATION,
'apply_time' => time(), 'apply_time' => time(),
)); ))->execute();
echo "done.\n"; echo "done.\n";
} }
/**
* Returns the migrations that are not applied.
* @return array list of new migrations
*/
protected function getNewMigrations() protected function getNewMigrations()
{ {
$applied = array(); $applied = array();
@ -541,39 +650,4 @@ class MigrateController extends Controller
sort($migrations); sort($migrations);
return $migrations; return $migrations;
} }
protected function getTemplate()
{
if ($this->templateFile !== null) {
return file_get_contents(Yii::getAlias($this->templateFile));
} else {
return <<<EOD
<?php
class {ClassName} extends CDbMigration
{
public function up()
{
}
public function down()
{
echo "{ClassName} does not support migration down.\\n";
return false;
}
/*
// Use safeUp/safeDown to do migration with transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
EOD;
}
}
} }

23
framework/views/migration.php

@ -0,0 +1,23 @@
<?php
/**
* This view is used by console/controllers/MigrateController.php
* The following variables are available in this view:
*
* @var string $className the new migration class name
*/
echo "<?php\n";
?>
class <?php echo $className; ?> extends \yii\db\Migration
{
public function up()
{
}
public function down()
{
echo "<?php echo $className; ?> cannot be reverted.\n";
return false;
}
}
Loading…
Cancel
Save