Skip to content

Migration ​

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

View source

Public Properties ​

PropertyDescription
behaviorsyii\base\Behavior – List of behaviors attached to this component.
compactboolean – Indicates whether the console output should be compacted.
dbcraft\db\Connection – the DB connection that this command is associated with
maxSqlOutputLengthinteger – Max number of characters of the SQL outputted.

db ​

Type
craft\db\Connection
Default value
null

the DB connection that this command is associated with

View source

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 a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
addCheck()Creates a SQL command for adding a check constraint to an existing table.
addColumn()Builds and executes a SQL statement for adding a new DB column.
addCommentOnColumn()Builds and execute a SQL statement for adding comment to column.
addCommentOnTable()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()Builds and executes a SQL statement for changing the definition of a column.
archiveTableIfExists()Creates and executes a SQL statement for renaming a DB table to *_old, if it exists.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
batchInsert()Creates and executes a batch INSERT SQL statement.
behaviors()Returns a list of behaviors that this component should behave as.
bigInteger()Creates a bigint column.
bigPrimaryKey()Creates a big primary key column.
binary()Creates a binary column.
boolean()Creates a boolean column.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
char()Creates a char column.
className()Returns the fully qualified name of this class.
createIndex()Builds and executes a SQL statement for creating a new index.
createIndexIfMissing()Creates a new index if a similar one doesn’t already exist.
createTable()Builds and executes a SQL statement for creating a new DB table.
date()Creates a date column.
dateTime()Creates a datetime column.
decimal()Creates a decimal column.
delete()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()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
double()Creates a double column.
down()This method contains the logic to be executed when removing this migration.
dropAllForeignKeysToTable()Creates and executes a SQL statement for dropping all foreign keys to a table.
dropCheck()Creates a SQL command for dropping a check constraint.
dropColumn()Builds and executes a SQL statement for dropping a DB column.
dropCommentFromColumn()Builds and execute a SQL statement for dropping comment from column.
dropCommentFromTable()Builds a SQL statement for dropping comment from table.
dropForeignKey()Builds a SQL statement for dropping a foreign key constraint.
dropForeignKeyIfExists()Creates and executes a SQL statement for dropping a foreign key if it exists.
dropIndex()Builds and executes a SQL statement for dropping an index.
dropIndexIfExists()Creates and executes a SQL statement for dropping an index if it exists.
dropPrimaryKey()Builds and executes a SQL statement for dropping a primary key.
dropTable()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()Makes sure that the behaviors declared in behaviors() 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()Executes a SQL statement.
float()Creates a float column.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getDb()returns the connection the DB connection that this command is associated with
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 for this component.
init()Initializes the migration.
insert()Creates and executes an INSERT SQL statement.
integer()Creates an integer column.
json()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()Creates a money column.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
primaryKey()Creates a primary key column.
renameColumn()Builds and executes a SQL statement for renaming a column.
renameSequence()Creates and executes a SQL statement for renaming a DB sequence.
renameTable()Builds and executes a SQL statement for renaming a DB table and its corresponding sequence (if PostgreSQL).
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()This method contains the logic to be executed when removing this migration.
safeUp()This method contains the logic to be executed when applying this migration.
smallInteger()Creates a smallint column.
softDelete()Creates and executes a SQL statement for soft-deleting a row.
string()Creates a string column.
text()Creates a text column.
time()Creates a time column.
timestamp()Creates a timestamp column.
tinyInteger()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()Triggers an event.
truncateTable()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

Arguments ​

  • $name (string, null) – The name of the foreign key constraint. If null, a name will be automatically generated.
  • $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) – The ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  • $update (string) – The ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL

addPrimaryKey() ​

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

The method will properly quote the table and column names.

View source

Arguments ​

  • $name (string, null) – The name of the primary key constraint. If null, a name will be automatically generated.
  • $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.

archiveTableIfExists() ​

Since
4.0.0

Creates and executes a SQL statement for renaming a DB table to *_old, if it exists.

View source

Arguments ​

  • $table (string) – The table to be renamed. The name will be properly quoted by the method.

batchInsert() ​

Creates and executes a batch INSERT SQL statement.

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

If the table contains dateCreated, dateUpdated, and/or uid columns, those values will be included automatically, if not already set.

View source

Arguments ​

  • $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

createIndex() ​

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

View source

Arguments ​

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

createIndexIfMissing() ​

Since
3.7.32

Creates a new index if a similar one doesn’t already exist.

View source

Arguments ​

  • $table (string) – The table that the new index will be created for. The table name will be properly quoted by the method.
  • $columns (array, string) – The column(s) that should be included in the index. If there are multiple columns, please separate them by commas or use an array.
  • $unique (boolean) – 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

Arguments ​

  • $table (string) – The table where the data will be deleted from
  • $columns (string[]) – The column names that contain duplicate data
  • $pk (string) – The primary key column name

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

Arguments ​

  • $throwExceptions (boolean) – Whether exceptions should be thrown

Returns ​

boolean – Whether the operation was successful

Throws ​

dropAllForeignKeysToTable() ​

Since
4.0.0

Creates and executes a SQL statement for dropping all foreign keys to a table.

View source

Arguments ​

  • $table (string) – The table that the foreign keys should reference.

dropForeignKeyIfExists() ​

Since
4.0.0

Creates and executes a SQL statement for dropping a foreign key if it exists.

View source

Arguments ​

  • $table (string) – The table that the foreign key was created for. The table name will be properly quoted by the method.
  • $columns (string, string[]) – The column(s) that are included in the foreign key. If there are multiple columns, separate them by commas or use an array.

dropIndexIfExists() ​

Since
3.7.32

Creates and executes a SQL statement for dropping an index if it exists.

View source

Arguments ​

  • $table (string) – The table that the index was created for. The table name will be properly quoted by the method.
  • $columns (string, string[]) – The column(s) that are included in the index. If there are multiple columns, separate them by commas or use an array.
  • $unique (boolean) – Whether the index has a UNIQUE constraint.

dropTableIfExists() ​

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

View source

Arguments ​

  • $table (string) – The table to be dropped. The name will be properly quoted by the method.

enum() ​

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

View source

Arguments ​

  • $columnName (string) – The column name
  • $values (string[]) – The allowed column values

Returns ​

yii\db\ColumnSchemaBuilder – The column instance which can be further customized.

getDb() ​

returns the connection the DB connection that this command is associated with

View source

Returns ​

craft\db\Connection –

insert() ​

Creates and executes an INSERT SQL statement.

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

If the table contains dateCreated, dateUpdated, and/or uid columns, those values will be included automatically, if not already set.

View source

Arguments ​

  • $table (string) – The table that new rows will be inserted into.
  • $columns (array) – The column data (name => value) to be inserted into the table.

longText() ​

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

View source

Returns ​

yii\db\ColumnSchemaBuilder – The column instance which can be further customized.

mediumText() ​

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

View source

Returns ​

yii\db\ColumnSchemaBuilder – The column instance which can be further customized.

renameSequence() ​

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

View source

Arguments ​

  • $oldName (string) – The sequence to be renamed. The name will be properly quoted by the method.
  • $newName (string) – The new sequence name. The name will be properly quoted by the method.

renameTable() ​

Since
4.0.0

Builds and executes a SQL statement for renaming a DB table and its corresponding sequence (if PostgreSQL).

View source

Arguments ​

  • $table
  • $newName

replace() ​

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

View source

Arguments ​

  • $table (string) – The table to be updated.
  • $column (string) – The column to be searched.
  • $find (string) – The text to be searched for.
  • $replace (string) – The replacement text.
  • $condition (array, string) – The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
  • $params (array) – The parameters to be bound to the command.

restore() ​

Since
3.1.0

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

View source

Arguments ​

  • $table (string) – The table to be updated.
  • $condition (array, string) – The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
  • $params (array) – The parameters to be bound to the command.

softDelete() ​

Since
3.1.0

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

View source

Arguments ​

  • $table (string) – The table to be updated.
  • $condition (array, string) – The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
  • $params (array) – The parameters to be bound to the command.

tinyText() ​

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

View source

Returns ​

yii\db\ColumnSchemaBuilder – The column instance which can be further customized.

uid() ​

Shortcut for creating a uid column

View source

Returns ​

yii\db\ColumnSchemaBuilder – 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

Arguments ​

  • $throwExceptions (boolean) – Whether exceptions should be thrown

Returns ​

boolean – Whether the operation was successful

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

Arguments ​

  • $table (string) – The table to be updated.
  • $columns (array) – The column data (name => value) to be updated.
  • $condition (string, array) – The condition that will be put in the WHERE part. Please refer to craft\db\Query::where() on how to specify condition.
  • $params (array) – The parameters to be bound to the command.
  • $updateTimestamp (boolean) – Whether the dateUpdated column should be updated, if the table has one.

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.

If the table contains dateCreated, dateUpdated, and/or uid columns, those values will be included for new rows automatically, if not already set.

View source

Arguments ​

  • $table (string) – The table that new rows will be inserted into/updated in.
  • $insertColumns (array, craft\db\Query) – The column data (name => value) to be inserted into the table or instance of craft\db\Query to perform INSERT INTO ... SELECT SQL statement.
  • $updateColumns (array, boolean) – 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.
  • $updateTimestamp (boolean) – Whether the dateUpdated column should be updated for existing rows, if the table has one.

Protected Methods ​

MethodDescription
beginCommand()Prepares for a command to be executed, and outputs to the console.
endCommand()Finalizes after the command has been executed, and outputs to the console the time elapsed.

Events ​

EVENT_AFTER_DOWN ​

Type
yii\base\Event
Since
3.0.6

The event that is triggered after the migration is reverted


EVENT_AFTER_UP ​

Type
yii\base\Event
Since
3.0.6

The event that is triggered after the migration is executed