MigrateController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. * ```sql
  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. * Since 2.0.10 you can use namespaced migrations. In order to enable this feature you should configure [[migrationNamespaces]]
  51. * property for the controller at application configuration:
  52. *
  53. * ```php
  54. * return [
  55. * 'controllerMap' => [
  56. * 'migrate' => [
  57. * 'class' => 'yii\console\controllers\MigrateController',
  58. * 'migrationNamespaces' => [
  59. * 'app\migrations',
  60. * 'some\extension\migrations',
  61. * ],
  62. * //'migrationPath' => null, // allows to disable not namespaced migration completely
  63. * ],
  64. * ],
  65. * ];
  66. * ```
  67. *
  68. * @author Qiang Xue <qiang.xue@gmail.com>
  69. * @since 2.0
  70. */
  71. class MigrateController extends BaseMigrateController
  72. {
  73. /**
  74. * Maximum length of a migration name.
  75. * @since 2.0.13
  76. */
  77. const MAX_NAME_LENGTH = 180;
  78. /**
  79. * @var string the name of the table for keeping applied migration information.
  80. */
  81. public $migrationTable = '{{%migration}}';
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public $templateFile = '@yii/views/migration.php';
  86. /**
  87. * @var array a set of template paths for generating migration code automatically.
  88. *
  89. * The key is the template type, the value is a path or the alias. Supported types are:
  90. * - `create_table`: table creating template
  91. * - `drop_table`: table dropping template
  92. * - `add_column`: adding new column template
  93. * - `drop_column`: dropping column template
  94. * - `create_junction`: create junction template
  95. *
  96. * @since 2.0.7
  97. */
  98. public $generatorTemplateFiles = [
  99. 'create_table' => '@yii/views/createTableMigration.php',
  100. 'drop_table' => '@yii/views/dropTableMigration.php',
  101. 'add_column' => '@yii/views/addColumnMigration.php',
  102. 'drop_column' => '@yii/views/dropColumnMigration.php',
  103. 'create_junction' => '@yii/views/createTableMigration.php',
  104. ];
  105. /**
  106. * @var bool indicates whether the table names generated should consider
  107. * the `tablePrefix` setting of the DB connection. For example, if the table
  108. * name is `post` the generator wil return `{{%post}}`.
  109. * @since 2.0.8
  110. */
  111. public $useTablePrefix = true;
  112. /**
  113. * @var array column definition strings used for creating migration code.
  114. *
  115. * The format of each definition is `COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR`. Delimiter is `,`.
  116. * For example, `--fields="name:string(12):notNull:unique"`
  117. * produces a string column of size 12 which is not null and unique values.
  118. *
  119. * Note: primary key is added automatically and is named id by default.
  120. * If you want to use another name you may specify it explicitly like
  121. * `--fields="id_key:primaryKey,name:string(12):notNull:unique"`
  122. * @since 2.0.7
  123. */
  124. public $fields = [];
  125. /**
  126. * @var Connection|array|string the DB connection object or the application component ID of the DB connection to use
  127. * when applying migrations. Starting from version 2.0.3, this can also be a configuration array
  128. * for creating the object.
  129. */
  130. public $db = 'db';
  131. /**
  132. * @var string the comment for the table being created.
  133. * @since 2.0.14
  134. */
  135. public $comment = '';
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function options($actionID)
  140. {
  141. return array_merge(
  142. parent::options($actionID),
  143. ['migrationTable', 'db'], // global for all actions
  144. $actionID === 'create'
  145. ? ['templateFile', 'fields', 'useTablePrefix', 'comment']
  146. : []
  147. );
  148. }
  149. /**
  150. * {@inheritdoc}
  151. * @since 2.0.8
  152. */
  153. public function optionAliases()
  154. {
  155. return array_merge(parent::optionAliases(), [
  156. 'C' => 'comment',
  157. 'f' => 'fields',
  158. 'p' => 'migrationPath',
  159. 't' => 'migrationTable',
  160. 'F' => 'templateFile',
  161. 'P' => 'useTablePrefix',
  162. 'c' => 'compact',
  163. ]);
  164. }
  165. /**
  166. * This method is invoked right before an action is to be executed (after all possible filters.)
  167. * It checks the existence of the [[migrationPath]].
  168. * @param \yii\base\Action $action the action to be executed.
  169. * @return bool whether the action should continue to be executed.
  170. */
  171. public function beforeAction($action)
  172. {
  173. if (parent::beforeAction($action)) {
  174. $this->db = Instance::ensure($this->db, Connection::className());
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * Creates a new migration instance.
  181. * @param string $class the migration class name
  182. * @return \yii\db\Migration the migration instance
  183. */
  184. protected function createMigration($class)
  185. {
  186. $this->includeMigrationFile($class);
  187. return Yii::createObject([
  188. 'class' => $class,
  189. 'db' => $this->db,
  190. 'compact' => $this->compact,
  191. ]);
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. protected function getMigrationHistory($limit)
  197. {
  198. if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
  199. $this->createMigrationHistoryTable();
  200. }
  201. $query = (new Query())
  202. ->select(['version', 'apply_time'])
  203. ->from($this->migrationTable)
  204. ->orderBy(['apply_time' => SORT_DESC, 'version' => SORT_DESC]);
  205. if (empty($this->migrationNamespaces)) {
  206. $query->limit($limit);
  207. $rows = $query->all($this->db);
  208. $history = ArrayHelper::map($rows, 'version', 'apply_time');
  209. unset($history[self::BASE_MIGRATION]);
  210. return $history;
  211. }
  212. $rows = $query->all($this->db);
  213. $history = [];
  214. foreach ($rows as $key => $row) {
  215. if ($row['version'] === self::BASE_MIGRATION) {
  216. continue;
  217. }
  218. if (preg_match('/m?(\d{6}_?\d{6})(\D.*)?$/is', $row['version'], $matches)) {
  219. $time = str_replace('_', '', $matches[1]);
  220. $row['canonicalVersion'] = $time;
  221. } else {
  222. $row['canonicalVersion'] = $row['version'];
  223. }
  224. $row['apply_time'] = (int) $row['apply_time'];
  225. $history[] = $row;
  226. }
  227. usort($history, function ($a, $b) {
  228. if ($a['apply_time'] === $b['apply_time']) {
  229. if (($compareResult = strcasecmp($b['canonicalVersion'], $a['canonicalVersion'])) !== 0) {
  230. return $compareResult;
  231. }
  232. return strcasecmp($b['version'], $a['version']);
  233. }
  234. return ($a['apply_time'] > $b['apply_time']) ? -1 : +1;
  235. });
  236. $history = array_slice($history, 0, $limit);
  237. $history = ArrayHelper::map($history, 'version', 'apply_time');
  238. return $history;
  239. }
  240. /**
  241. * Creates the migration history table.
  242. */
  243. protected function createMigrationHistoryTable()
  244. {
  245. $tableName = $this->db->schema->getRawTableName($this->migrationTable);
  246. $this->stdout("Creating migration history table \"$tableName\"...", Console::FG_YELLOW);
  247. $this->db->createCommand()->createTable($this->migrationTable, [
  248. 'version' => 'varchar(' . static::MAX_NAME_LENGTH . ') NOT NULL PRIMARY KEY',
  249. 'apply_time' => 'integer',
  250. ])->execute();
  251. $this->db->createCommand()->insert($this->migrationTable, [
  252. 'version' => self::BASE_MIGRATION,
  253. 'apply_time' => time(),
  254. ])->execute();
  255. $this->stdout("Done.\n", Console::FG_GREEN);
  256. }
  257. /**
  258. * {@inheritdoc}
  259. */
  260. protected function addMigrationHistory($version)
  261. {
  262. $command = $this->db->createCommand();
  263. $command->insert($this->migrationTable, [
  264. 'version' => $version,
  265. 'apply_time' => time(),
  266. ])->execute();
  267. }
  268. /**
  269. * {@inheritdoc}
  270. * @since 2.0.13
  271. */
  272. protected function truncateDatabase()
  273. {
  274. $db = $this->db;
  275. $schemas = $db->schema->getTableSchemas();
  276. // First drop all foreign keys,
  277. foreach ($schemas as $schema) {
  278. if ($schema->foreignKeys) {
  279. foreach ($schema->foreignKeys as $name => $foreignKey) {
  280. $db->createCommand()->dropForeignKey($name, $schema->name)->execute();
  281. $this->stdout("Foreign key $name dropped.\n");
  282. }
  283. }
  284. }
  285. // Then drop the tables:
  286. foreach ($schemas as $schema) {
  287. try {
  288. $db->createCommand()->dropTable($schema->name)->execute();
  289. $this->stdout("Table {$schema->name} dropped.\n");
  290. } catch (\Exception $e) {
  291. if (strpos($e->getMessage(), 'DROP VIEW to delete view') !== false) {
  292. $db->createCommand()->dropView($schema->name)->execute();
  293. $this->stdout("View {$schema->name} dropped.\n");
  294. } else {
  295. $this->stdout("Cannot drop {$schema->name} Table .\n");
  296. }
  297. }
  298. }
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. protected function removeMigrationHistory($version)
  304. {
  305. $command = $this->db->createCommand();
  306. $command->delete($this->migrationTable, [
  307. 'version' => $version,
  308. ])->execute();
  309. }
  310. private $_migrationNameLimit;
  311. /**
  312. * {@inheritdoc}
  313. * @since 2.0.13
  314. */
  315. protected function getMigrationNameLimit()
  316. {
  317. if ($this->_migrationNameLimit !== null) {
  318. return $this->_migrationNameLimit;
  319. }
  320. $tableSchema = $this->db->schema ? $this->db->schema->getTableSchema($this->migrationTable, true) : null;
  321. if ($tableSchema !== null) {
  322. return $this->_migrationNameLimit = $tableSchema->columns['version']->size;
  323. }
  324. return static::MAX_NAME_LENGTH;
  325. }
  326. /**
  327. * {@inheritdoc}
  328. * @since 2.0.8
  329. */
  330. protected function generateMigrationSourceCode($params)
  331. {
  332. $parsedFields = $this->parseFields();
  333. $fields = $parsedFields['fields'];
  334. $foreignKeys = $parsedFields['foreignKeys'];
  335. $name = $params['name'];
  336. $templateFile = $this->templateFile;
  337. $table = null;
  338. if (preg_match('/^create_junction(?:_table_for_|_for_|_)(.+)_and_(.+)_tables?$/', $name, $matches)) {
  339. $templateFile = $this->generatorTemplateFiles['create_junction'];
  340. $firstTable = $matches[1];
  341. $secondTable = $matches[2];
  342. $fields = array_merge(
  343. [
  344. [
  345. 'property' => $firstTable . '_id',
  346. 'decorators' => 'integer()',
  347. ],
  348. [
  349. 'property' => $secondTable . '_id',
  350. 'decorators' => 'integer()',
  351. ],
  352. ],
  353. $fields,
  354. [
  355. [
  356. 'property' => 'PRIMARY KEY(' .
  357. $firstTable . '_id, ' .
  358. $secondTable . '_id)',
  359. ],
  360. ]
  361. );
  362. $foreignKeys[$firstTable . '_id']['table'] = $firstTable;
  363. $foreignKeys[$secondTable . '_id']['table'] = $secondTable;
  364. $foreignKeys[$firstTable . '_id']['column'] = null;
  365. $foreignKeys[$secondTable . '_id']['column'] = null;
  366. $table = $firstTable . '_' . $secondTable;
  367. } elseif (preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches)) {
  368. $templateFile = $this->generatorTemplateFiles['add_column'];
  369. $table = $matches[2];
  370. } elseif (preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches)) {
  371. $templateFile = $this->generatorTemplateFiles['drop_column'];
  372. $table = $matches[2];
  373. } elseif (preg_match('/^create_(.+)_table$/', $name, $matches)) {
  374. $this->addDefaultPrimaryKey($fields);
  375. $templateFile = $this->generatorTemplateFiles['create_table'];
  376. $table = $matches[1];
  377. } elseif (preg_match('/^drop_(.+)_table$/', $name, $matches)) {
  378. $this->addDefaultPrimaryKey($fields);
  379. $templateFile = $this->generatorTemplateFiles['drop_table'];
  380. $table = $matches[1];
  381. }
  382. foreach ($foreignKeys as $column => $foreignKey) {
  383. $relatedColumn = $foreignKey['column'];
  384. $relatedTable = $foreignKey['table'];
  385. // Since 2.0.11 if related column name is not specified,
  386. // we're trying to get it from table schema
  387. // @see https://github.com/yiisoft/yii2/issues/12748
  388. if ($relatedColumn === null) {
  389. $relatedColumn = 'id';
  390. try {
  391. $this->db = Instance::ensure($this->db, Connection::className());
  392. $relatedTableSchema = $this->db->getTableSchema($relatedTable);
  393. if ($relatedTableSchema !== null) {
  394. $primaryKeyCount = count($relatedTableSchema->primaryKey);
  395. if ($primaryKeyCount === 1) {
  396. $relatedColumn = $relatedTableSchema->primaryKey[0];
  397. } elseif ($primaryKeyCount > 1) {
  398. $this->stdout("Related table for field \"{$column}\" exists, but primary key is composite. Default name \"id\" will be used for related field\n", Console::FG_YELLOW);
  399. } elseif ($primaryKeyCount === 0) {
  400. $this->stdout("Related table for field \"{$column}\" exists, but does not have a primary key. Default name \"id\" will be used for related field.\n", Console::FG_YELLOW);
  401. }
  402. }
  403. } catch (\ReflectionException $e) {
  404. $this->stdout("Cannot initialize database component to try reading referenced table schema for field \"{$column}\". Default name \"id\" will be used for related field.\n", Console::FG_YELLOW);
  405. }
  406. }
  407. $foreignKeys[$column] = [
  408. 'idx' => $this->generateTableName("idx-$table-$column"),
  409. 'fk' => $this->generateTableName("fk-$table-$column"),
  410. 'relatedTable' => $this->generateTableName($relatedTable),
  411. 'relatedColumn' => $relatedColumn,
  412. ];
  413. }
  414. return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
  415. 'table' => $this->generateTableName($table),
  416. 'fields' => $fields,
  417. 'foreignKeys' => $foreignKeys,
  418. 'tableComment' => $this->comment,
  419. ]));
  420. }
  421. /**
  422. * If `useTablePrefix` equals true, then the table name will contain the
  423. * prefix format.
  424. *
  425. * @param string $tableName the table name to generate.
  426. * @return string
  427. * @since 2.0.8
  428. */
  429. protected function generateTableName($tableName)
  430. {
  431. if (!$this->useTablePrefix) {
  432. return $tableName;
  433. }
  434. return '{{%' . $tableName . '}}';
  435. }
  436. /**
  437. * Parse the command line migration fields.
  438. * @return array parse result with following fields:
  439. *
  440. * - fields: array, parsed fields
  441. * - foreignKeys: array, detected foreign keys
  442. *
  443. * @since 2.0.7
  444. */
  445. protected function parseFields()
  446. {
  447. $fields = [];
  448. $foreignKeys = [];
  449. foreach ($this->fields as $index => $field) {
  450. $chunks = preg_split('/\s?:\s?/', $field, null);
  451. $property = array_shift($chunks);
  452. foreach ($chunks as $i => &$chunk) {
  453. if (strncmp($chunk, 'foreignKey', 10) === 0) {
  454. preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
  455. $foreignKeys[$property] = [
  456. 'table' => isset($matches[1])
  457. ? $matches[1]
  458. : preg_replace('/_id$/', '', $property),
  459. 'column' => !empty($matches[2])
  460. ? $matches[2]
  461. : null,
  462. ];
  463. unset($chunks[$i]);
  464. continue;
  465. }
  466. if (!preg_match('/^(.+?)\(([^(]+)\)$/', $chunk)) {
  467. $chunk .= '()';
  468. }
  469. }
  470. $fields[] = [
  471. 'property' => $property,
  472. 'decorators' => implode('->', $chunks),
  473. ];
  474. }
  475. return [
  476. 'fields' => $fields,
  477. 'foreignKeys' => $foreignKeys,
  478. ];
  479. }
  480. /**
  481. * Adds default primary key to fields list if there's no primary key specified.
  482. * @param array $fields parsed fields
  483. * @since 2.0.7
  484. */
  485. protected function addDefaultPrimaryKey(&$fields)
  486. {
  487. foreach ($fields as $field) {
  488. if (false !== strripos($field['decorators'], 'primarykey()')) {
  489. return;
  490. }
  491. }
  492. array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
  493. }
  494. }