# Migration

> Migration is the base class for representing a database migration.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › Migration

| class | `yii\db\Migration` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`MigrationInterface`](/docs/api/yii/db/migrationinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Uses traits | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| Since | 2.0 |
| Source | [`framework/db/Migration.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/Migration.php#L47) |

Migration is the base class for representing a database migration.

Migration is designed to be used together with the "yii migrate" command.

Each child class of Migration represents an individual database migration which
is identified by the child class name.

Within each migration, the [`up()`](/docs/api/yii/db/migration/index.md#method-up) method should be overridden to contain the logic
for "upgrading" the database; while the [`down()`](/docs/api/yii/db/migration/index.md#method-down) method for the "downgrading"
logic. The "yii migrate" command manages all available migrations in an application.

If the database supports transactions, you may also override [`safeUp()`](/docs/api/yii/db/migration/index.md#method-safeUp) and
[`safeDown()`](/docs/api/yii/db/migration/index.md#method-safeDown) so that if anything wrong happens during the upgrading or downgrading,
the whole migration can be reverted in a whole.

Note that some DB queries in some DBMS cannot be put into a transaction. For some examples,
please refer to [implicit commit](https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case,
you should still implement `up()` and `down()`, instead.

Migration provides a set of convenient methods for manipulating database data and schema.
For example, the [`insert()`](/docs/api/yii/db/migration/index.md#method-insert) method can be used to easily insert a row of data into
a database table; the [`createTable()`](/docs/api/yii/db/migration/index.md#method-createTable) method can be used to create a database table.
Compared with the same methods in [`Command`](/docs/api/yii/db/command/index.md), these methods will display extra
information showing the method parameters and execution time, which may be useful when
applying migrations.

For more details and usage information on Migration, see the [guide article on Migration](/docs/guide/db-migrations/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$compact`](#property-compact) | `bool` | Indicates whether the console output should be compacted. Default: `false`. Available since 2.0.13. | `Migration` |
| [`$db`](#property-db) | [`Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection that this migration should work with. Default: `'db'`. | `Migration` |
| [`$maxSqlOutputLength`](#property-maxSqlOutputLength) | `int` | Max number of characters of the SQL outputted. Default: `null`. Available since 2.0.13. | `Migration` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`addCheck()`](#method-addCheck) | Creates a SQL command for adding a check constraint to an existing table. | `Migration` |
| [`addColumn()`](#method-addColumn) | Builds and executes a SQL statement for adding a new DB column. | `Migration` |
| [`addCommentOnColumn()`](#method-addCommentOnColumn) | Builds and execute a SQL statement for adding comment to column. | `Migration` |
| [`addCommentOnTable()`](#method-addCommentOnTable) | Builds a SQL statement for adding comment to table. | `Migration` |
| [`addForeignKey()`](#method-addForeignKey) | Builds a SQL statement for adding a foreign key constraint to an existing table. | `Migration` |
| [`addPrimaryKey()`](#method-addPrimaryKey) | Builds and executes a SQL statement for creating a primary key. | `Migration` |
| [`alterColumn()`](#method-alterColumn) | Builds and executes a SQL statement for changing the definition of a column. | `Migration` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`batchInsert()`](#method-batchInsert) | Creates and executes a batch INSERT SQL statement. | `Migration` |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-bigInteger"></span>[`bigInteger()`](/docs/api/yii/db/schemabuildertrait/index.md#method-bigInteger) | Creates a bigint column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-bigPrimaryKey"></span>[`bigPrimaryKey()`](/docs/api/yii/db/schemabuildertrait/index.md#method-bigPrimaryKey) | Creates a big primary key column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-binary"></span>[`binary()`](/docs/api/yii/db/schemabuildertrait/index.md#method-binary) | Creates a binary column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-boolean"></span>[`boolean()`](/docs/api/yii/db/schemabuildertrait/index.md#method-boolean) | Creates a boolean column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-char"></span>[`char()`](/docs/api/yii/db/schemabuildertrait/index.md#method-char) | Creates a char column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`createIndex()`](#method-createIndex) | Builds and executes a SQL statement for creating a new index. | `Migration` |
| [`createTable()`](#method-createTable) | Builds and executes a SQL statement for creating a new DB table. | `Migration` |
| <span id="method-date"></span>[`date()`](/docs/api/yii/db/schemabuildertrait/index.md#method-date) | Creates a date column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-dateTime"></span>[`dateTime()`](/docs/api/yii/db/schemabuildertrait/index.md#method-dateTime) | Creates a datetime column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-decimal"></span>[`decimal()`](/docs/api/yii/db/schemabuildertrait/index.md#method-decimal) | Creates a decimal column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| [`delete()`](#method-delete) | Creates and executes a DELETE SQL statement. | `Migration` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-double"></span>[`double()`](/docs/api/yii/db/schemabuildertrait/index.md#method-double) | Creates a double column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| [`down()`](#method-down) | This method contains the logic to be executed when removing this migration. | `Migration` |
| [`dropCheck()`](#method-dropCheck) | Creates a SQL command for dropping a check constraint. | `Migration` |
| [`dropColumn()`](#method-dropColumn) | Builds and executes a SQL statement for dropping a DB column. | `Migration` |
| [`dropCommentFromColumn()`](#method-dropCommentFromColumn) | Builds and execute a SQL statement for dropping comment from column. | `Migration` |
| [`dropCommentFromTable()`](#method-dropCommentFromTable) | Builds a SQL statement for dropping comment from table. | `Migration` |
| [`dropForeignKey()`](#method-dropForeignKey) | Builds a SQL statement for dropping a foreign key constraint. | `Migration` |
| [`dropIndex()`](#method-dropIndex) | Builds and executes a SQL statement for dropping an index. | `Migration` |
| [`dropPrimaryKey()`](#method-dropPrimaryKey) | Builds and executes a SQL statement for dropping a primary key. | `Migration` |
| [`dropTable()`](#method-dropTable) | Builds and executes a SQL statement for dropping a DB table. | `Migration` |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`execute()`](#method-execute) | Executes a SQL statement. | `Migration` |
| <span id="method-float"></span>[`float()`](/docs/api/yii/db/schemabuildertrait/index.md#method-float) | Creates a float column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the migration. | `Migration` |
| [`insert()`](#method-insert) | Creates and executes an INSERT SQL statement. | `Migration` |
| <span id="method-integer"></span>[`integer()`](/docs/api/yii/db/schemabuildertrait/index.md#method-integer) | Creates an integer column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-json"></span>[`json()`](/docs/api/yii/db/schemabuildertrait/index.md#method-json) | Creates a JSON column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-money"></span>[`money()`](/docs/api/yii/db/schemabuildertrait/index.md#method-money) | Creates a money column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-primaryKey"></span>[`primaryKey()`](/docs/api/yii/db/schemabuildertrait/index.md#method-primaryKey) | Creates a primary key column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| [`renameColumn()`](#method-renameColumn) | Builds and executes a SQL statement for renaming a column. | `Migration` |
| [`renameTable()`](#method-renameTable) | Builds and executes a SQL statement for renaming a DB table. | `Migration` |
| [`safeDown()`](#method-safeDown) | This method contains the logic to be executed when removing this migration. | `Migration` |
| [`safeUp()`](#method-safeUp) | This method contains the logic to be executed when applying this migration. | `Migration` |
| <span id="method-smallInteger"></span>[`smallInteger()`](/docs/api/yii/db/schemabuildertrait/index.md#method-smallInteger) | Creates a smallint column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-string"></span>[`string()`](/docs/api/yii/db/schemabuildertrait/index.md#method-string) | Creates a string column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-text"></span>[`text()`](/docs/api/yii/db/schemabuildertrait/index.md#method-text) | Creates a text column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-time"></span>[`time()`](/docs/api/yii/db/schemabuildertrait/index.md#method-time) | Creates a time column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-timestamp"></span>[`timestamp()`](/docs/api/yii/db/schemabuildertrait/index.md#method-timestamp) | Creates a timestamp column. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-tinyInteger"></span>[`tinyInteger()`](/docs/api/yii/db/schemabuildertrait/index.md#method-tinyInteger) | Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. | [`SchemaBuilderTrait`](/docs/api/yii/db/schemabuildertrait/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`truncateTable()`](#method-truncateTable) | Builds and executes a SQL statement for truncating a DB table. | `Migration` |
| [`up()`](#method-up) | This method contains the logic to be executed when applying this migration. | `Migration` |
| [`update()`](#method-update) | Creates and executes an UPDATE SQL statement. | `Migration` |
| [`upsert()`](#method-upsert) | Creates and executes a command to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do. | `Migration` |
| [`beginCommand()`](#method-beginCommand) protected | Prepares for a command to be executed, and outputs to the console. | `Migration` |
| [`endCommand()`](#method-endCommand) protected | Finalizes after the command has been executed, and outputs to the console the time elapsed. | `Migration` |
| [`getDb()`](#method-getDb) protected |  | `Migration` |

## Property details

### $compact <span id="property-compact"></span>

```php
public bool $compact = false
```

Indicates whether the console output should be compacted.

If this is set to true, the individual commands ran within the migration will not be output to the console.
Default is false, in other words the output is fully verbose by default.

**Since**

2.0.13

### $db <span id="property-db"></span>

```php
public yii\db\Connection|array|string $db = 'db'
```

The DB connection object or the application component ID of the DB connection that this migration should work with.

Starting from version 2.0.2, this can also be a configuration array
for creating the object.

Note that when a Migration object is created by the `migrate` command, this property will be overwritten
by the command. If you do not want to use the DB connection provided by the command, you may override
the [`init()`](/docs/api/yii/db/migration/index.md#method-init) method like the following:

```
public function init()
{
    $this->db = 'db2';
    parent::init();
}
```

### $maxSqlOutputLength <span id="property-maxSqlOutputLength"></span>

```php
public int $maxSqlOutputLength = null
```

Max number of characters of the SQL outputted.

Useful for reduction of long statements and making
console output more compact.

**Since**

2.0.13

## Method details

### addCheck() <span id="method-addCheck"></span>

```php
public function addCheck(string $name, string $table, string $expression): mixed
```

Creates a SQL command for adding a check constraint to an existing table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the check constraint. The name will be properly quoted by the method. |
| `$table` | `string` | The table that the check constraint will be added to. The name will be properly quoted by the method. |
| `$expression` | `string` | The SQL of the `CHECK` constraint. |

**Returns**

`mixed`

### addColumn() <span id="method-addColumn"></span>

```php
public function addColumn(string $table, string $column, string $type): mixed
```

Builds and executes a SQL statement for adding a new DB column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table that the new column will be added to. The table name will be properly quoted by the method. |
| `$column` | `string` | The name of the new column. The name will be properly quoted by the method. |
| `$type` | `string` | The column type. The [`QueryBuilder::getColumnType()`](/docs/api/yii/db/querybuilder/index.md#method-getColumnType) method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |

**Returns**

`mixed`

### addCommentOnColumn() <span id="method-addCommentOnColumn"></span>

```php
public function addCommentOnColumn(string $table, string $column, string $comment): mixed
```

Builds and execute a SQL statement for adding comment to column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be commented. The table name will be properly quoted by the method. |
| `$column` | `string` | The name of the column to be commented. The column name will be properly quoted by the method. |
| `$comment` | `string` | The text of the comment to be added. The comment will be properly quoted by the method. |

**Returns**

`mixed`

**Since**

2.0.8

### addCommentOnTable() <span id="method-addCommentOnTable"></span>

```php
public function addCommentOnTable(string $table, string $comment): mixed
```

Builds a SQL statement for adding comment to table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table to be commented. The table name will be properly quoted by the method. |
| `$comment` | `string` | The text of the comment to be added. The comment will be properly quoted by the method. |

**Returns**

`mixed`

**Since**

2.0.8

### addForeignKey() <span id="method-addForeignKey"></span>

```php
public function addForeignKey(string $name, string $table, string|array $columns, string $refTable, string|array $refColumns, string|null $delete = null, string|null $update = null): mixed
```

Builds a SQL statement for adding a foreign key constraint to an existing table.

The method will properly quote the table and column names.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the foreign key constraint. |
| `$table` | `string` | The table that the foreign key constraint will be added to. |
| `$columns` | `string\|array` | 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. |
| `$refTable` | `string` | The table that the foreign key references to. |
| `$refColumns` | `string\|array` | The name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. |
| `$delete` | `string\|null` | The ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
| `$update` | `string\|null` | The ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |

**Returns**

`mixed`

### addPrimaryKey() <span id="method-addPrimaryKey"></span>

```php
public function addPrimaryKey(string $name, string $table, string|array $columns): mixed
```

Builds and executes a SQL statement for creating a primary key.

The method will properly quote the table and column names.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the primary key constraint. |
| `$table` | `string` | The table that the primary key constraint will be added to. |
| `$columns` | `string\|array` | Comma separated string or array of columns that the primary key will consist of. |

**Returns**

`mixed`

### alterColumn() <span id="method-alterColumn"></span>

```php
public function alterColumn(string $table, string $column, string $type): mixed
```

Builds and executes a SQL statement for changing the definition of a column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be changed. The table name will be properly quoted by the method. |
| `$column` | `string` | The name of the column to be changed. The name will be properly quoted by the method. |
| `$type` | `string` | The new column type. The [`QueryBuilder::getColumnType()`](/docs/api/yii/db/querybuilder/index.md#method-getColumnType) method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |

**Returns**

`mixed`

### batchInsert() <span id="method-batchInsert"></span>

```php
public function batchInsert(string $table, array $columns, array $rows): mixed
```

Creates and executes a batch INSERT SQL statement.

The method will properly escape the column names, and bind the values to be inserted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table that new rows will be inserted into. |
| `$columns` | `array` | The column names. |
| `$rows` | `array` | The rows to be batch inserted into the table |

**Returns**

`mixed`

### createIndex() <span id="method-createIndex"></span>

```php
public function createIndex(string $name, string $table, string|array $columns, bool $unique = false): mixed
```

Builds and executes a SQL statement for creating a new index.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the index. The name will be properly quoted by the method. |
| `$table` | `string` | The table that the new index will be created for. The table name will be properly quoted by the method. |
| `$columns` | `string\|array` | The column(s) that should be included in the index. If there are multiple columns, please separate them by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that include a left parenthesis "(". |
| `$unique` | `bool` | Whether to add UNIQUE constraint on the created index. |

**Returns**

`mixed`

### createTable() <span id="method-createTable"></span>

```php
public function createTable(string $table, array $columns, string|null $options = null): mixed
```

Builds and executes a SQL statement for creating a new DB table.

The columns in the new  table should be specified as name-definition pairs (e.g. 'name' => 'string'),
where name stands for a column name which will be properly quoted by the method, and definition
stands for the column type which must contain an abstract DB type.

The [`QueryBuilder::getColumnType()`](/docs/api/yii/db/querybuilder/index.md#method-getColumnType) method will be invoked to convert any abstract type into a physical one.

If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
put into the generated SQL.

Example usage:
```
class m200000_000000_create_table_fruits extends \yii\db\Migration
{
    public function safeUp()
    {
         $this->createTable('{{%fruits}}', [
             // ...
             'column_name double precision null default null',
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The name of the table to be created. The name will be properly quoted by the method. |
| `$columns` | `array` | The columns (name => definition) in the new table. |
| `$options` | `string\|null` | Additional SQL fragment that will be appended to the generated SQL. |

**Returns**

`mixed`

### delete() <span id="method-delete"></span>

```php
public function delete(string $table, array|string $condition = '', array $params = []): mixed
```

Creates and executes a DELETE SQL statement.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table where the data will be deleted from. |
| `$condition` | `array\|string` | The conditions that will be put in the WHERE part. Please refer to [`Query::where()`](/docs/api/yii/db/query/index.md#method-where) on how to specify conditions. |
| `$params` | `array` | The parameters to be bound to the query. |

**Returns**

`mixed`

### down() <span id="method-down"></span>

```php
public function down(): false|void|mixed
```

This method contains the logic to be executed when removing this migration.

The default implementation throws an exception indicating the migration cannot be removed.
Child classes may override this method if the corresponding migrations can be removed.

**Returns**

`false|void|mixed`: Return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.

### dropCheck() <span id="method-dropCheck"></span>

```php
public function dropCheck(string $name, string $table): mixed
```

Creates a SQL command for dropping a check constraint.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the check constraint to be dropped. The name will be properly quoted by the method. |
| `$table` | `string` | The table whose check constraint is to be dropped. The name will be properly quoted by the method. |

**Returns**

`mixed`

### dropColumn() <span id="method-dropColumn"></span>

```php
public function dropColumn(string $table, string $column): mixed
```

Builds and executes a SQL statement for dropping a DB column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be dropped. The name will be properly quoted by the method. |
| `$column` | `string` | The name of the column to be dropped. The name will be properly quoted by the method. |

**Returns**

`mixed`

### dropCommentFromColumn() <span id="method-dropCommentFromColumn"></span>

```php
public function dropCommentFromColumn(string $table, string $column): mixed
```

Builds and execute a SQL statement for dropping comment from column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be commented. The table name will be properly quoted by the method. |
| `$column` | `string` | The name of the column to be commented. The column name will be properly quoted by the method. |

**Returns**

`mixed`

**Since**

2.0.8

### dropCommentFromTable() <span id="method-dropCommentFromTable"></span>

```php
public function dropCommentFromTable(string $table): mixed
```

Builds a SQL statement for dropping comment from table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be commented. The table name will be properly quoted by the method. |

**Returns**

`mixed`

**Since**

2.0.8

### dropForeignKey() <span id="method-dropForeignKey"></span>

```php
public function dropForeignKey(string $name, string $table): mixed
```

Builds a SQL statement for dropping a foreign key constraint.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
| `$table` | `string` | The table whose foreign is to be dropped. The name will be properly quoted by the method. |

**Returns**

`mixed`

### dropIndex() <span id="method-dropIndex"></span>

```php
public function dropIndex(string $name, string $table): mixed
```

Builds and executes a SQL statement for dropping an index.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the index to be dropped. The name will be properly quoted by the method. |
| `$table` | `string` | The table whose index is to be dropped. The name will be properly quoted by the method. |

**Returns**

`mixed`

### dropPrimaryKey() <span id="method-dropPrimaryKey"></span>

```php
public function dropPrimaryKey(string $name, string $table): mixed
```

Builds and executes a SQL statement for dropping a primary key.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the primary key constraint to be removed. |
| `$table` | `string` | The table that the primary key constraint will be removed from. |

**Returns**

`mixed`

### dropTable() <span id="method-dropTable"></span>

```php
public function dropTable(string $table): mixed
```

Builds and executes a SQL statement for dropping a DB table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table to be dropped. The name will be properly quoted by the method. |

**Returns**

`mixed`

### execute() <span id="method-execute"></span>

```php
public function execute(string $sql, array $params = []): mixed
```

Executes a SQL statement.

This method executes the specified SQL statement using [`$db`](/docs/api/yii/db/migration/index.md#property-db).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$sql` | `string` | The SQL statement to be executed |
| `$params` | `array` | Input parameters (name => value) for the SQL execution. See [`Command::execute()`](/docs/api/yii/db/command/index.md#method-execute) for more details. |

**Returns**

`mixed`

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the migration.

This method will set [`$db`](/docs/api/yii/db/migration/index.md#property-db) to be the 'db' application component, if it is `null`.

**Returns**

`mixed`

### insert() <span id="method-insert"></span>

```php
public function insert(string $table, array $columns): mixed
```

Creates and executes an INSERT SQL statement.

The method will properly escape the column names, and bind the values to be inserted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table that new rows will be inserted into. |
| `$columns` | `array` | The column data (name => value) to be inserted into the table. |

**Returns**

`mixed`

### renameColumn() <span id="method-renameColumn"></span>

```php
public function renameColumn(string $table, string $name, string $newName): mixed
```

Builds and executes a SQL statement for renaming a column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table whose column is to be renamed. The name will be properly quoted by the method. |
| `$name` | `string` | The old name of the column. The name will be properly quoted by the method. |
| `$newName` | `string` | The new name of the column. The name will be properly quoted by the method. |

**Returns**

`mixed`

### renameTable() <span id="method-renameTable"></span>

```php
public function renameTable(string $table, string $newName): mixed
```

Builds and executes a SQL statement for renaming a DB table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table to be renamed. The name will be properly quoted by the method. |
| `$newName` | `string` | The new table name. The name will be properly quoted by the method. |

**Returns**

`mixed`

### safeDown() <span id="method-safeDown"></span>

```php
public function safeDown(): false|void|mixed
```

This method contains the logic to be executed when removing this migration.

This method differs from [`down()`](/docs/api/yii/db/migration/index.md#method-down) in that the DB logic implemented here will
be enclosed within a DB transaction.
Child classes may implement this method instead of [`down()`](/docs/api/yii/db/migration/index.md#method-down) if the DB logic
needs to be within a transaction.

Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
please refer to [implicit commit](https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).

**Returns**

`false|void|mixed`: Return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.

### safeUp() <span id="method-safeUp"></span>

```php
public function safeUp(): false|void|mixed
```

This method contains the logic to be executed when applying this migration.

This method differs from [`up()`](/docs/api/yii/db/migration/index.md#method-up) in that the DB logic implemented here will
be enclosed within a DB transaction.
Child classes may implement this method instead of [`up()`](/docs/api/yii/db/migration/index.md#method-up) if the DB logic
needs to be within a transaction.

Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
please refer to [implicit commit](https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html).

**Returns**

`false|void|mixed`: Return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.

### truncateTable() <span id="method-truncateTable"></span>

```php
public function truncateTable(string $table): mixed
```

Builds and executes a SQL statement for truncating a DB table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table to be truncated. The name will be properly quoted by the method. |

**Returns**

`mixed`

### up() <span id="method-up"></span>

```php
public function up(): false|void|mixed
```

This method contains the logic to be executed when applying this migration.

Child classes may override this method to provide actual migration logic.

**Returns**

`false|void|mixed`: Return a false value to indicate the migration fails and should not proceed further. All other return values mean the migration succeeds.

### update() <span id="method-update"></span>

```php
public function update(string $table, array $columns, array|string $condition = '', array $params = []): mixed
```

Creates and executes an UPDATE SQL statement.

The method will properly escape the column names and bind the values to be updated.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table to be updated. |
| `$columns` | `array` | The column data (name => value) to be updated. |
| `$condition` | `array\|string` | The conditions that will be put in the WHERE part. Please refer to [`Query::where()`](/docs/api/yii/db/query/index.md#method-where) on how to specify conditions. |
| `$params` | `array` | The parameters to be bound to the query. |

**Returns**

`mixed`

### upsert() <span id="method-upsert"></span>

```php
public function upsert(string $table, array|yii\db\Query $insertColumns, array|bool $updateColumns = true, array $params = []): mixed
```

Creates and executes a command to insert rows into a database table if
they do not already exist (matching unique constraints),
or update them if they do.

The method will properly escape the column names, and bind the values to be inserted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table that new rows will be inserted into/updated in. |
| `$insertColumns` | `array\|`[`Query`](/docs/api/yii/db/query/index.md) | The column data (name => value) to be inserted into the table or instance of [`Query`](/docs/api/yii/db/query/index.md) to perform `INSERT INTO ... SELECT` SQL statement. |
| `$updateColumns` | `array\|bool` | The column data (name => value) to be updated if they already exist. If `true` is passed, the column data will be updated to match the insert column data. If `false` is passed, no update will be performed if the column data already exists. |
| `$params` | `array` | The parameters to be bound to the command. |

**Returns**

`mixed`

**Since**

2.0.14

### beginCommand() <span id="method-beginCommand"></span>

```php
protected function beginCommand(string $description): float
```

Prepares for a command to be executed, and outputs to the console.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$description` | `string` | The description for the command, to be output to the console. |

**Returns**

`float`: The time before the command is executed, for the time elapsed to be calculated.

**Since**

2.0.13

### endCommand() <span id="method-endCommand"></span>

```php
protected function endCommand(float $time): mixed
```

Finalizes after the command has been executed, and outputs to the console the time elapsed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$time` | `float` | The time before the command was executed. |

**Returns**

`mixed`

**Since**

2.0.13

### getDb() <span id="method-getDb"></span>

```php
protected function getDb()
```

**Since**

2.0.6