Completed
Push — master ( a8d4f8...8bb334 )
by Alexander
220:03 queued 217:16
created

Migration   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 546
Duplicated Lines 0 %

Test Coverage

Coverage 24.04%

Importance

Changes 0
Metric Value
wmc 51
eloc 129
dl 0
loc 546
ccs 44
cts 183
cp 0.2404
rs 7.92
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A safeUp() 0 2 1
A renameColumn() 0 5 1
A execute() 0 10 2
A dropCommentFromTable() 0 5 1
A createIndex() 0 5 2
A addPrimaryKey() 0 5 2
A addForeignKey() 0 5 1
A dropPrimaryKey() 0 5 1
A insert() 0 5 1
A createTable() 0 10 4
A dropForeignKey() 0 5 1
A alterColumn() 0 8 3
A down() 0 20 4
A batchInsert() 0 5 1
A endCommand() 0 4 2
A dropIndex() 0 5 1
A dropColumn() 0 5 1
A upsert() 0 5 1
A renameTable() 0 5 1
A addCommentOnTable() 0 5 1
A dropCommentFromColumn() 0 5 1
A truncateTable() 0 5 1
A update() 0 5 1
A beginCommand() 0 6 2
A dropTable() 0 5 1
A printException() 0 4 1
A getDb() 0 3 1
A delete() 0 5 1
A up() 0 20 4
A init() 0 6 1
A addCommentOnColumn() 0 5 1
A safeDown() 0 2 1
A addColumn() 0 8 3

How to fix   Complexity   

Complex Class

Complex classes like Migration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Migration, and based on these observations, apply Extract Interface, too.

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
8
namespace yii\db;
9
10
use yii\base\Component;
11
use yii\di\Instance;
12
use yii\helpers\StringHelper;
13
14
/**
15
 * Migration is the base class for representing a database migration.
16
 *
17
 * Migration is designed to be used together with the "yii migrate" command.
18
 *
19
 * Each child class of Migration represents an individual database migration which
20
 * is identified by the child class name.
21
 *
22
 * Within each migration, the [[up()]] method should be overridden to contain the logic
23
 * for "upgrading" the database; while the [[down()]] method for the "downgrading"
24
 * logic. The "yii migrate" command manages all available migrations in an application.
25
 *
26
 * If the database supports transactions, you may also override [[safeUp()]] and
27
 * [[safeDown()]] so that if anything wrong happens during the upgrading or downgrading,
28
 * the whole migration can be reverted in a whole.
29
 *
30
 * Note that some DB queries in some DBMS cannot be put into a transaction. For some examples,
31
 * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case,
32
 * you should still implement `up()` and `down()`, instead.
33
 *
34
 * Migration provides a set of convenient methods for manipulating database data and schema.
35
 * For example, the [[insert()]] method can be used to easily insert a row of data into
36
 * a database table; the [[createTable()]] method can be used to create a database table.
37
 * Compared with the same methods in [[Command]], these methods will display extra
38
 * information showing the method parameters and execution time, which may be useful when
39
 * applying migrations.
40
 *
41
 * For more details and usage information on Migration, see the [guide article on Migration](guide:db-migrations).
42
 *
43
 * @author Qiang Xue <[email protected]>
44
 * @since 2.0
45
 */
46
class Migration extends Component implements MigrationInterface
47
{
48
    use SchemaBuilderTrait;
49
50
    /**
51
     * @var Connection|array|string the DB connection object or the application component ID of the DB connection
52
     * that this migration should work with. Starting from version 2.0.2, this can also be a configuration array
53
     * for creating the object.
54
     *
55
     * Note that when a Migration object is created by the `migrate` command, this property will be overwritten
56
     * by the command. If you do not want to use the DB connection provided by the command, you may override
57
     * the [[init()]] method like the following:
58
     *
59
     * ```php
60
     * public function init()
61
     * {
62
     *     $this->db = 'db2';
63
     *     parent::init();
64
     * }
65
     * ```
66
     */
67
    public $db = 'db';
68
    /**
69
     * @var int max number of characters of the SQL outputted. Useful for reduction of long statements and making
70
     * console output more compact.
71
     * @since 2.0.13
72
     */
73
    public $maxSqlOutputLength;
74
    /**
75
     * @var bool indicates whether the console output should be compacted.
76
     * If this is set to true, the individual commands ran within the migration will not be output to the console.
77
     * Default is false, in other words the output is fully verbose by default.
78
     * @since 2.0.13
79
     */
80
    public $compact = false;
81
82
83
    /**
84
     * Initializes the migration.
85
     * This method will set [[db]] to be the 'db' application component, if it is `null`.
86
     */
87 45
    public function init()
88
    {
89 45
        parent::init();
90 45
        $this->db = Instance::ensure($this->db, Connection::className());
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
        $this->db = Instance::ensure($this->db, /** @scrutinizer ignore-deprecated */ Connection::className());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91 45
        $this->db->getSchema()->refresh();
92 45
        $this->db->enableSlaves = false;
93 45
    }
94
95
    /**
96
     * {@inheritdoc}
97
     * @since 2.0.6
98
     */
99 30
    protected function getDb()
100
    {
101 30
        return $this->db;
102
    }
103
104
    /**
105
     * This method contains the logic to be executed when applying this migration.
106
     * Child classes may override this method to provide actual migration logic.
107
     * @return bool return a false value to indicate the migration fails
108
     * and should not proceed further. All other return values mean the migration succeeds.
109
     */
110 1
    public function up()
111
    {
112 1
        $transaction = $this->db->beginTransaction();
113
        try {
114 1
            if ($this->safeUp() === false) {
115
                $transaction->rollBack();
116
                return false;
117
            }
118 1
            $transaction->commit();
119
        } catch (\Exception $e) {
120
            $this->printException($e);
121
            $transaction->rollBack();
122
            return false;
123
        } catch (\Throwable $e) {
124
            $this->printException($e);
125
            $transaction->rollBack();
126
            return false;
127
        }
128
129 1
        return null;
130
    }
131
132
    /**
133
     * This method contains the logic to be executed when removing this migration.
134
     * The default implementation throws an exception indicating the migration cannot be removed.
135
     * Child classes may override this method if the corresponding migrations can be removed.
136
     * @return bool return a false value to indicate the migration fails
137
     * and should not proceed further. All other return values mean the migration succeeds.
138
     */
139
    public function down()
140
    {
141
        $transaction = $this->db->beginTransaction();
142
        try {
143
            if ($this->safeDown() === false) {
144
                $transaction->rollBack();
145
                return false;
146
            }
147
            $transaction->commit();
148
        } catch (\Exception $e) {
149
            $this->printException($e);
150
            $transaction->rollBack();
151
            return false;
152
        } catch (\Throwable $e) {
153
            $this->printException($e);
154
            $transaction->rollBack();
155
            return false;
156
        }
157
158
        return null;
159
    }
160
161
    /**
162
     * @param \Throwable|\Exception $e
163
     */
164
    private function printException($e)
165
    {
166
        echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
167
        echo $e->getTraceAsString() . "\n";
168
    }
169
170
    /**
171
     * This method contains the logic to be executed when applying this migration.
172
     * This method differs from [[up()]] in that the DB logic implemented here will
173
     * be enclosed within a DB transaction.
174
     * Child classes may implement this method instead of [[up()]] if the DB logic
175
     * needs to be within a transaction.
176
     *
177
     * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
178
     * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).
179
     *
180
     * @return bool return a false value to indicate the migration fails
181
     * and should not proceed further. All other return values mean the migration succeeds.
182
     */
183
    public function safeUp()
184
    {
185
    }
186
187
    /**
188
     * This method contains the logic to be executed when removing this migration.
189
     * This method differs from [[down()]] in that the DB logic implemented here will
190
     * be enclosed within a DB transaction.
191
     * Child classes may implement this method instead of [[down()]] if the DB logic
192
     * needs to be within a transaction.
193
     *
194
     * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
195
     * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).
196
     *
197
     * @return bool return a false value to indicate the migration fails
198
     * and should not proceed further. All other return values mean the migration succeeds.
199
     */
200
    public function safeDown()
201
    {
202
    }
203
204
    /**
205
     * Executes a SQL statement.
206
     * This method executes the specified SQL statement using [[db]].
207
     * @param string $sql the SQL statement to be executed
208
     * @param array $params input parameters (name => value) for the SQL execution.
209
     * See [[Command::execute()]] for more details.
210
     */
211
    public function execute($sql, $params = [])
212
    {
213
        $sqlOutput = $sql;
214
        if ($this->maxSqlOutputLength !== null) {
215
            $sqlOutput = StringHelper::truncate($sql, $this->maxSqlOutputLength, '[... hidden]');
216
        }
217
218
        $time = $this->beginCommand("execute SQL: $sqlOutput");
219
        $this->db->createCommand($sql)->bindValues($params)->execute();
220
        $this->endCommand($time);
221
    }
222
223
    /**
224
     * Creates and executes an INSERT SQL statement.
225
     * The method will properly escape the column names, and bind the values to be inserted.
226
     * @param string $table the table that new rows will be inserted into.
227
     * @param array $columns the column data (name => value) to be inserted into the table.
228
     */
229
    public function insert($table, $columns)
230
    {
231
        $time = $this->beginCommand("insert into $table");
232
        $this->db->createCommand()->insert($table, $columns)->execute();
233
        $this->endCommand($time);
234
    }
235
236
    /**
237
     * Creates and executes a batch INSERT SQL statement.
238
     * The method will properly escape the column names, and bind the values to be inserted.
239
     * @param string $table the table that new rows will be inserted into.
240
     * @param array $columns the column names.
241
     * @param array $rows the rows to be batch inserted into the table
242
     */
243
    public function batchInsert($table, $columns, $rows)
244
    {
245
        $time = $this->beginCommand("insert into $table");
246
        $this->db->createCommand()->batchInsert($table, $columns, $rows)->execute();
247
        $this->endCommand($time);
248
    }
249
250
    /**
251
     * Creates and executes a command to insert rows into a database table if
252
     * they do not already exist (matching unique constraints),
253
     * or update them if they do.
254
     *
255
     * The method will properly escape the column names, and bind the values to be inserted.
256
     *
257
     * @param string $table the table that new rows will be inserted into/updated in.
258
     * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance
259
     * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement.
260
     * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist.
261
     * If `true` is passed, the column data will be updated to match the insert column data.
262
     * If `false` is passed, no update will be performed if the column data already exists.
263
     * @param array $params the parameters to be bound to the command.
264
     * @return $this the command object itself.
265
     * @since 2.0.14
266
     */
267
    public function upsert($table, $insertColumns, $updateColumns = true, $params = [])
268
    {
269
        $time = $this->beginCommand("upsert into $table");
270
        $this->db->createCommand()->upsert($table, $insertColumns, $updateColumns, $params)->execute();
271
        $this->endCommand($time);
272
    }
273
274
    /**
275
     * Creates and executes an UPDATE SQL statement.
276
     * The method will properly escape the column names and bind the values to be updated.
277
     * @param string $table the table to be updated.
278
     * @param array $columns the column data (name => value) to be updated.
279
     * @param array|string $condition the conditions that will be put in the WHERE part. Please
280
     * refer to [[Query::where()]] on how to specify conditions.
281
     * @param array $params the parameters to be bound to the query.
282
     */
283
    public function update($table, $columns, $condition = '', $params = [])
284
    {
285
        $time = $this->beginCommand("update $table");
286
        $this->db->createCommand()->update($table, $columns, $condition, $params)->execute();
287
        $this->endCommand($time);
288
    }
289
290
    /**
291
     * Creates and executes a DELETE SQL statement.
292
     * @param string $table the table where the data will be deleted from.
293
     * @param array|string $condition the conditions that will be put in the WHERE part. Please
294
     * refer to [[Query::where()]] on how to specify conditions.
295
     * @param array $params the parameters to be bound to the query.
296
     */
297
    public function delete($table, $condition = '', $params = [])
298
    {
299
        $time = $this->beginCommand("delete from $table");
300
        $this->db->createCommand()->delete($table, $condition, $params)->execute();
301
        $this->endCommand($time);
302
    }
303
304
    /**
305
     * Builds and executes a SQL statement for creating a new DB table.
306
     *
307
     * The columns in the new  table should be specified as name-definition pairs (e.g. 'name' => 'string'),
308
     * where name stands for a column name which will be properly quoted by the method, and definition
309
     * stands for the column type which can contain an abstract DB type.
310
     *
311
     * The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one.
312
     *
313
     * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
314
     * put into the generated SQL.
315
     *
316
     * @param string $table the name of the table to be created. The name will be properly quoted by the method.
317
     * @param array $columns the columns (name => definition) in the new table.
318
     * @param string $options additional SQL fragment that will be appended to the generated SQL.
319
     */
320 30
    public function createTable($table, $columns, $options = null)
321
    {
322 30
        $time = $this->beginCommand("create table $table");
323 30
        $this->db->createCommand()->createTable($table, $columns, $options)->execute();
324 30
        foreach ($columns as $column => $type) {
325 30
            if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
326 30
                $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
327
            }
328
        }
329 30
        $this->endCommand($time);
330 30
    }
331
332
    /**
333
     * Builds and executes a SQL statement for renaming a DB table.
334
     * @param string $table the table to be renamed. The name will be properly quoted by the method.
335
     * @param string $newName the new table name. The name will be properly quoted by the method.
336
     */
337
    public function renameTable($table, $newName)
338
    {
339
        $time = $this->beginCommand("rename table $table to $newName");
340
        $this->db->createCommand()->renameTable($table, $newName)->execute();
341
        $this->endCommand($time);
342
    }
343
344
    /**
345
     * Builds and executes a SQL statement for dropping a DB table.
346
     * @param string $table the table to be dropped. The name will be properly quoted by the method.
347
     */
348 30
    public function dropTable($table)
349
    {
350 30
        $time = $this->beginCommand("drop table $table");
351 30
        $this->db->createCommand()->dropTable($table)->execute();
352 30
        $this->endCommand($time);
353 30
    }
354
355
    /**
356
     * Builds and executes a SQL statement for truncating a DB table.
357
     * @param string $table the table to be truncated. The name will be properly quoted by the method.
358
     */
359
    public function truncateTable($table)
360
    {
361
        $time = $this->beginCommand("truncate table $table");
362
        $this->db->createCommand()->truncateTable($table)->execute();
363
        $this->endCommand($time);
364
    }
365
366
    /**
367
     * Builds and executes a SQL statement for adding a new DB column.
368
     * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
369
     * @param string $column the name of the new column. The name will be properly quoted by the method.
370
     * @param string $type the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any)
371
     * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
372
     * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
373
     */
374 3
    public function addColumn($table, $column, $type)
375
    {
376 3
        $time = $this->beginCommand("add column $column $type to table $table");
377 3
        $this->db->createCommand()->addColumn($table, $column, $type)->execute();
378 3
        if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
0 ignored issues
show
introduced by
$type is never a sub-type of yii\db\ColumnSchemaBuilder.
Loading history...
379
            $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
380
        }
381 3
        $this->endCommand($time);
382 3
    }
383
384
    /**
385
     * Builds and executes a SQL statement for dropping a DB column.
386
     * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
387
     * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
388
     */
389
    public function dropColumn($table, $column)
390
    {
391
        $time = $this->beginCommand("drop column $column from table $table");
392
        $this->db->createCommand()->dropColumn($table, $column)->execute();
393
        $this->endCommand($time);
394
    }
395
396
    /**
397
     * Builds and executes a SQL statement for renaming a column.
398
     * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
399
     * @param string $name the old name of the column. The name will be properly quoted by the method.
400
     * @param string $newName the new name of the column. The name will be properly quoted by the method.
401
     */
402
    public function renameColumn($table, $name, $newName)
403
    {
404
        $time = $this->beginCommand("rename column $name in table $table to $newName");
405
        $this->db->createCommand()->renameColumn($table, $name, $newName)->execute();
406
        $this->endCommand($time);
407
    }
408
409
    /**
410
     * Builds and executes a SQL statement for changing the definition of a column.
411
     * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
412
     * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
413
     * @param string $type the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any)
414
     * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
415
     * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
416
     */
417
    public function alterColumn($table, $column, $type)
418
    {
419
        $time = $this->beginCommand("alter column $column in table $table to $type");
420
        $this->db->createCommand()->alterColumn($table, $column, $type)->execute();
421
        if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
0 ignored issues
show
introduced by
$type is never a sub-type of yii\db\ColumnSchemaBuilder.
Loading history...
422
            $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
423
        }
424
        $this->endCommand($time);
425
    }
426
427
    /**
428
     * Builds and executes a SQL statement for creating a primary key.
429
     * The method will properly quote the table and column names.
430
     * @param string $name the name of the primary key constraint.
431
     * @param string $table the table that the primary key constraint will be added to.
432
     * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
433
     */
434
    public function addPrimaryKey($name, $table, $columns)
435
    {
436
        $time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')');
437
        $this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
438
        $this->endCommand($time);
439
    }
440
441
    /**
442
     * Builds and executes a SQL statement for dropping a primary key.
443
     * @param string $name the name of the primary key constraint to be removed.
444
     * @param string $table the table that the primary key constraint will be removed from.
445
     */
446
    public function dropPrimaryKey($name, $table)
447
    {
448
        $time = $this->beginCommand("drop primary key $name");
449
        $this->db->createCommand()->dropPrimaryKey($name, $table)->execute();
450
        $this->endCommand($time);
451
    }
452
453
    /**
454
     * Builds a SQL statement for adding a foreign key constraint to an existing table.
455
     * The method will properly quote the table and column names.
456
     * @param string $name the name of the foreign key constraint.
457
     * @param string $table the table that the foreign key constraint will be added to.
458
     * @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.
459
     * @param string $refTable the table that the foreign key references to.
460
     * @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.
461
     * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
462
     * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
463
     */
464
    public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
465
    {
466
        $time = $this->beginCommand("add foreign key $name: $table (" . implode(',', (array) $columns) . ") references $refTable (" . implode(',', (array) $refColumns) . ')');
467
        $this->db->createCommand()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update)->execute();
468
        $this->endCommand($time);
469
    }
470
471
    /**
472
     * Builds a SQL statement for dropping a foreign key constraint.
473
     * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
474
     * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
475
     */
476
    public function dropForeignKey($name, $table)
477
    {
478
        $time = $this->beginCommand("drop foreign key $name from table $table");
479
        $this->db->createCommand()->dropForeignKey($name, $table)->execute();
480
        $this->endCommand($time);
481
    }
482
483
    /**
484
     * Builds and executes a SQL statement for creating a new index.
485
     * @param string $name the name of the index. The name will be properly quoted by the method.
486
     * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
487
     * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them
488
     * by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that
489
     * include a left parenthesis "(".
490
     * @param bool $unique whether to add UNIQUE constraint on the created index.
491
     */
492 6
    public function createIndex($name, $table, $columns, $unique = false)
493
    {
494 6
        $time = $this->beginCommand('create' . ($unique ? ' unique' : '') . " index $name on $table (" . implode(',', (array) $columns) . ')');
495 6
        $this->db->createCommand()->createIndex($name, $table, $columns, $unique)->execute();
496 6
        $this->endCommand($time);
497 6
    }
498
499
    /**
500
     * Builds and executes a SQL statement for dropping an index.
501
     * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
502
     * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
503
     */
504
    public function dropIndex($name, $table)
505
    {
506
        $time = $this->beginCommand("drop index $name on $table");
507
        $this->db->createCommand()->dropIndex($name, $table)->execute();
508
        $this->endCommand($time);
509
    }
510
511
    /**
512
     * Builds and execute a SQL statement for adding comment to column.
513
     *
514
     * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
515
     * @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
516
     * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
517
     * @since 2.0.8
518
     */
519
    public function addCommentOnColumn($table, $column, $comment)
520
    {
521
        $time = $this->beginCommand("add comment on column $column");
522
        $this->db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();
523
        $this->endCommand($time);
524
    }
525
526
    /**
527
     * Builds a SQL statement for adding comment to table.
528
     *
529
     * @param string $table the table to be commented. The table name will be properly quoted by the method.
530
     * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
531
     * @since 2.0.8
532
     */
533
    public function addCommentOnTable($table, $comment)
534
    {
535
        $time = $this->beginCommand("add comment on table $table");
536
        $this->db->createCommand()->addCommentOnTable($table, $comment)->execute();
537
        $this->endCommand($time);
538
    }
539
540
    /**
541
     * Builds and execute a SQL statement for dropping comment from column.
542
     *
543
     * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
544
     * @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
545
     * @since 2.0.8
546
     */
547
    public function dropCommentFromColumn($table, $column)
548
    {
549
        $time = $this->beginCommand("drop comment from column $column");
550
        $this->db->createCommand()->dropCommentFromColumn($table, $column)->execute();
551
        $this->endCommand($time);
552
    }
553
554
    /**
555
     * Builds a SQL statement for dropping comment from table.
556
     *
557
     * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
558
     * @since 2.0.8
559
     */
560
    public function dropCommentFromTable($table)
561
    {
562
        $time = $this->beginCommand("drop comment from table $table");
563
        $this->db->createCommand()->dropCommentFromTable($table)->execute();
564
        $this->endCommand($time);
565
    }
566
567
    /**
568
     * Prepares for a command to be executed, and outputs to the console.
569
     *
570
     * @param string $description the description for the command, to be output to the console.
571
     * @return float the time before the command is executed, for the time elapsed to be calculated.
572
     * @since 2.0.13
573
     */
574 30
    protected function beginCommand($description)
575
    {
576 30
        if (!$this->compact) {
577 30
            echo "    > $description ...";
578
        }
579 30
        return microtime(true);
580
    }
581
582
    /**
583
     * Finalizes after the command has been executed, and outputs to the console the time elapsed.
584
     *
585
     * @param float $time the time before the command was executed.
586
     * @since 2.0.13
587
     */
588 30
    protected function endCommand($time)
589
    {
590 30
        if (!$this->compact) {
591 30
            echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
592
        }
593 30
    }
594
}
595