PHP Class yii\console\controllers\MigrateController

A migration means a set of persistent changes to the application environment that is shared among different developers. For example, in an application backed by a database, a migration may refer to a set of changes to the database, such as creating a new table, adding a new table column. This command provides support for tracking the migration history, upgrading or downloading with migrations, and creating new migration skeletons. The migration history is stored in a database table named as [[migrationTable]]. The table will be automatically created the first time this command is executed, if it does not exist. You may also manually create it as follows: ~~~ CREATE TABLE migration ( version varchar(180) PRIMARY KEY, apply_time integer ) ~~~ Below are some common usages of this command: ~~~ # creates a new migration named 'create_user_table' yii migrate/create create_user_table # applies ALL new migrations yii migrate # reverts the last applied migration yii migrate/down ~~~
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends BaseMigrateController
ファイルを表示 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$db the DB connection object or the application component ID of the DB connection to use when applying migrations. Starting from version 2.0.3, this can also be a configuration array for creating the object.
$fields column definition strings used for creating migration code. The format of each definition is COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR. Delimiter is ,. For example, --fields="name:string(12):notNull:unique" produces a string column of size 12 which is not null and unique values. Note: primary key is added automatically and is named id by default. If you want to use another name you may specify it explicitly like --fields="id_key:primaryKey,name:string(12):notNull:unique"
$generatorTemplateFiles a set of template paths for generating migration code automatically. The key is the template type, the value is a path or the alias. Supported types are: - create_table: table creating template - drop_table: table dropping template - add_column: adding new column template - drop_column: dropping column template - create_junction: create junction template
$migrationTable the name of the table for keeping applied migration information.
$templateFile
$useTablePrefix indicates whether the table names generated should consider the tablePrefix setting of the DB connection. For example, if the table name is post the generator wil return {{%post}}.

Public Methods

Method Description
beforeAction ( Action $action ) : boolean This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the [[migrationPath]].
optionAliases ( )
options ( $actionID )

Protected Methods

Method Description
addDefaultPrimaryKey ( array &$fields ) Adds default primary key to fields list if there's no primary key specified
addMigrationHistory ( $version )
createMigration ( string $class ) : Migration Creates a new migration instance.
createMigrationHistoryTable ( ) Creates the migration history table.
generateMigrationSourceCode ( $params )
generateTableName ( string $tableName ) : string If useTablePrefix equals true, then the table name will contain the prefix format.
getMigrationHistory ( $limit )
parseFields ( ) : array Parse the command line migration fields
removeMigrationHistory ( $version )

Method Details

addDefaultPrimaryKey() protected method

Adds default primary key to fields list if there's no primary key specified
Since: 2.0.7
protected addDefaultPrimaryKey ( array &$fields )
$fields array parsed fields

addMigrationHistory() protected method

protected addMigrationHistory ( $version )

beforeAction() public method

This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the [[migrationPath]].
public beforeAction ( Action $action ) : boolean
$action yii\base\Action the action to be executed.
return boolean whether the action should continue to be executed.

createMigration() protected method

Creates a new migration instance.
protected createMigration ( string $class ) : Migration
$class string the migration class name
return yii\db\Migration the migration instance

createMigrationHistoryTable() protected method

Creates the migration history table.

generateMigrationSourceCode() protected method

Since: 2.0.8
protected generateMigrationSourceCode ( $params )

generateTableName() protected method

If useTablePrefix equals true, then the table name will contain the prefix format.
Since: 2.0.8
protected generateTableName ( string $tableName ) : string
$tableName string the table name to generate.
return string

getMigrationHistory() protected method

protected getMigrationHistory ( $limit )

optionAliases() public method

Since: 2.0.8
public optionAliases ( )

options() public method

public options ( $actionID )

parseFields() protected method

Parse the command line migration fields
Since: 2.0.7
protected parseFields ( ) : array
return array parse result with following fields: - fields: array, parsed fields - foreignKeys: array, detected foreign keys

removeMigrationHistory() protected method

protected removeMigrationHistory ( $version )

Property Details

$db public_oe property

the DB connection object or the application component ID of the DB connection to use when applying migrations. Starting from version 2.0.3, this can also be a configuration array for creating the object.
public $db

$fields public_oe property

column definition strings used for creating migration code. The format of each definition is COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR. Delimiter is ,. For example, --fields="name:string(12):notNull:unique" produces a string column of size 12 which is not null and unique values. Note: primary key is added automatically and is named id by default. If you want to use another name you may specify it explicitly like --fields="id_key:primaryKey,name:string(12):notNull:unique"
Since: 2.0.7
public $fields

$generatorTemplateFiles public_oe property

a set of template paths for generating migration code automatically. The key is the template type, the value is a path or the alias. Supported types are: - create_table: table creating template - drop_table: table dropping template - add_column: adding new column template - drop_column: dropping column template - create_junction: create junction template
Since: 2.0.7
public $generatorTemplateFiles

$migrationTable public_oe property

the name of the table for keeping applied migration information.
public $migrationTable

$templateFile public_oe property

public $templateFile

$useTablePrefix public_oe property

indicates whether the table names generated should consider the tablePrefix setting of the DB connection. For example, if the table name is post the generator wil return {{%post}}.
Since: 2.0.8
public $useTablePrefix