MigrateController

Type
Class
Namespace
craft\console\controllers
Inherits
craft\console\controllers\MigrateController » yii\console\controllers\BaseMigrateController » yii\console\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable, yii\base\ViewContextInterface
Since
3.0

Manages Craft and plugin migrations.

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 controller provides support for tracking the migration history, updating migrations, and creating new migration skeleton files. The migration history is stored in a database table named migrations. The table will be automatically created the first time this controller is executed, if it does not exist. Below are some common usages of this command:

# creates a new migration named 'create_user_table' for a plugin with the handle pluginHandle.
craft migrate/create create_user_table --plugin=pluginHandle
# applies ALL new migrations for a plugin with the handle pluginHandle
craft migrate up --plugin=pluginHandle

View source

Public Properties

PropertyDescription
$actionyii\base\Action – The action that is currently being executed.
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$colorboolean – Whether to enable ANSI color in the output.
$compactboolean – Indicates whether the console output should be compacted.
$defaultActionstring – The ID of the action that is used when the action ID is not specified in the request.
$helpboolean – Whether to display help information about current command.
$helpSummarystring
$idstring – The ID of this controller.
$interactiveboolean – Whether to run the command interactively.
$layoutnull, string, false – The name of the layout to be applied to this controller's views.
$migrationNamespacesarray – List of namespaces containing the migration classes.
$migrationPathstring, array – The directory containing the migration classes.
$moduleyii\base\Module – The module that this controller belongs to.
$modulesyii\base\Module – All ancestor modules that this controller is located within.
$passedOptionValuesarray – The properties corresponding to the passed options
$passedOptionsarray – The names of the options passed during execution
$pluginstring, craft\base\Plugin, null – The handle of the plugin to use during migration operations, or the plugin itself
$routestring – The route (module ID, controller ID and action ID) of the current request.
$templateFilestring – The template file for generating new migrations.
$typestring, null – The type of migrations we're dealing with here.
$uniqueIdstring – The controller ID that is prefixed with the module ID (if any).
$viewyii\base\View, yii\web\View – The view object that can be used to render views or view files.
$viewPathstring – The directory containing the view files for this controller.

$plugin

Type
string, craft\base\Plugin, null

The handle of the plugin to use during migration operations, or the plugin itself

View source

Signature

public string, craft\base\Plugin, null $plugin = null

$type

Type
string, null

The type of migrations we're dealing with here. Can be 'app', 'plugin', or 'content'.

If --plugin is passed, this will automatically be set to 'plugin'. Otherwise defaults to 'content'.

View source

Signature

public string, null $type = \craft\db\MigrationManager::TYPE_CONTENT

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to null.
actionAll()Runs all pending Craft, plugin, and content migrations.
actionCreate()Creates a new migration.
actionDown()Downgrades the application by reverting old migrations.
actionFresh()Truncates the whole database and starts the migration from the beginning.
actionHistory()Displays the migration history.
actionMark()Modifies the migration history to the specified version.
actionNew()Displays the un-applied new migrations.
actionRedo()Redoes the last few migrations.
actionTo()Upgrades or downgrades till the specified version.
actionUp()Upgrades the application by applying new migrations.
actions()Declares external actions for the controller.
afterAction()This method is invoked right after an action is executed.
ansiFormat()Formats a string with ANSI codes.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the 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.
behaviors()Returns a list of behaviors that this component should behave as.
bindActionParams()Binds the parameters to the action.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
confirm()Asks user to confirm by typing y or n.
createAction()Creates an action based on the given action ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
findLayoutFile()Finds the applicable layout file.
getActionArgsHelp()Returns the help information for the anonymous arguments for the action.
getActionHelp()Returns the detailed help information for the specified action.
getActionHelpSummary()Returns a one-line short summary describing the specified action.
getActionOptionsHelp()Returns the help information for the options for the action.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getHelp()Returns help information for this controller.
getHelpSummary()Returns one-line short summary describing this controller.
getModules()Returns all ancestor modules of this controller.
getOptionValues()Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties.
getPassedOptionValues()Returns the properties corresponding to the passed options.
getPassedOptions()Returns the names of valid options passed during execution.
getRoute()Returns the route of the current request.
getUniqueId()Returns the unique ID of the controller.
getView()Returns the view object that can be used to render views or view files.
getViewPath()Returns the directory containing view files for this controller.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
isColorEnabled()Returns a value indicating whether ANSI color is enabled.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
optionAliases()Returns option alias names.
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.
prompt()Prompts the user for input and validates it.
render()Renders a view and applies layout if available.
renderContent()Renders a static string by applying a layout.
renderFile()Renders a view file.
renderPartial()Renders a view without applying layout.
run()Runs a request specified in terms of a route.
runAction()Runs an action within this controller with the specified action ID and parameters.
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.
setView()Sets the view object to be used by this controller.
setViewPath()Sets the directory that contains the view files.
stderr()Prints a string to STDERR.
stdout()Prints a string to STDOUT.
trigger()Triggers an event.

actionAll()

Runs all pending Craft, plugin, and content migrations.

View source

Returns

integer

Throws

Signature

public integer actionAll ( )

actionCreate()

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.

craft migrate/create create_news_section

By default the migration will be created within the project's migrations/ folder (as a "content migration"). Use --plugin=<plugin-handle> to create a new plugin migration. Use --type=app to create a new Craft CMS app migration.

View source

Arguments

  • $name (string) – The name of the new migration. This should only contain letters, digits, and underscores.

Throws

Signature

public void actionCreate ( $name )

actionUp()

Upgrades the application by applying new migrations.

For example,

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

View source

Arguments

  • $limit (integer) – The number of new migrations to be applied. If 0, it means applying all available new migrations.

Returns

integer – The status of the action execution. 0 means normal, other values mean abnormal.

Signature

public integer actionUp ( $limit = 0 )

beforeAction()

This method is invoked right before an action is to be executed (after all possible filters.) It checks the existence of the $migrationPath.

View source

Arguments

Returns

boolean – Whether the action should continue to be executed.

Throws

Signature

public boolean beforeAction ( $action )

init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

Signature

public void init ( )

optionAliases()

Returns option alias names.

Child classes may override this method to specify alias options.

View source

Returns

array – The options alias names valid for the action where the keys is alias name for option and value is option name.

Signature

public array optionAliases ( )

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. 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.

View source

Arguments

  • $actionID (string) – The action id of the current request

Returns

string[] – The names of the options valid for the action

Signature

public string[] options ( $actionID )

Protected Methods

MethodDescription
addMigrationHistory()Adds new migration entry to the history.
createMigration()Creates a new migration instance.
generateMigrationSourceCode()Generates new migration source PHP code.
getActionMethodReflection()
getMigrationHistory()Returns the migration history.
getMigrationNameLimit()Return the maximum name length for a migration.
getMigrator()Returns the migration manager that should be used for this request
getNewMigrations()Returns the migrations that are not applied.
includeMigrationFile()Includes the migration file for a given migration class name.
migrateDown()Downgrades with the specified migration class.
migrateToTime()Migrates to the specified apply time in the past.
migrateToVersion()Migrates to the certain version.
migrateUp()Upgrades with the specified migration class.
parseDocCommentDetail()Returns full description from the docblock.
parseDocCommentSummary()Returns the first line of docblock.
parseDocCommentTags()Parses the comment block into tags.
removeMigrationHistory()Removes existing migration from the history.
truncateDatabase()Truncates the database.

addMigrationHistory()

Adds new migration entry to the history.

View source

Arguments

  • $version (string) – Migration version name.

Signature

protected void addMigrationHistory ( $version )

createMigration()

Creates a new migration instance.

View source

Arguments

  • $class (string) – The migration class name

Returns

yii\db\MigrationInterface – The migration instance

Signature

protected yii\db\MigrationInterface createMigration ( $class )

getMigrationHistory()

Returns the migration history.

View source

Arguments

  • $limit (integer) – The maximum number of records in the history to be returned. null for "no limit".

Returns

array – The migration history

Signature

protected array getMigrationHistory ( $limit )

getMigrator()

Returns the migration manager that should be used for this request

View source

Returns

craft\db\MigrationManager

Signature

protected craft\db\MigrationManager getMigrator ( )

getNewMigrations()

Returns the migrations that are not applied.

View source

Returns

array – List of new migrations

Signature

protected array getNewMigrations ( )

removeMigrationHistory()

Removes existing migration from the history.

View source

Arguments

  • $version (string) – Migration version name.

Signature

protected void removeMigrationHistory ( $version )

Constants

ConstantDescription
BASE_MIGRATIONThe name of the dummy migration that marks the beginning of the whole migration history.
EXIT_CODE_ERROR
EXIT_CODE_NORMAL