Migration.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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\db;
  8. use yii\base\Component;
  9. use yii\di\Instance;
  10. use yii\helpers\StringHelper;
  11. /**
  12. * Migration is the base class for representing a database migration.
  13. *
  14. * Migration is designed to be used together with the "yii migrate" command.
  15. *
  16. * Each child class of Migration represents an individual database migration which
  17. * is identified by the child class name.
  18. *
  19. * Within each migration, the [[up()]] method should be overridden to contain the logic
  20. * for "upgrading" the database; while the [[down()]] method for the "downgrading"
  21. * logic. The "yii migrate" command manages all available migrations in an application.
  22. *
  23. * If the database supports transactions, you may also override [[safeUp()]] and
  24. * [[safeDown()]] so that if anything wrong happens during the upgrading or downgrading,
  25. * the whole migration can be reverted in a whole.
  26. *
  27. * Note that some DB queries in some DBMS cannot be put into a transaction. For some examples,
  28. * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case,
  29. * you should still implement `up()` and `down()`, instead.
  30. *
  31. * Migration provides a set of convenient methods for manipulating database data and schema.
  32. * For example, the [[insert()]] method can be used to easily insert a row of data into
  33. * a database table; the [[createTable()]] method can be used to create a database table.
  34. * Compared with the same methods in [[Command]], these methods will display extra
  35. * information showing the method parameters and execution time, which may be useful when
  36. * applying migrations.
  37. *
  38. * For more details and usage information on Migration, see the [guide article on Migration](guide:db-migrations).
  39. *
  40. * @author Qiang Xue <qiang.xue@gmail.com>
  41. * @since 2.0
  42. */
  43. class Migration extends Component implements MigrationInterface
  44. {
  45. use SchemaBuilderTrait;
  46. /**
  47. * @var Connection|array|string the DB connection object or the application component ID of the DB connection
  48. * that this migration should work with. Starting from version 2.0.2, this can also be a configuration array
  49. * for creating the object.
  50. *
  51. * Note that when a Migration object is created by the `migrate` command, this property will be overwritten
  52. * by the command. If you do not want to use the DB connection provided by the command, you may override
  53. * the [[init()]] method like the following:
  54. *
  55. * ```php
  56. * public function init()
  57. * {
  58. * $this->db = 'db2';
  59. * parent::init();
  60. * }
  61. * ```
  62. */
  63. public $db = 'db';
  64. /**
  65. * @var int max number of characters of the SQL outputted. Useful for reduction of long statements and making
  66. * console output more compact.
  67. * @since 2.0.13
  68. */
  69. public $maxSqlOutputLength;
  70. /**
  71. * @var bool indicates whether the console output should be compacted.
  72. * If this is set to true, the individual commands ran within the migration will not be output to the console.
  73. * Default is false, in other words the output is fully verbose by default.
  74. * @since 2.0.13
  75. */
  76. public $compact = false;
  77. /**
  78. * Initializes the migration.
  79. * This method will set [[db]] to be the 'db' application component, if it is `null`.
  80. */
  81. public function init()
  82. {
  83. parent::init();
  84. $this->db = Instance::ensure($this->db, Connection::className());
  85. $this->db->getSchema()->refresh();
  86. $this->db->enableSlaves = false;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. * @since 2.0.6
  91. */
  92. protected function getDb()
  93. {
  94. return $this->db;
  95. }
  96. /**
  97. * This method contains the logic to be executed when applying this migration.
  98. * Child classes may override this method to provide actual migration logic.
  99. * @return bool return a false value to indicate the migration fails
  100. * and should not proceed further. All other return values mean the migration succeeds.
  101. */
  102. public function up()
  103. {
  104. $transaction = $this->db->beginTransaction();
  105. try {
  106. if ($this->safeUp() === false) {
  107. $transaction->rollBack();
  108. return false;
  109. }
  110. $transaction->commit();
  111. } catch (\Exception $e) {
  112. $this->printException($e);
  113. $transaction->rollBack();
  114. return false;
  115. } catch (\Throwable $e) {
  116. $this->printException($e);
  117. $transaction->rollBack();
  118. return false;
  119. }
  120. return null;
  121. }
  122. /**
  123. * This method contains the logic to be executed when removing this migration.
  124. * The default implementation throws an exception indicating the migration cannot be removed.
  125. * Child classes may override this method if the corresponding migrations can be removed.
  126. * @return bool return a false value to indicate the migration fails
  127. * and should not proceed further. All other return values mean the migration succeeds.
  128. */
  129. public function down()
  130. {
  131. $transaction = $this->db->beginTransaction();
  132. try {
  133. if ($this->safeDown() === false) {
  134. $transaction->rollBack();
  135. return false;
  136. }
  137. $transaction->commit();
  138. } catch (\Exception $e) {
  139. $this->printException($e);
  140. $transaction->rollBack();
  141. return false;
  142. } catch (\Throwable $e) {
  143. $this->printException($e);
  144. $transaction->rollBack();
  145. return false;
  146. }
  147. return null;
  148. }
  149. /**
  150. * @param \Throwable|\Exception $e
  151. */
  152. private function printException($e)
  153. {
  154. echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
  155. echo $e->getTraceAsString() . "\n";
  156. }
  157. /**
  158. * This method contains the logic to be executed when applying this migration.
  159. * This method differs from [[up()]] in that the DB logic implemented here will
  160. * be enclosed within a DB transaction.
  161. * Child classes may implement this method instead of [[up()]] if the DB logic
  162. * needs to be within a transaction.
  163. *
  164. * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
  165. * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).
  166. *
  167. * @return bool return a false value to indicate the migration fails
  168. * and should not proceed further. All other return values mean the migration succeeds.
  169. */
  170. public function safeUp()
  171. {
  172. }
  173. /**
  174. * This method contains the logic to be executed when removing this migration.
  175. * This method differs from [[down()]] in that the DB logic implemented here will
  176. * be enclosed within a DB transaction.
  177. * Child classes may implement this method instead of [[down()]] if the DB logic
  178. * needs to be within a transaction.
  179. *
  180. * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
  181. * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).
  182. *
  183. * @return bool return a false value to indicate the migration fails
  184. * and should not proceed further. All other return values mean the migration succeeds.
  185. */
  186. public function safeDown()
  187. {
  188. }
  189. /**
  190. * Executes a SQL statement.
  191. * This method executes the specified SQL statement using [[db]].
  192. * @param string $sql the SQL statement to be executed
  193. * @param array $params input parameters (name => value) for the SQL execution.
  194. * See [[Command::execute()]] for more details.
  195. */
  196. public function execute($sql, $params = [])
  197. {
  198. $sqlOutput = $sql;
  199. if ($this->maxSqlOutputLength !== null) {
  200. $sqlOutput = StringHelper::truncate($sql, $this->maxSqlOutputLength, '[... hidden]');
  201. }
  202. $time = $this->beginCommand("execute SQL: $sqlOutput");
  203. $this->db->createCommand($sql)->bindValues($params)->execute();
  204. $this->endCommand($time);
  205. }
  206. /**
  207. * Creates and executes an INSERT SQL statement.
  208. * The method will properly escape the column names, and bind the values to be inserted.
  209. * @param string $table the table that new rows will be inserted into.
  210. * @param array $columns the column data (name => value) to be inserted into the table.
  211. */
  212. public function insert($table, $columns)
  213. {
  214. $time = $this->beginCommand("insert into $table");
  215. $this->db->createCommand()->insert($table, $columns)->execute();
  216. $this->endCommand($time);
  217. }
  218. /**
  219. * Creates and executes a batch INSERT SQL statement.
  220. * The method will properly escape the column names, and bind the values to be inserted.
  221. * @param string $table the table that new rows will be inserted into.
  222. * @param array $columns the column names.
  223. * @param array $rows the rows to be batch inserted into the table
  224. */
  225. public function batchInsert($table, $columns, $rows)
  226. {
  227. $time = $this->beginCommand("insert into $table");
  228. $this->db->createCommand()->batchInsert($table, $columns, $rows)->execute();
  229. $this->endCommand($time);
  230. }
  231. /**
  232. * Creates and executes a command to insert rows into a database table if
  233. * they do not already exist (matching unique constraints),
  234. * or update them if they do.
  235. *
  236. * The method will properly escape the column names, and bind the values to be inserted.
  237. *
  238. * @param string $table the table that new rows will be inserted into/updated in.
  239. * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance
  240. * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement.
  241. * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist.
  242. * If `true` is passed, the column data will be updated to match the insert column data.
  243. * If `false` is passed, no update will be performed if the column data already exists.
  244. * @param array $params the parameters to be bound to the command.
  245. * @return $this the command object itself.
  246. * @since 2.0.14
  247. */
  248. public function upsert($table, $insertColumns, $updateColumns = true, $params = [])
  249. {
  250. $time = $this->beginCommand("upsert into $table");
  251. $this->db->createCommand()->upsert($table, $insertColumns, $updateColumns, $params)->execute();
  252. $this->endCommand($time);
  253. }
  254. /**
  255. * Creates and executes an UPDATE SQL statement.
  256. * The method will properly escape the column names and bind the values to be updated.
  257. * @param string $table the table to be updated.
  258. * @param array $columns the column data (name => value) to be updated.
  259. * @param array|string $condition the conditions that will be put in the WHERE part. Please
  260. * refer to [[Query::where()]] on how to specify conditions.
  261. * @param array $params the parameters to be bound to the query.
  262. */
  263. public function update($table, $columns, $condition = '', $params = [])
  264. {
  265. $time = $this->beginCommand("update $table");
  266. $this->db->createCommand()->update($table, $columns, $condition, $params)->execute();
  267. $this->endCommand($time);
  268. }
  269. /**
  270. * Creates and executes a DELETE SQL statement.
  271. * @param string $table the table where the data will be deleted from.
  272. * @param array|string $condition the conditions that will be put in the WHERE part. Please
  273. * refer to [[Query::where()]] on how to specify conditions.
  274. * @param array $params the parameters to be bound to the query.
  275. */
  276. public function delete($table, $condition = '', $params = [])
  277. {
  278. $time = $this->beginCommand("delete from $table");
  279. $this->db->createCommand()->delete($table, $condition, $params)->execute();
  280. $this->endCommand($time);
  281. }
  282. /**
  283. * Builds and executes a SQL statement for creating a new DB table.
  284. *
  285. * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'),
  286. * where name stands for a column name which will be properly quoted by the method, and definition
  287. * stands for the column type which can contain an abstract DB type.
  288. *
  289. * The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one.
  290. *
  291. * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
  292. * put into the generated SQL.
  293. *
  294. * @param string $table the name of the table to be created. The name will be properly quoted by the method.
  295. * @param array $columns the columns (name => definition) in the new table.
  296. * @param string $options additional SQL fragment that will be appended to the generated SQL.
  297. */
  298. public function createTable($table, $columns, $options = null)
  299. {
  300. $time = $this->beginCommand("create table $table");
  301. $this->db->createCommand()->createTable($table, $columns, $options)->execute();
  302. foreach ($columns as $column => $type) {
  303. if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
  304. $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
  305. }
  306. }
  307. $this->endCommand($time);
  308. }
  309. /**
  310. * Builds and executes a SQL statement for renaming a DB table.
  311. * @param string $table the table to be renamed. The name will be properly quoted by the method.
  312. * @param string $newName the new table name. The name will be properly quoted by the method.
  313. */
  314. public function renameTable($table, $newName)
  315. {
  316. $time = $this->beginCommand("rename table $table to $newName");
  317. $this->db->createCommand()->renameTable($table, $newName)->execute();
  318. $this->endCommand($time);
  319. }
  320. /**
  321. * Builds and executes a SQL statement for dropping a DB table.
  322. * @param string $table the table to be dropped. The name will be properly quoted by the method.
  323. */
  324. public function dropTable($table)
  325. {
  326. $time = $this->beginCommand("drop table $table");
  327. $this->db->createCommand()->dropTable($table)->execute();
  328. $this->endCommand($time);
  329. }
  330. /**
  331. * Builds and executes a SQL statement for truncating a DB table.
  332. * @param string $table the table to be truncated. The name will be properly quoted by the method.
  333. */
  334. public function truncateTable($table)
  335. {
  336. $time = $this->beginCommand("truncate table $table");
  337. $this->db->createCommand()->truncateTable($table)->execute();
  338. $this->endCommand($time);
  339. }
  340. /**
  341. * Builds and executes a SQL statement for adding a new DB column.
  342. * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
  343. * @param string $column the name of the new column. The name will be properly quoted by the method.
  344. * @param string $type the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any)
  345. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  346. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  347. */
  348. public function addColumn($table, $column, $type)
  349. {
  350. $time = $this->beginCommand("add column $column $type to table $table");
  351. $this->db->createCommand()->addColumn($table, $column, $type)->execute();
  352. if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
  353. $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
  354. }
  355. $this->endCommand($time);
  356. }
  357. /**
  358. * Builds and executes a SQL statement for dropping a DB column.
  359. * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
  360. * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
  361. */
  362. public function dropColumn($table, $column)
  363. {
  364. $time = $this->beginCommand("drop column $column from table $table");
  365. $this->db->createCommand()->dropColumn($table, $column)->execute();
  366. $this->endCommand($time);
  367. }
  368. /**
  369. * Builds and executes a SQL statement for renaming a column.
  370. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
  371. * @param string $name the old name of the column. The name will be properly quoted by the method.
  372. * @param string $newName the new name of the column. The name will be properly quoted by the method.
  373. */
  374. public function renameColumn($table, $name, $newName)
  375. {
  376. $time = $this->beginCommand("rename column $name in table $table to $newName");
  377. $this->db->createCommand()->renameColumn($table, $name, $newName)->execute();
  378. $this->endCommand($time);
  379. }
  380. /**
  381. * Builds and executes a SQL statement for changing the definition of a column.
  382. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  383. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  384. * @param string $type the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any)
  385. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  386. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  387. */
  388. public function alterColumn($table, $column, $type)
  389. {
  390. $time = $this->beginCommand("alter column $column in table $table to $type");
  391. $this->db->createCommand()->alterColumn($table, $column, $type)->execute();
  392. if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
  393. $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
  394. }
  395. $this->endCommand($time);
  396. }
  397. /**
  398. * Builds and executes a SQL statement for creating a primary key.
  399. * The method will properly quote the table and column names.
  400. * @param string $name the name of the primary key constraint.
  401. * @param string $table the table that the primary key constraint will be added to.
  402. * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
  403. */
  404. public function addPrimaryKey($name, $table, $columns)
  405. {
  406. $time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')');
  407. $this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
  408. $this->endCommand($time);
  409. }
  410. /**
  411. * Builds and executes a SQL statement for dropping a primary key.
  412. * @param string $name the name of the primary key constraint to be removed.
  413. * @param string $table the table that the primary key constraint will be removed from.
  414. */
  415. public function dropPrimaryKey($name, $table)
  416. {
  417. $time = $this->beginCommand("drop primary key $name");
  418. $this->db->createCommand()->dropPrimaryKey($name, $table)->execute();
  419. $this->endCommand($time);
  420. }
  421. /**
  422. * Builds a SQL statement for adding a foreign key constraint to an existing table.
  423. * The method will properly quote the table and column names.
  424. * @param string $name the name of the foreign key constraint.
  425. * @param string $table the table that the foreign key constraint will be added to.
  426. * @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array.
  427. * @param string $refTable the table that the foreign key references to.
  428. * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array.
  429. * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  430. * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  431. */
  432. public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
  433. {
  434. $time = $this->beginCommand("add foreign key $name: $table (" . implode(',', (array) $columns) . ") references $refTable (" . implode(',', (array) $refColumns) . ')');
  435. $this->db->createCommand()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update)->execute();
  436. $this->endCommand($time);
  437. }
  438. /**
  439. * Builds a SQL statement for dropping a foreign key constraint.
  440. * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
  441. * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
  442. */
  443. public function dropForeignKey($name, $table)
  444. {
  445. $time = $this->beginCommand("drop foreign key $name from table $table");
  446. $this->db->createCommand()->dropForeignKey($name, $table)->execute();
  447. $this->endCommand($time);
  448. }
  449. /**
  450. * Builds and executes a SQL statement for creating a new index.
  451. * @param string $name the name of the index. The name will be properly quoted by the method.
  452. * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  453. * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them
  454. * by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that
  455. * include a left parenthesis "(".
  456. * @param bool $unique whether to add UNIQUE constraint on the created index.
  457. */
  458. public function createIndex($name, $table, $columns, $unique = false)
  459. {
  460. $time = $this->beginCommand('create' . ($unique ? ' unique' : '') . " index $name on $table (" . implode(',', (array) $columns) . ')');
  461. $this->db->createCommand()->createIndex($name, $table, $columns, $unique)->execute();
  462. $this->endCommand($time);
  463. }
  464. /**
  465. * Builds and executes a SQL statement for dropping an index.
  466. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  467. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  468. */
  469. public function dropIndex($name, $table)
  470. {
  471. $time = $this->beginCommand("drop index $name on $table");
  472. $this->db->createCommand()->dropIndex($name, $table)->execute();
  473. $this->endCommand($time);
  474. }
  475. /**
  476. * Builds and execute a SQL statement for adding comment to column.
  477. *
  478. * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
  479. * @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
  480. * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
  481. * @since 2.0.8
  482. */
  483. public function addCommentOnColumn($table, $column, $comment)
  484. {
  485. $time = $this->beginCommand("add comment on column $column");
  486. $this->db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();
  487. $this->endCommand($time);
  488. }
  489. /**
  490. * Builds a SQL statement for adding comment to table.
  491. *
  492. * @param string $table the table to be commented. The table name will be properly quoted by the method.
  493. * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
  494. * @since 2.0.8
  495. */
  496. public function addCommentOnTable($table, $comment)
  497. {
  498. $time = $this->beginCommand("add comment on table $table");
  499. $this->db->createCommand()->addCommentOnTable($table, $comment)->execute();
  500. $this->endCommand($time);
  501. }
  502. /**
  503. * Builds and execute a SQL statement for dropping comment from column.
  504. *
  505. * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
  506. * @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
  507. * @since 2.0.8
  508. */
  509. public function dropCommentFromColumn($table, $column)
  510. {
  511. $time = $this->beginCommand("drop comment from column $column");
  512. $this->db->createCommand()->dropCommentFromColumn($table, $column)->execute();
  513. $this->endCommand($time);
  514. }
  515. /**
  516. * Builds a SQL statement for dropping comment from table.
  517. *
  518. * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
  519. * @since 2.0.8
  520. */
  521. public function dropCommentFromTable($table)
  522. {
  523. $time = $this->beginCommand("drop comment from table $table");
  524. $this->db->createCommand()->dropCommentFromTable($table)->execute();
  525. $this->endCommand($time);
  526. }
  527. /**
  528. * Prepares for a command to be executed, and outputs to the console.
  529. *
  530. * @param string $description the description for the command, to be output to the console.
  531. * @return float the time before the command is executed, for the time elapsed to be calculated.
  532. * @since 2.0.13
  533. */
  534. protected function beginCommand($description)
  535. {
  536. if (!$this->compact) {
  537. echo " > $description ...";
  538. }
  539. return microtime(true);
  540. }
  541. /**
  542. * Finalizes after the command has been executed, and outputs to the console the time elapsed.
  543. *
  544. * @param float $time the time before the command was executed.
  545. * @since 2.0.13
  546. */
  547. protected function endCommand($time)
  548. {
  549. if (!$this->compact) {
  550. echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  551. }
  552. }
  553. }