MigrateController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 ($this->isViewRelated($e->getMessage())) {
  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. * Determines whether the error message is related to deleting a view or not
  302. * @param string $errorMessage
  303. * @return bool
  304. */
  305. private function isViewRelated($errorMessage)
  306. {
  307. $dropViewErrors = [
  308. 'DROP VIEW to delete view', // SQLite
  309. 'SQLSTATE[42S02]', // MySQL
  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. * {@inheritdoc}
  346. * @since 2.0.8
  347. */
  348. protected function generateMigrationSourceCode($params)
  349. {
  350. $parsedFields = $this->parseFields();
  351. $fields = $parsedFields['fields'];
  352. $foreignKeys = $parsedFields['foreignKeys'];
  353. $name = $params['name'];
  354. $templateFile = $this->templateFile;
  355. $table = null;
  356. if (preg_match('/^create_junction(?:_table_for_|_for_|_)(.+)_and_(.+)_tables?$/', $name, $matches)) {
  357. $templateFile = $this->generatorTemplateFiles['create_junction'];
  358. $firstTable = $matches[1];
  359. $secondTable = $matches[2];
  360. $fields = array_merge(
  361. [
  362. [
  363. 'property' => $firstTable . '_id',
  364. 'decorators' => 'integer()',
  365. ],
  366. [
  367. 'property' => $secondTable . '_id',
  368. 'decorators' => 'integer()',
  369. ],
  370. ],
  371. $fields,
  372. [
  373. [
  374. 'property' => 'PRIMARY KEY(' .
  375. $firstTable . '_id, ' .
  376. $secondTable . '_id)',
  377. ],
  378. ]
  379. );
  380. $foreignKeys[$firstTable . '_id']['table'] = $firstTable;
  381. $foreignKeys[$secondTable . '_id']['table'] = $secondTable;
  382. $foreignKeys[$firstTable . '_id']['column'] = null;
  383. $foreignKeys[$secondTable . '_id']['column'] = null;
  384. $table = $firstTable . '_' . $secondTable;
  385. } elseif (preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches)) {
  386. $templateFile = $this->generatorTemplateFiles['add_column'];
  387. $table = $matches[2];
  388. } elseif (preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches)) {
  389. $templateFile = $this->generatorTemplateFiles['drop_column'];
  390. $table = $matches[2];
  391. } elseif (preg_match('/^create_(.+)_table$/', $name, $matches)) {
  392. $this->addDefaultPrimaryKey($fields);
  393. $templateFile = $this->generatorTemplateFiles['create_table'];
  394. $table = $matches[1];
  395. } elseif (preg_match('/^drop_(.+)_table$/', $name, $matches)) {
  396. $this->addDefaultPrimaryKey($fields);
  397. $templateFile = $this->generatorTemplateFiles['drop_table'];
  398. $table = $matches[1];
  399. }
  400. foreach ($foreignKeys as $column => $foreignKey) {
  401. $relatedColumn = $foreignKey['column'];
  402. $relatedTable = $foreignKey['table'];
  403. // Since 2.0.11 if related column name is not specified,
  404. // we're trying to get it from table schema
  405. // @see https://github.com/yiisoft/yii2/issues/12748
  406. if ($relatedColumn === null) {
  407. $relatedColumn = 'id';
  408. try {
  409. $this->db = Instance::ensure($this->db, Connection::className());
  410. $relatedTableSchema = $this->db->getTableSchema($relatedTable);
  411. if ($relatedTableSchema !== null) {
  412. $primaryKeyCount = count($relatedTableSchema->primaryKey);
  413. if ($primaryKeyCount === 1) {
  414. $relatedColumn = $relatedTableSchema->primaryKey[0];
  415. } elseif ($primaryKeyCount > 1) {
  416. $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);
  417. } elseif ($primaryKeyCount === 0) {
  418. $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);
  419. }
  420. }
  421. } catch (\ReflectionException $e) {
  422. $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);
  423. }
  424. }
  425. $foreignKeys[$column] = [
  426. 'idx' => $this->generateTableName("idx-$table-$column"),
  427. 'fk' => $this->generateTableName("fk-$table-$column"),
  428. 'relatedTable' => $this->generateTableName($relatedTable),
  429. 'relatedColumn' => $relatedColumn,
  430. ];
  431. }
  432. return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
  433. 'table' => $this->generateTableName($table),
  434. 'fields' => $fields,
  435. 'foreignKeys' => $foreignKeys,
  436. 'tableComment' => $this->comment,
  437. ]));
  438. }
  439. /**
  440. * If `useTablePrefix` equals true, then the table name will contain the
  441. * prefix format.
  442. *
  443. * @param string $tableName the table name to generate.
  444. * @return string
  445. * @since 2.0.8
  446. */
  447. protected function generateTableName($tableName)
  448. {
  449. if (!$this->useTablePrefix) {
  450. return $tableName;
  451. }
  452. return '{{%' . $tableName . '}}';
  453. }
  454. /**
  455. * Parse the command line migration fields.
  456. * @return array parse result with following fields:
  457. *
  458. * - fields: array, parsed fields
  459. * - foreignKeys: array, detected foreign keys
  460. *
  461. * @since 2.0.7
  462. */
  463. protected function parseFields()
  464. {
  465. $fields = [];
  466. $foreignKeys = [];
  467. foreach ($this->fields as $index => $field) {
  468. $chunks = $this->splitFieldIntoChunks($field);
  469. $property = array_shift($chunks);
  470. foreach ($chunks as $i => &$chunk) {
  471. if (strncmp($chunk, 'foreignKey', 10) === 0) {
  472. preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
  473. $foreignKeys[$property] = [
  474. 'table' => isset($matches[1])
  475. ? $matches[1]
  476. : preg_replace('/_id$/', '', $property),
  477. 'column' => !empty($matches[2])
  478. ? $matches[2]
  479. : null,
  480. ];
  481. unset($chunks[$i]);
  482. continue;
  483. }
  484. if (!preg_match('/^(.+?)\(([^(]+)\)$/', $chunk)) {
  485. $chunk .= '()';
  486. }
  487. }
  488. $fields[] = [
  489. 'property' => $property,
  490. 'decorators' => implode('->', $chunks),
  491. ];
  492. }
  493. return [
  494. 'fields' => $fields,
  495. 'foreignKeys' => $foreignKeys,
  496. ];
  497. }
  498. /**
  499. * Splits field into chunks
  500. *
  501. * @param string $field
  502. * @return string[]|false
  503. */
  504. protected function splitFieldIntoChunks($field)
  505. {
  506. $hasDoubleQuotes = false;
  507. preg_match_all('/defaultValue\(.*?:.*?\)/', $field, $matches);
  508. if (isset($matches[0][0])) {
  509. $hasDoubleQuotes = true;
  510. $originalDefaultValue = $matches[0][0];
  511. $defaultValue = str_replace(':', '{{colon}}', $originalDefaultValue);
  512. $field = str_replace($originalDefaultValue, $defaultValue, $field);
  513. }
  514. $chunks = preg_split('/\s?:\s?/', $field);
  515. if (is_array($chunks) && $hasDoubleQuotes) {
  516. foreach ($chunks as $key => $chunk) {
  517. $chunks[$key] = str_replace($defaultValue, $originalDefaultValue, $chunk);
  518. }
  519. }
  520. return $chunks;
  521. }
  522. /**
  523. * Adds default primary key to fields list if there's no primary key specified.
  524. * @param array $fields parsed fields
  525. * @since 2.0.7
  526. */
  527. protected function addDefaultPrimaryKey(&$fields)
  528. {
  529. foreach ($fields as $field) {
  530. if (false !== strripos($field['decorators'], 'primarykey()')) {
  531. return;
  532. }
  533. }
  534. array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
  535. }
  536. }