Migration

Type
Abstract Class
Namespace
craft\db
Inherits
craft\db\Migration » yii\db\Migration (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window), yii\db\MigrationInterface (opens new window)
Uses traits
yii\db\SchemaBuilderTrait (opens new window)
Extended by
craft\migrations\CreateDbCacheTable, craft\migrations\CreateMatrixContentTable, craft\migrations\CreatePhpSessionTable, craft\migrations\Install
Since
3.0.0

View source (opens new window)

# Public Properties

Property Description
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
compact (opens new window) boolean (opens new window) – Indicates whether the console output should be compacted.
db (opens new window) yii\db\Connection (opens new window), array (opens new window), string (opens new window) – The DB connection object or the application component ID of the DB connection that this migration should work with.
maxSqlOutputLength (opens new window) integer (opens new window) – Max number of characters of the SQL outputted.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
addColumn() (opens new window) Builds and executes a SQL statement for adding a new DB column.
addCommentOnColumn() (opens new window) Builds and execute a SQL statement for adding comment to column.
addCommentOnTable() (opens new window) Builds a SQL statement for adding comment to table.
addForeignKey() Builds a SQL statement for adding a foreign key constraint to an existing table.
addPrimaryKey() Builds and executes a SQL statement for creating a primary key.
alterColumn() (opens new window) Builds and executes a SQL statement for changing the definition of a column.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
batchInsert() Creates and executes a batch INSERT SQL statement.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
bigInteger() (opens new window) Creates a bigint column.
bigPrimaryKey() (opens new window) Creates a big primary key column.
binary() (opens new window) Creates a binary column.
boolean() (opens new window) Creates a boolean column.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
char() (opens new window) Creates a char column.
className() (opens new window) Returns the fully qualified name of this class.
createIndex() Builds and executes a SQL statement for creating a new index.
createTable() (opens new window) Builds and executes a SQL statement for creating a new DB table.
date() (opens new window) Creates a date column.
dateTime() (opens new window) Creates a datetime column.
decimal() (opens new window) Creates a decimal column.
delete() (opens new window) Creates and executes a DELETE SQL statement.
deleteDuplicates() Creates and executes a DELETE SQL statement that will only delete duplicate rows from a table.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
double() (opens new window) Creates a double column.
down() This method contains the logic to be executed when removing this migration.
dropColumn() (opens new window) Builds and executes a SQL statement for dropping a DB column.
dropCommentFromColumn() (opens new window) Builds and execute a SQL statement for dropping comment from column.
dropCommentFromTable() (opens new window) Builds a SQL statement for dropping comment from table.
dropForeignKey() (opens new window) Builds a SQL statement for dropping a foreign key constraint.
dropIndex() (opens new window) Builds and executes a SQL statement for dropping an index.
dropPrimaryKey() (opens new window) Builds and executes a SQL statement for dropping a primary key.
dropTable() (opens new window) Builds and executes a SQL statement for dropping a DB table.
dropTableIfExists() Creates and executes a SQL statement for dropping a DB table, if it exists.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
enum() Creates an enum column for MySQL and PostgreSQL, or a string column with a check constraint for others.
execute() (opens new window) Executes a SQL statement.
float() (opens new window) Creates a float column.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() (opens new window) Initializes the migration.
insert() Creates and executes an INSERT SQL statement.
integer() (opens new window) Creates an integer column.
json() (opens new window) Creates a JSON column.
longText() Creates a longtext column for MySQL, or text column for others.
mediumText() Creates a mediumtext column for MySQL, or text column for others.
money() (opens new window) Creates a money column.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
primaryKey() (opens new window) Creates a primary key column.
renameColumn() (opens new window) Builds and executes a SQL statement for renaming a column.
renameSequence() Creates and executes a SQL statement for renaming a DB sequence.
renameTable() (opens new window) Builds and executes a SQL statement for renaming a DB table.
replace() Creates and executes a SQL statement for replacing some text with other text in a given table column.
restore() Creates and executes a SQL statement for restoring a soft-deleted row.
safeDown() (opens new window) This method contains the logic to be executed when removing this migration.
safeUp() (opens new window) This method contains the logic to be executed when applying this migration.
smallInteger() (opens new window) Creates a smallint column.
softDelete() Creates and executes a SQL statement for soft-deleting a row.
string() (opens new window) Creates a string column.
text() (opens new window) Creates a text column.
time() (opens new window) Creates a time column.
timestamp() (opens new window) Creates a timestamp column.
tinyInteger() (opens new window) Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
tinyText() Creates a tinytext column for MySQL, or text column for others.
trigger() (opens new window) Triggers an event.
truncateTable() (opens new window) Builds and executes a SQL statement for truncating a DB table.
uid() Shortcut for creating a uid column
up() This method contains the logic to be executed when applying this migration.
update() Creates and executes an UPDATE SQL statement.
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.

# addForeignKey()

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

The method will properly quote the table and column names.

View source (opens new window)

Arguments

# addPrimaryKey()

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

The method will properly quote the table and column names.

View source (opens new window)

Arguments

# batchInsert()

Creates and executes a batch INSERT SQL statement.

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

View source (opens new window)

Arguments

# createIndex()

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

View source (opens new window)

Arguments

  • $name (string (opens new window), null (opens new window)) – The name of the index. The name will be properly quoted by the method. If null, a name will be automatically generated.
  • $table (string (opens new window)) – The table that the new index will be created for. The table name will be properly quoted by the method.
  • $columns (string (opens new window), array (opens new window)) – 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 (boolean (opens new window)) – Whether to add UNIQUE constraint on the created index.

# deleteDuplicates()

Since
3.5.2

Creates and executes a DELETE SQL statement that will only delete duplicate rows from a table.

View source (opens new window)

Arguments

# down()

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.

View source (opens new window)

Arguments

Returns

false (opens new window), null (opens new window)

Throws

# dropTableIfExists()

Creates and executes a SQL statement for dropping a DB table, if it exists.

View source (opens new window)

Arguments

# enum()

Creates an enum column for MySQL and PostgreSQL, or a string column with a check constraint for others.

View source (opens new window)

Arguments

Returns

yii\db\ColumnSchemaBuilder (opens new window) – The column instance which can be further customized.

# insert()

Creates and executes an INSERT SQL statement.

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

View source (opens new window)

Arguments

# longText()

Creates a longtext column for MySQL, or text column for others.

View source (opens new window)

Returns

yii\db\ColumnSchemaBuilder (opens new window) – The column instance which can be further customized.

# mediumText()

Creates a mediumtext column for MySQL, or text column for others.

View source (opens new window)

Returns

yii\db\ColumnSchemaBuilder (opens new window) – The column instance which can be further customized.

# renameSequence()

Creates and executes a SQL statement for renaming a DB sequence.

View source (opens new window)

Arguments

# replace()

Creates and executes a SQL statement for replacing some text with other text in a given table column.

View source (opens new window)

Arguments

# restore()

Since
3.1.0

Creates and executes a SQL statement for restoring a soft-deleted row.

View source (opens new window)

Arguments

# softDelete()

Since
3.1.0

Creates and executes a SQL statement for soft-deleting a row.

View source (opens new window)

Arguments

# tinyText()

Creates a tinytext column for MySQL, or text column for others.

View source (opens new window)

Returns

yii\db\ColumnSchemaBuilder (opens new window) – The column instance which can be further customized.

# uid()

Shortcut for creating a uid column

View source (opens new window)

Returns

yii\db\ColumnSchemaBuilder (opens new window) – The column instance which can be further customized.

# up()

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

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

View source (opens new window)

Arguments

Returns

false (opens new window), null (opens new window)

Throws

# update()

Creates and executes an UPDATE SQL statement.

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

View source (opens new window)

Arguments

# upsert()

Since
2.0.14

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.

View source (opens new window)

Arguments

Returns

$this – The command object itself.

# Protected Methods

Method Description
beginCommand() (opens new window) Prepares for a command to be executed, and outputs to the console.
endCommand() (opens new window) Finalizes after the command has been executed, and outputs to the console the time elapsed.
getDb() (opens new window)

# Events

# EVENT_AFTER_DOWN

Type
yii\base\Event (opens new window)
Since
3.0.6

The event that is triggered after the migration is reverted

# EVENT_AFTER_UP

Type
yii\base\Event (opens new window)
Since
3.0.6

The event that is triggered after the migration is executed