# Abstract Class yii\console\controllers\BaseMigrateController

Inheritance | yii\console\controllers\BaseMigrateController » yii\console\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable, yii\base\ViewContextInterface
Subclasses | yii\console\controllers\MigrateController, yii\mongodb\console\controllers\MigrateController
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/console/controllers/BaseMigrateController.php

BaseMigrateController is the base class for migrate controllers.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$action | yii\base\Action|null | The action that is currently being executed. | yii\base\Controller
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$color | boolean|null | Whether to enable ANSI color in the output. | yii\console\Controller
$compact | boolean | Indicates whether the console output should be compacted. | yii\console\controllers\BaseMigrateController
$defaultAction | string | The default command action. | yii\console\controllers\BaseMigrateController
$help | boolean | Whether to display help information about current command. | yii\console\Controller
$helpSummary | string | This property is read-only. | yii\console\Controller
$id | string | The ID of this controller. | yii\base\Controller
$interactive | boolean | Whether to run the command interactively. | yii\console\Controller
$layout | null|string|false | The name of the layout to be applied to this controller's views. | yii\base\Controller
$migrationNamespaces | array | List of namespaces containing the migration classes. | yii\console\controllers\BaseMigrateController
$migrationPath | string|array | The directory containing the migration classes. | yii\console\controllers\BaseMigrateController
$module | yii\base\Module | The module that this controller belongs to. | yii\base\Controller
$modules | yii\base\Module[] | All ancestor modules that this controller is located within. This property is read-only. | yii\base\Controller
$passedOptionValues | array | The properties corresponding to the passed options. This property is read-only. | yii\console\Controller
$passedOptions | array | The names of the options passed during execution. This property is read-only. | yii\console\Controller
$request | yii\console\Request |  | yii\console\Controller
$response | yii\console\Response |  | yii\console\Controller
$route | string | The route (module ID, controller ID and action ID) of the current request. This property is read-only. | yii\base\Controller
$silentExitOnException | boolean|null | If true - script finish with `ExitCode::OK` in case of exception. | yii\console\Controller
$templateFile | string | The template file for generating new migrations. | yii\console\controllers\BaseMigrateController
$uniqueId | string | The controller ID that is prefixed with the module ID (if any). This property is read-only. | yii\base\Controller
$view | yii\base\View|yii\web\View | The view object that can be used to render views or view files. | yii\base\Controller
$viewPath | string | The directory containing the view files for this controller. | yii\base\Controller

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component
__construct() |  | yii\base\Controller
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
actionCreate() | Creates a new migration. | yii\console\controllers\BaseMigrateController
actionDown() | Downgrades the application by reverting old migrations. | yii\console\controllers\BaseMigrateController
actionFresh() | Drops all tables and related constraints. Starts the migration from the beginning. | yii\console\controllers\BaseMigrateController
actionHistory() | Displays the migration history. | yii\console\controllers\BaseMigrateController
actionMark() | Modifies the migration history to the specified version. | yii\console\controllers\BaseMigrateController
actionNew() | Displays the un-applied new migrations. | yii\console\controllers\BaseMigrateController
actionRedo() | Redoes the last few migrations. | yii\console\controllers\BaseMigrateController
actionTo() | Upgrades or downgrades till the specified version. | yii\console\controllers\BaseMigrateController
actionUp() | Upgrades the application by applying new migrations. | yii\console\controllers\BaseMigrateController
actions() | Declares external actions for the controller. | yii\base\Controller
afterAction() | This method is invoked right after an action is executed. | yii\base\Controller
ansiFormat() | Formats a string with ANSI codes. | yii\console\Controller
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the $migrationPath. | yii\console\controllers\BaseMigrateController
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
bindActionParams() | Binds the parameters to the action. | yii\console\Controller
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
confirm() | Asks user to confirm by typing y or n. | yii\console\Controller
createAction() | Creates an action based on the given action ID. | yii\base\Controller
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
findLayoutFile() | Finds the applicable layout file. | yii\base\Controller
getActionArgsHelp() | Returns the help information for the anonymous arguments for the action. | yii\console\Controller
getActionHelp() | Returns the detailed help information for the specified action. | yii\console\Controller
getActionHelpSummary() | Returns a one-line short summary describing the specified action. | yii\console\Controller
getActionOptionsHelp() | Returns the help information for the options for the action. | yii\console\Controller
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
getHelp() | Returns help information for this controller. | yii\console\Controller
getHelpSummary() | Returns one-line short summary describing this controller. | yii\console\Controller
getModules() | Returns all ancestor modules of this controller. | yii\base\Controller
getOptionValues() | Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties. | yii\console\Controller
getPassedOptionValues() | Returns the properties corresponding to the passed options. | yii\console\Controller
getPassedOptions() | Returns the names of valid options passed during execution. | yii\console\Controller
getRoute() | Returns the route of the current request. | yii\base\Controller
getUniqueId() | Returns the unique ID of the controller. | yii\base\Controller
getView() | Returns the view object that can be used to render views or view files. | yii\base\Controller
getViewPath() |  | yii\base\ViewContextInterface
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
init() | Initializes the object. | yii\base\Controller
isColorEnabled() | Returns a value indicating whether ANSI color is enabled. | yii\console\Controller
off() | Detaches an existing event handler from this component. | yii\base\Component
on() | Attaches an event handler to an event. | yii\base\Component
optionAliases() | Returns option alias names. | yii\console\Controller
options() | Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name. | yii\console\controllers\BaseMigrateController
prompt() | Prompts the user for input and validates it. | yii\console\Controller
render() | Renders a view and applies layout if available. | yii\base\Controller
renderContent() | Renders a static string by applying a layout. | yii\base\Controller
renderFile() | Renders a view file. | yii\base\Controller
renderPartial() | Renders a view without applying layout. | yii\base\Controller
run() | Runs a request specified in terms of a route. | yii\base\Controller
runAction() | Runs an action with the specified action ID and parameters. | yii\console\Controller
select() | Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations. | yii\console\Controller
setView() | Sets the view object to be used by this controller. | yii\base\Controller
setViewPath() | Sets the directory that contains the view files. | yii\base\Controller
stderr() | Prints a string to STDERR. | yii\console\Controller
stdout() | Prints a string to STDOUT. | yii\console\Controller
trigger() | Triggers an event. | yii\base\Component

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
addMigrationHistory() | Adds new migration entry to the history. | yii\console\controllers\BaseMigrateController
bindInjectedParams() | Fills parameters based on types and names in action method signature. | yii\base\Controller
createMigration() | Creates a new migration instance. | yii\console\controllers\BaseMigrateController
generateMigrationSourceCode() | Generates new migration source PHP code. | yii\console\controllers\BaseMigrateController
getActionMethodReflection() |  | yii\console\Controller
getMigrationHistory() | Returns the migration history. | yii\console\controllers\BaseMigrateController
getMigrationNameLimit() | Return the maximum name length for a migration. | yii\console\controllers\BaseMigrateController
getNewMigrations() | Returns the migrations that are not applied. | yii\console\controllers\BaseMigrateController
includeMigrationFile() | Includes the migration file for a given migration class name. | yii\console\controllers\BaseMigrateController
migrateDown() | Downgrades with the specified migration class. | yii\console\controllers\BaseMigrateController
migrateToTime() | Migrates to the specified apply time in the past. | yii\console\controllers\BaseMigrateController
migrateToVersion() | Migrates to the certain version. | yii\console\controllers\BaseMigrateController
migrateUp() | Upgrades with the specified migration class. | yii\console\controllers\BaseMigrateController
parseDocCommentDetail() | Returns full description from the docblock. | yii\console\Controller
parseDocCommentSummary() | Returns the first line of docblock. | yii\console\Controller
parseDocCommentTags() | Parses the comment block into tags. | yii\console\Controller
removeMigrationHistory() | Removes existing migration from the history. | yii\console\controllers\BaseMigrateController
truncateDatabase() | Truncates the database. | yii\console\controllers\BaseMigrateController

## Events

Hide inherited events

Event | Type | Description | Defined By
--- | --- | --- | ---
EVENT_AFTER_ACTION | yii\base\ActionEvent | An event raised right after executing a controller action. | yii\base\Controller
EVENT_BEFORE_ACTION | yii\base\ActionEvent | An event raised right before executing a controller action. | yii\base\Controller

## Constants

Hide inherited constants

Constant | Value | Description | Defined By
--- | --- | --- | ---
BASE_MIGRATION | 'm000000_000000_base' | The name of the dummy migration that marks the beginning of the whole migration history. | yii\console\controllers\BaseMigrateController
EXIT_CODE_ERROR | 1 | Deprecated since 2.0.13. Use yii\console\ExitCode::UNSPECIFIED_ERROR instead. | yii\console\Controller
EXIT_CODE_NORMAL | 0 | Deprecated since 2.0.13. Use yii\console\ExitCode::OK instead. | yii\console\Controller

## Property Details

$compact public property (available since version 2.0.13)

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.

public boolean $compact = false
$defaultAction public property

The default command action.

public string $defaultAction = 'up'
$migrationNamespaces public property (available since version 2.0.10)

List of namespaces containing the migration classes.

Migration namespaces should be resolvable as a path alias if prefixed with `@`, e.g. if you specify the namespace `app\migrations`, the code `Yii::getAlias('@app/migrations')` should be able to return the file path to the directory this namespace refers to. This corresponds with the autoloading conventions of Yii.

For example:

    [
        'app\migrations',
        'some\extension\migrations',
    ]

See also $migrationPath.

public array $migrationNamespaces = []
$migrationPath public property

The directory containing the migration classes. This can be either a path alias or a directory path.

Migration classes located at this path should be declared without a namespace. Use $migrationNamespaces property in case you are using namespaced migrations.

If you have set up $migrationNamespaces, you may set this field to `null` in order to disable usage of migrations that are not namespaced.

Since version 2.0.12 you may also specify an array of migration paths that should be searched for migrations to load. This is mainly useful to support old extensions that provide migrations without namespace and to adopt the new feature of namespaced migrations while keeping existing migrations.

In general, to load migrations from different locations, $migrationNamespaces is the preferable solution as the migration name contains the origin of the migration in the history, which is not the case when using multiple migration paths.

See also $migrationNamespaces.

public string|array $migrationPath = ['@app/migrations']
$templateFile public property

The template file for generating new migrations. This can be either a path alias (e.g. "@app/migrations/template.php") or a file path.

public string $templateFile = null

## Method Details

actionCreate() public method

Creates a new migration.

This command creates a new migration using the available migration template. After using this command, developers should modify the created migration skeleton by filling up the actual migration logic.

    yii migrate/create create_user_table

In order to generate a namespaced migration, you should specify a namespace before the migration's name. Note that backslash (`\`) is usually considered a special character in the shell, so you need to escape it properly to avoid shell errors or incorrect behavior. For example:

    yii migrate/create 'app\\migrations\\createUserTable'

In case $migrationPath is not set and no namespace is provided, the first entry of $migrationNamespaces will be used.

public void actionCreate ( $name )
$name | string | The name of the new migration. This should only contain letters, digits, underscores and/or backslashes. Note: If the migration name is of a special form, for example create_xxx or drop_xxx, then the generated migration file will contain extra code, in this case for creating/dropping tables.
throws | yii\console\Exception | if the name argument is invalid.

actionDown() public method

Downgrades the application by reverting old migrations.

For example,

    yii migrate/down     # revert the last migration
    yii migrate/down 3   # revert the last 3 migrations
    yii migrate/down all # revert all migrations

public integer actionDown ( $limit = 1 )
$limit | integer|string | The number of migrations to be reverted. Defaults to 1, meaning the last applied migration will be reverted. When value is "all", all migrations will be reverted.
return | integer | The status of the action execution. 0 means normal, other values mean abnormal.
throws | yii\console\Exception | if the number of the steps specified is less than 1.

actionFresh() public method (available since version 2.0.13)

Drops all tables and related constraints. Starts the migration from the beginning.

    yii migrate/fresh

public void actionFresh ( )

actionHistory() public method

Displays the migration history.

This command will show the list of migrations that have been applied so far. For example,

    yii migrate/history     # showing the last 10 migrations
    yii migrate/history 5   # showing the last 5 migrations
    yii migrate/history all # showing the whole history

public void actionHistory ( $limit = 10 )
$limit | integer|string | The maximum number of migrations to be displayed. If it is "all", the whole migration history will be displayed.
throws | yii\console\Exception | if invalid limit value passed

actionMark() public method

Modifies the migration history to the specified version.

No actual migration will be performed.

    yii migrate/mark 101129_185401                        # using timestamp
    yii migrate/mark m101129_185401_create_user_table     # using full name
    yii migrate/mark app\migrations\M101129185401CreateUser # using full namespace name
    yii migrate/mark m000000_000000_base # reset the complete migration history

public integer actionMark ( $version )
$version | string | The version at which the migration history should be marked. This can be either the timestamp or the full name of the migration. You may specify the name `m000000_000000_base` to set the migration history to a state where no migration has been applied.
return | integer | CLI exit code
throws | yii\console\Exception | if the version argument is invalid or the version cannot be found.

actionNew() public method

Displays the un-applied new migrations.

This command will show the new migrations that have not been applied. For example,

    yii migrate/new     # showing the first 10 new migrations
    yii migrate/new 5   # showing the first 5 new migrations
    yii migrate/new all # showing all new migrations

public void actionNew ( $limit = 10 )
$limit | integer|string | The maximum number of new migrations to be displayed. If it is `all`, all available new migrations will be displayed.
throws | yii\console\Exception | if invalid limit value passed

actionRedo() public method

Redoes the last few migrations.

This command will first revert the specified migrations, and then apply them again. For example,

    yii migrate/redo     # redo the last applied migration
    yii migrate/redo 3   # redo the last 3 applied migrations
    yii migrate/redo all # redo all migrations

public integer actionRedo ( $limit = 1 )
$limit | integer|string | The number of migrations to be redone. Defaults to 1, meaning the last applied migration will be redone. When equals "all", all migrations will be redone.
return | integer | The status of the action execution. 0 means normal, other values mean abnormal.
throws | yii\console\Exception | if the number of the steps specified is less than 1.

actionTo() public method

Upgrades or downgrades till the specified version.

Can also downgrade versions to the certain apply time in the past by providing a UNIX timestamp or a string parseable by the strtotime() function. This means that all the versions applied after the specified certain time would be reverted.

This command will first revert the specified migrations, and then apply them again. For example,

    yii migrate/to 101129_185401                          # using timestamp
    yii migrate/to m101129_185401_create_user_table       # using full name
    yii migrate/to 1392853618                             # using UNIX timestamp
    yii migrate/to "2014-02-15 13:00:50"                  # using strtotime() parseable string
    yii migrate/to app\migrations\M101129185401CreateUser # using full namespace name

public void actionTo ( $version )
$version | string | Either the version name or the certain time value in the past that the application should be migrated to. This can be either the timestamp, the full name of the migration, the UNIX timestamp, or the parseable datetime string.
throws | yii\console\Exception | if the version argument is invalid.

actionUp() public method

Upgrades the application by applying new migrations.

For example,

    yii migrate     # apply all new migrations
    yii migrate 3   # apply the first 3 new migrations

public integer actionUp ( $limit = 0 )
$limit | integer | The number of new migrations to be applied. If 0, it means applying all available new migrations.
return | integer | The status of the action execution. 0 means normal, other values mean abnormal.

addMigrationHistory() protected abstract method

Adds new migration entry to the history.

protected abstract void addMigrationHistory ( $version )
$version | string | Migration version name.

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 boolean beforeAction ( $action )
$action | yii\base\Action | The action to be executed.
return | boolean | Whether the action should continue to be executed.
throws | yii\base\InvalidConfigException | if directory specified in migrationPath doesn't exist and action isn't "create".

createMigration() protected method

Creates a new migration instance.

protected yii\db\MigrationInterface createMigration ( $class )
$class | string | The migration class name
return | yii\db\MigrationInterface | The migration instance

generateMigrationSourceCode() protected method (available since version 2.0.8)

Generates new migration source PHP code.

Child class may override this method, adding extra logic or variation to the process.

protected string generateMigrationSourceCode ( $params )
$params | array | Generation parameters, usually following parameters are present: name: string migration base name className: string migration class name
return | string | Generated PHP code.

getMigrationHistory() protected abstract method

Returns the migration history.

protected abstract array getMigrationHistory ( $limit )
$limit | integer | The maximum number of records in the history to be returned. `null` for "no limit".
return | array | The migration history

getMigrationNameLimit() protected method (available since version 2.0.13)

Return the maximum name length for a migration.

Subclasses may override this method to define a limit.

protected integer|null getMigrationNameLimit ( )
return | integer|null | The maximum name length for a migration or `null` if no limit applies.

getNewMigrations() protected method

Returns the migrations that are not applied.

protected array getNewMigrations ( )
return | array | List of new migrations

includeMigrationFile() protected method (available since version 2.0.12)

Includes the migration file for a given migration class name.

This function will do nothing on namespaced migrations, which are loaded by autoloading automatically. It will include the migration file, by searching $migrationPath for classes without namespace.

protected void includeMigrationFile ( $class )
$class | string | The migration class name.

migrateDown() protected method

Downgrades with the specified migration class.

protected boolean migrateDown ( $class )
$class | string | The migration class name
return | boolean | Whether the migration is successful

migrateToTime() protected method

Migrates to the specified apply time in the past.

protected void migrateToTime ( $time )
$time | integer | UNIX timestamp value.

migrateToVersion() protected method

Migrates to the certain version.

protected integer migrateToVersion ( $version )
$version | string | Name in the full format.
return | integer | CLI exit code
throws | yii\console\Exception | if the provided version cannot be found.

migrateUp() protected method

Upgrades with the specified migration class.

protected boolean migrateUp ( $class )
$class | string | The migration class name
return | boolean | Whether the migration is successful

options() public method

Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.

Child classes may override this method to specify possible options.

Note that the values setting via options are not available until beforeAction() is being called.

public string[] options ( $actionID )
$actionID | string | The action id of the current request
return | string[] | The names of the options valid for the action

removeMigrationHistory() protected abstract method

Removes existing migration from the history.

protected abstract void removeMigrationHistory ( $version )
$version | string | Migration version name.

truncateDatabase() protected method (available since version 2.0.13)

Truncates the database.

This method should be overwritten in subclasses to implement the task of clearing the database.

protected void truncateDatabase ( )
throws | yii\base\NotSupportedException | if not overridden