MigrateController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 = false;
  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. $db->createCommand()->dropTable($schema->name)->execute();
  288. $this->stdout("Table {$schema->name} dropped.\n");
  289. }
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. protected function removeMigrationHistory($version)
  295. {
  296. $command = $this->db->createCommand();
  297. $command->delete($this->migrationTable, [
  298. 'version' => $version,
  299. ])->execute();
  300. }
  301. private $_migrationNameLimit;
  302. /**
  303. * {@inheritdoc}
  304. * @since 2.0.13
  305. */
  306. protected function getMigrationNameLimit()
  307. {
  308. if ($this->_migrationNameLimit !== null) {
  309. return $this->_migrationNameLimit;
  310. }
  311. $tableSchema = $this->db->schema ? $this->db->schema->getTableSchema($this->migrationTable, true) : null;
  312. if ($tableSchema !== null) {
  313. return $this->_migrationNameLimit = $tableSchema->columns['version']->size;
  314. }
  315. return static::MAX_NAME_LENGTH;
  316. }
  317. /**
  318. * {@inheritdoc}
  319. * @since 2.0.8
  320. */
  321. protected function generateMigrationSourceCode($params)
  322. {
  323. $parsedFields = $this->parseFields();
  324. $fields = $parsedFields['fields'];
  325. $foreignKeys = $parsedFields['foreignKeys'];
  326. $name = $params['name'];
  327. $templateFile = $this->templateFile;
  328. $table = null;
  329. if (preg_match('/^create_junction(?:_table_for_|_for_|_)(.+)_and_(.+)_tables?$/', $name, $matches)) {
  330. $templateFile = $this->generatorTemplateFiles['create_junction'];
  331. $firstTable = $matches[1];
  332. $secondTable = $matches[2];
  333. $fields = array_merge(
  334. [
  335. [
  336. 'property' => $firstTable . '_id',
  337. 'decorators' => 'integer()',
  338. ],
  339. [
  340. 'property' => $secondTable . '_id',
  341. 'decorators' => 'integer()',
  342. ],
  343. ],
  344. $fields,
  345. [
  346. [
  347. 'property' => 'PRIMARY KEY(' .
  348. $firstTable . '_id, ' .
  349. $secondTable . '_id)',
  350. ],
  351. ]
  352. );
  353. $foreignKeys[$firstTable . '_id']['table'] = $firstTable;
  354. $foreignKeys[$secondTable . '_id']['table'] = $secondTable;
  355. $foreignKeys[$firstTable . '_id']['column'] = null;
  356. $foreignKeys[$secondTable . '_id']['column'] = null;
  357. $table = $firstTable . '_' . $secondTable;
  358. } elseif (preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches)) {
  359. $templateFile = $this->generatorTemplateFiles['add_column'];
  360. $table = $matches[2];
  361. } elseif (preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches)) {
  362. $templateFile = $this->generatorTemplateFiles['drop_column'];
  363. $table = $matches[2];
  364. } elseif (preg_match('/^create_(.+)_table$/', $name, $matches)) {
  365. $this->addDefaultPrimaryKey($fields);
  366. $templateFile = $this->generatorTemplateFiles['create_table'];
  367. $table = $matches[1];
  368. } elseif (preg_match('/^drop_(.+)_table$/', $name, $matches)) {
  369. $this->addDefaultPrimaryKey($fields);
  370. $templateFile = $this->generatorTemplateFiles['drop_table'];
  371. $table = $matches[1];
  372. }
  373. foreach ($foreignKeys as $column => $foreignKey) {
  374. $relatedColumn = $foreignKey['column'];
  375. $relatedTable = $foreignKey['table'];
  376. // Since 2.0.11 if related column name is not specified,
  377. // we're trying to get it from table schema
  378. // @see https://github.com/yiisoft/yii2/issues/12748
  379. if ($relatedColumn === null) {
  380. $relatedColumn = 'id';
  381. try {
  382. $this->db = Instance::ensure($this->db, Connection::className());
  383. $relatedTableSchema = $this->db->getTableSchema($relatedTable);
  384. if ($relatedTableSchema !== null) {
  385. $primaryKeyCount = count($relatedTableSchema->primaryKey);
  386. if ($primaryKeyCount === 1) {
  387. $relatedColumn = $relatedTableSchema->primaryKey[0];
  388. } elseif ($primaryKeyCount > 1) {
  389. $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);
  390. } elseif ($primaryKeyCount === 0) {
  391. $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);
  392. }
  393. }
  394. } catch (\ReflectionException $e) {
  395. $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);
  396. }
  397. }
  398. $foreignKeys[$column] = [
  399. 'idx' => $this->generateTableName("idx-$table-$column"),
  400. 'fk' => $this->generateTableName("fk-$table-$column"),
  401. 'relatedTable' => $this->generateTableName($relatedTable),
  402. 'relatedColumn' => $relatedColumn,
  403. ];
  404. }
  405. return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
  406. 'table' => $this->generateTableName($table),
  407. 'fields' => $fields,
  408. 'foreignKeys' => $foreignKeys,
  409. 'tableComment' => $this->comment,
  410. ]));
  411. }
  412. /**
  413. * If `useTablePrefix` equals true, then the table name will contain the
  414. * prefix format.
  415. *
  416. * @param string $tableName the table name to generate.
  417. * @return string
  418. * @since 2.0.8
  419. */
  420. protected function generateTableName($tableName)
  421. {
  422. if (!$this->useTablePrefix) {
  423. return $tableName;
  424. }
  425. return '{{%' . $tableName . '}}';
  426. }
  427. /**
  428. * Parse the command line migration fields.
  429. * @return array parse result with following fields:
  430. *
  431. * - fields: array, parsed fields
  432. * - foreignKeys: array, detected foreign keys
  433. *
  434. * @since 2.0.7
  435. */
  436. protected function parseFields()
  437. {
  438. $fields = [];
  439. $foreignKeys = [];
  440. foreach ($this->fields as $index => $field) {
  441. $chunks = preg_split('/\s?:\s?/', $field, null);
  442. $property = array_shift($chunks);
  443. foreach ($chunks as $i => &$chunk) {
  444. if (strncmp($chunk, 'foreignKey', 10) === 0) {
  445. preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
  446. $foreignKeys[$property] = [
  447. 'table' => isset($matches[1])
  448. ? $matches[1]
  449. : preg_replace('/_id$/', '', $property),
  450. 'column' => !empty($matches[2])
  451. ? $matches[2]
  452. : null,
  453. ];
  454. unset($chunks[$i]);
  455. continue;
  456. }
  457. if (!preg_match('/^(.+?)\(([^(]+)\)$/', $chunk)) {
  458. $chunk .= '()';
  459. }
  460. }
  461. $fields[] = [
  462. 'property' => $property,
  463. 'decorators' => implode('->', $chunks),
  464. ];
  465. }
  466. return [
  467. 'fields' => $fields,
  468. 'foreignKeys' => $foreignKeys,
  469. ];
  470. }
  471. /**
  472. * Adds default primary key to fields list if there's no primary key specified.
  473. * @param array $fields parsed fields
  474. * @since 2.0.7
  475. */
  476. protected function addDefaultPrimaryKey(&$fields)
  477. {
  478. foreach ($fields as $field) {
  479. if (false !== strripos($field['decorators'], 'primarykey()')) {
  480. return;
  481. }
  482. }
  483. array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
  484. }
  485. }