Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

178 lines
5.1KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console\controllers;
  8. use Yii;
  9. use yii\db\Connection;
  10. use yii\db\Query;
  11. use yii\di\Instance;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Console;
  14. /**
  15. * Manages application migrations.
  16. *
  17. * A migration means a set of persistent changes to the application environment
  18. * that is shared among different developers. For example, in an application
  19. * backed by a database, a migration may refer to a set of changes to
  20. * the database, such as creating a new table, adding a new table column.
  21. *
  22. * This command provides support for tracking the migration history, upgrading
  23. * or downloading with migrations, and creating new migration skeletons.
  24. *
  25. * The migration history is stored in a database table named
  26. * as [[migrationTable]]. The table will be automatically created the first time
  27. * this command is executed, if it does not exist. You may also manually
  28. * create it as follows:
  29. *
  30. * ~~~
  31. * CREATE TABLE migration (
  32. * version varchar(180) PRIMARY KEY,
  33. * apply_time integer
  34. * )
  35. * ~~~
  36. *
  37. * Below are some common usages of this command:
  38. *
  39. * ~~~
  40. * # creates a new migration named 'create_user_table'
  41. * yii migrate/create create_user_table
  42. *
  43. * # applies ALL new migrations
  44. * yii migrate
  45. *
  46. * # reverts the last applied migration
  47. * yii migrate/down
  48. * ~~~
  49. *
  50. * @author Qiang Xue <qiang.xue@gmail.com>
  51. * @since 2.0
  52. */
  53. class MigrateController extends BaseMigrateController
  54. {
  55. /**
  56. * @var string the name of the table for keeping applied migration information.
  57. */
  58. public $migrationTable = '{{%migration}}';
  59. /**
  60. * @inheritdoc
  61. */
  62. public $templateFile = '@yii/views/migration.php';
  63. /**
  64. * @var Connection|array|string the DB connection object or the application component ID of the DB connection to use
  65. * when applying migrations. Starting from version 2.0.3, this can also be a configuration array
  66. * for creating the object.
  67. */
  68. public $db = 'db';
  69. /**
  70. * @inheritdoc
  71. */
  72. public function options($actionID)
  73. {
  74. return array_merge(
  75. parent::options($actionID),
  76. ['migrationTable', 'db'] // global for all actions
  77. );
  78. }
  79. /**
  80. * This method is invoked right before an action is to be executed (after all possible filters.)
  81. * It checks the existence of the [[migrationPath]].
  82. * @param \yii\base\Action $action the action to be executed.
  83. * @return boolean whether the action should continue to be executed.
  84. */
  85. public function beforeAction($action)
  86. {
  87. if (parent::beforeAction($action)) {
  88. if ($action->id !== 'create') {
  89. $this->db = Instance::ensure($this->db, Connection::className());
  90. }
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Creates a new migration instance.
  98. * @param string $class the migration class name
  99. * @return \yii\db\Migration the migration instance
  100. */
  101. protected function createMigration($class)
  102. {
  103. $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php';
  104. require_once($file);
  105. return new $class(['db' => $this->db]);
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. protected function getMigrationHistory($limit)
  111. {
  112. if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
  113. $this->createMigrationHistoryTable();
  114. }
  115. $query = new Query;
  116. $rows = $query->select(['version', 'apply_time'])
  117. ->from($this->migrationTable)
  118. ->orderBy('version DESC')
  119. ->limit($limit)
  120. ->createCommand($this->db)
  121. ->queryAll();
  122. $history = ArrayHelper::map($rows, 'version', 'apply_time');
  123. unset($history[self::BASE_MIGRATION]);
  124. return $history;
  125. }
  126. /**
  127. * Creates the migration history table.
  128. */
  129. protected function createMigrationHistoryTable()
  130. {
  131. $tableName = $this->db->schema->getRawTableName($this->migrationTable);
  132. $this->stdout("Creating migration history table \"$tableName\"...", Console::FG_YELLOW);
  133. $this->db->createCommand()->createTable($this->migrationTable, [
  134. 'version' => 'varchar(180) NOT NULL PRIMARY KEY',
  135. 'apply_time' => 'integer',
  136. ])->execute();
  137. $this->db->createCommand()->insert($this->migrationTable, [
  138. 'version' => self::BASE_MIGRATION,
  139. 'apply_time' => time(),
  140. ])->execute();
  141. $this->stdout("Done.\n", Console::FG_GREEN);
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. protected function addMigrationHistory($version)
  147. {
  148. $command = $this->db->createCommand();
  149. $command->insert($this->migrationTable, [
  150. 'version' => $version,
  151. 'apply_time' => time(),
  152. ])->execute();
  153. }
  154. /**
  155. * @inheritdoc
  156. */
  157. protected function removeMigrationHistory($version)
  158. {
  159. $command = $this->db->createCommand();
  160. $command->delete($this->migrationTable, [
  161. 'version' => $version,
  162. ])->execute();
  163. }
  164. }