MigrateController.php 21 KB

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