MigrateController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://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. 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. // Then drop the tables:
  285. foreach ($schemas as $schema) {
  286. try {
  287. $db->createCommand()->dropTable($schema->name)->execute();
  288. $this->stdout("Table {$schema->name} dropped.\n");
  289. } catch (\Exception $e) {
  290. if ($this->isViewRelated($e->getMessage())) {
  291. $db->createCommand()->dropView($schema->name)->execute();
  292. $this->stdout("View {$schema->name} dropped.\n");
  293. } else {
  294. $this->stdout("Cannot drop {$schema->name} Table .\n");
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * Determines whether the error message is related to deleting a view or not
  301. * @param string $errorMessage
  302. * @return bool
  303. */
  304. private function isViewRelated($errorMessage)
  305. {
  306. $dropViewErrors = [
  307. 'DROP VIEW to delete view', // SQLite
  308. 'SQLSTATE[42S02]', // MySQL
  309. 'is a view. Use DROP VIEW', // Microsoft SQL Server
  310. ];
  311. foreach ($dropViewErrors as $dropViewError) {
  312. if (strpos($errorMessage, $dropViewError) !== false) {
  313. return true;
  314. }
  315. }
  316. return false;
  317. }
  318. /**
  319. * {@inheritdoc}
  320. */
  321. protected function removeMigrationHistory($version)
  322. {
  323. $command = $this->db->createCommand();
  324. $command->delete($this->migrationTable, [
  325. 'version' => $version,
  326. ])->execute();
  327. }
  328. private $_migrationNameLimit;
  329. /**
  330. * {@inheritdoc}
  331. * @since 2.0.13
  332. */
  333. protected function getMigrationNameLimit()
  334. {
  335. if ($this->_migrationNameLimit !== null) {
  336. return $this->_migrationNameLimit;
  337. }
  338. $tableSchema = $this->db->schema ? $this->db->schema->getTableSchema($this->migrationTable, true) : null;
  339. if ($tableSchema !== null) {
  340. return $this->_migrationNameLimit = $tableSchema->columns['version']->size;
  341. }
  342. return static::MAX_NAME_LENGTH;
  343. }
  344. /**
  345. * Normalizes table name for generator.
  346. * When name is preceded with underscore name case is kept - otherwise it's converted from camelcase to underscored.
  347. * Last underscore is always trimmed so if there should be underscore at the end of name use two of them.
  348. * @param string $name
  349. * @return string
  350. */
  351. private function normalizeTableName($name)
  352. {
  353. if (substr($name, -1) === '_') {
  354. $name = substr($name, 0, -1);
  355. }
  356. if (strncmp($name, '_', 1) === 0) {
  357. return substr($name, 1);
  358. }
  359. return Inflector::underscore($name);
  360. }
  361. /**
  362. * {@inheritdoc}
  363. * @since 2.0.8
  364. */
  365. protected function generateMigrationSourceCode($params)
  366. {
  367. $parsedFields = $this->parseFields();
  368. $fields = $parsedFields['fields'];
  369. $foreignKeys = $parsedFields['foreignKeys'];
  370. $name = $params['name'];
  371. if ($params['namespace']) {
  372. $name = substr($name, (strrpos($name, '\\') ?: -1) + 1);
  373. }
  374. $templateFile = $this->templateFile;
  375. $table = null;
  376. if (preg_match('/^create_?junction_?(?:table)?_?(?:for)?(.+)_?and(.+)_?tables?$/i', $name, $matches)) {
  377. $templateFile = $this->generatorTemplateFiles['create_junction'];
  378. $firstTable = $this->normalizeTableName($matches[1]);
  379. $secondTable = $this->normalizeTableName($matches[2]);
  380. $fields = array_merge(
  381. [
  382. [
  383. 'property' => $firstTable . '_id',
  384. 'decorators' => 'integer()',
  385. ],
  386. [
  387. 'property' => $secondTable . '_id',
  388. 'decorators' => 'integer()',
  389. ],
  390. ],
  391. $fields,
  392. [
  393. [
  394. 'property' => 'PRIMARY KEY(' .
  395. $firstTable . '_id, ' .
  396. $secondTable . '_id)',
  397. ],
  398. ]
  399. );
  400. $foreignKeys[$firstTable . '_id']['table'] = $firstTable;
  401. $foreignKeys[$secondTable . '_id']['table'] = $secondTable;
  402. $foreignKeys[$firstTable . '_id']['column'] = null;
  403. $foreignKeys[$secondTable . '_id']['column'] = null;
  404. $table = $firstTable . '_' . $secondTable;
  405. } elseif (preg_match('/^add(.+)columns?_?to(.+)table$/i', $name, $matches)) {
  406. $templateFile = $this->generatorTemplateFiles['add_column'];
  407. $table = $this->normalizeTableName($matches[2]);
  408. } elseif (preg_match('/^drop(.+)columns?_?from(.+)table$/i', $name, $matches)) {
  409. $templateFile = $this->generatorTemplateFiles['drop_column'];
  410. $table = $this->normalizeTableName($matches[2]);
  411. } elseif (preg_match('/^create(.+)table$/i', $name, $matches)) {
  412. $this->addDefaultPrimaryKey($fields);
  413. $templateFile = $this->generatorTemplateFiles['create_table'];
  414. $table = $this->normalizeTableName($matches[1]);
  415. } elseif (preg_match('/^drop(.+)table$/i', $name, $matches)) {
  416. $this->addDefaultPrimaryKey($fields);
  417. $templateFile = $this->generatorTemplateFiles['drop_table'];
  418. $table = $this->normalizeTableName($matches[1]);
  419. }
  420. foreach ($foreignKeys as $column => $foreignKey) {
  421. $relatedColumn = $foreignKey['column'];
  422. $relatedTable = $foreignKey['table'];
  423. // Since 2.0.11 if related column name is not specified,
  424. // we're trying to get it from table schema
  425. // @see https://github.com/yiisoft/yii2/issues/12748
  426. if ($relatedColumn === null) {
  427. $relatedColumn = 'id';
  428. try {
  429. $this->db = Instance::ensure($this->db, Connection::className());
  430. $relatedTableSchema = $this->db->getTableSchema($relatedTable);
  431. if ($relatedTableSchema !== null) {
  432. $primaryKeyCount = count($relatedTableSchema->primaryKey);
  433. if ($primaryKeyCount === 1) {
  434. $relatedColumn = $relatedTableSchema->primaryKey[0];
  435. } elseif ($primaryKeyCount > 1) {
  436. $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);
  437. } elseif ($primaryKeyCount === 0) {
  438. $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);
  439. }
  440. }
  441. } catch (\ReflectionException $e) {
  442. $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);
  443. }
  444. }
  445. $foreignKeys[$column] = [
  446. 'idx' => $this->generateTableName("idx-$table-$column"),
  447. 'fk' => $this->generateTableName("fk-$table-$column"),
  448. 'relatedTable' => $this->generateTableName($relatedTable),
  449. 'relatedColumn' => $relatedColumn,
  450. ];
  451. }
  452. return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
  453. 'table' => $this->generateTableName($table),
  454. 'fields' => $fields,
  455. 'foreignKeys' => $foreignKeys,
  456. 'tableComment' => $this->comment,
  457. ]));
  458. }
  459. /**
  460. * If `useTablePrefix` equals true, then the table name will contain the
  461. * prefix format.
  462. *
  463. * @param string $tableName the table name to generate.
  464. * @return string
  465. * @since 2.0.8
  466. */
  467. protected function generateTableName($tableName)
  468. {
  469. if (!$this->useTablePrefix) {
  470. return $tableName;
  471. }
  472. return '{{%' . $tableName . '}}';
  473. }
  474. /**
  475. * Parse the command line migration fields.
  476. * @return array parse result with following fields:
  477. *
  478. * - fields: array, parsed fields
  479. * - foreignKeys: array, detected foreign keys
  480. *
  481. * @since 2.0.7
  482. */
  483. protected function parseFields()
  484. {
  485. $fields = [];
  486. $foreignKeys = [];
  487. foreach ($this->fields as $index => $field) {
  488. $chunks = $this->splitFieldIntoChunks($field);
  489. $property = array_shift($chunks);
  490. foreach ($chunks as $i => &$chunk) {
  491. if (strncmp($chunk, 'foreignKey', 10) === 0) {
  492. preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
  493. $foreignKeys[$property] = [
  494. 'table' => isset($matches[1])
  495. ? $matches[1]
  496. : preg_replace('/_id$/', '', $property),
  497. 'column' => !empty($matches[2])
  498. ? $matches[2]
  499. : null,
  500. ];
  501. unset($chunks[$i]);
  502. continue;
  503. }
  504. if (!preg_match('/^(.+?)\(([^(]+)\)$/', $chunk)) {
  505. $chunk .= '()';
  506. }
  507. }
  508. $fields[] = [
  509. 'property' => $property,
  510. 'decorators' => implode('->', $chunks),
  511. ];
  512. }
  513. return [
  514. 'fields' => $fields,
  515. 'foreignKeys' => $foreignKeys,
  516. ];
  517. }
  518. /**
  519. * Splits field into chunks
  520. *
  521. * @param string $field
  522. * @return string[]|false
  523. */
  524. protected function splitFieldIntoChunks($field)
  525. {
  526. $originalDefaultValue = null;
  527. $defaultValue = null;
  528. preg_match_all('/defaultValue\(["\'].*?:?.*?["\']\)/', $field, $matches, PREG_SET_ORDER, 0);
  529. if (isset($matches[0][0])) {
  530. $originalDefaultValue = $matches[0][0];
  531. $defaultValue = str_replace(':', '{{colon}}', $originalDefaultValue);
  532. $field = str_replace($originalDefaultValue, $defaultValue, $field);
  533. }
  534. $chunks = preg_split('/\s?:\s?/', $field);
  535. if (is_array($chunks) && $defaultValue !== null && $originalDefaultValue !== null) {
  536. foreach ($chunks as $key => $chunk) {
  537. $chunks[$key] = str_replace($defaultValue, $originalDefaultValue, $chunk);
  538. }
  539. }
  540. return $chunks;
  541. }
  542. /**
  543. * Adds default primary key to fields list if there's no primary key specified.
  544. * @param array $fields parsed fields
  545. * @since 2.0.7
  546. */
  547. protected function addDefaultPrimaryKey(&$fields)
  548. {
  549. foreach ($fields as $field) {
  550. if ($field['property'] === 'id' || false !== strripos($field['decorators'], 'primarykey()')) {
  551. return;
  552. }
  553. }
  554. array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
  555. }
  556. }