CDbMigration is the base class for representing a database migration.
CDbMigration is designed to be used together with the "yiic migrate"
command.
Each child class of CDbMigration represents an individual database migration
which is identified by the child class name.
Within each migration, the CDbMigration::up()
method contains the logic for
"upgrading" the database used in an application; while the CDbMigration::down()
method
contains "downgrading" logic. The "yiic migrate" command manages all available
migrations in an application.
By overriding CDbMigration::safeUp()
or CDbMigration::safeDown()
methods instead of CDbMigration::up()
and CDbMigration::down()
the migration logic will be wrapped with a DB
transaction.
CDbMigration provides a set of convenient methods for manipulating database
data and schema. For example, the CDbMigration::insert()
method can be used to easily
insert a row of data into a database table; the CDbMigration::createTable()
method can
be used to create a database table. Compared with the same methods in CDbCommand
, these methods will display extra information showing the method
parameters and execution time, which may be useful when applying migrations.
Methods summary
public
boolean
|
#
up( )
This method contains the logic to be executed when applying this migration.
Child classes may implement this method to provide actual migration logic.
This method contains the logic to be executed when applying this migration.
Child classes may implement this method to provide actual migration logic.
Returns
boolean Returning false means, the migration will not be applied.
|
public
boolean
|
#
down( )
This method contains the logic to be executed when removing this migration.
Child classes may override this method if the corresponding migrations can be
removed.
This method contains the logic to be executed when removing this migration.
Child classes may override this method if the corresponding migrations can be
removed.
Returns
boolean Returning false means, the migration will not be applied.
|
public
boolean
|
#
safeUp( )
This method contains the logic to be executed when applying this migration.
This method differs from CDbMigration::up() in that the DB logic implemented here will
be enclosed within a DB transaction. Child classes may implement this method
instead of CDbMigration::up() if the DB logic needs to be within a transaction.
This method contains the logic to be executed when applying this migration.
This method differs from CDbMigration::up() in that the DB logic implemented here will
be enclosed within a DB transaction. Child classes may implement this method
instead of CDbMigration::up() if the DB logic needs to be within a transaction.
Returns
boolean Returning false means, the migration will not be applied and the transaction
will be rolled back.
Since
1.1.7
|
public
boolean
|
#
safeDown( )
This method contains the logic to be executed when removing this migration.
This method differs from CDbMigration::down() in that the DB logic implemented here will
be enclosed within a DB transaction. Child classes may implement this method
instead of CDbMigration::up() if the DB logic needs to be within a transaction.
This method contains the logic to be executed when removing this migration.
This method differs from CDbMigration::down() in that the DB logic implemented here will
be enclosed within a DB transaction. Child classes may implement this method
instead of CDbMigration::up() if the DB logic needs to be within a transaction.
Returns
boolean Returning false means, the migration will not be applied and the transaction
will be rolled back.
Since
1.1.7
|
public
CDbConnection
|
|
public
|
|
public
|
#
execute( string $sql, array $params = array() )
Executes a SQL statement. This method executes the specified SQL statement
using dbConnection.
Executes a SQL statement. This method executes the specified SQL statement
using dbConnection.
Parameters
- $sql
string $sql the SQL statement to be executed
- $params
array $params input parameters (name=>value) for the SQL execution. See CDbCommand::execute() for more details.
Since
1.1.7
|
public
|
#
insert( string $table, array $columns )
Creates and executes an INSERT SQL statement. The method will properly escape
the column names, and bind the values to be inserted.
Creates and executes an INSERT SQL statement. The method will properly escape
the column names, and bind the values to be inserted.
Parameters
- $table
string $table the table that new rows will be inserted into.
- $columns
array $columns the column data (name=>value) to be inserted into the table.
|
public
|
#
insertMultiple( string $table, array $data )
Creates and executes an INSERT SQL statement with multiple data. The method
will properly escape the column names, and bind the values to be inserted.
Creates and executes an INSERT SQL statement with multiple data. The method
will properly escape the column names, and bind the values to be inserted.
Parameters
- $table
string $table the table that new rows will be inserted into.
- $data
array $data an array of various column data (name=>value) to be inserted into the
table.
Since
1.1.16
|
public
|
#
update( string $table, array $columns, mixed $conditions = '', array $params = array() )
Creates and executes an UPDATE SQL statement. The method will properly escape
the column names and bind the values to be updated.
Creates and executes an UPDATE SQL statement. The method will properly escape
the column names and bind the values to be updated.
Parameters
- $table
string $table the table to be updated.
- $columns
array $columns the column data (name=>value) to be updated.
- $conditions
mixed $conditions the conditions that will be put in the WHERE part. Please refer to
CDbCommand::where() on how to specify conditions.
- $params
array $params the parameters to be bound to the query.
|
public
|
#
delete( string $table, mixed $conditions = '', array $params = array() )
Creates and executes a DELETE SQL statement.
Creates and executes a DELETE SQL statement.
Parameters
- $table
string $table the table where the data will be deleted from.
- $conditions
mixed $conditions the conditions that will be put in the WHERE part. Please refer to
CDbCommand::where() on how to specify conditions.
- $params
array $params the parameters to be bound to the query.
|
public
|
#
createTable( string $table, array $columns, string $options = null )
Builds and executes a SQL statement for creating a new DB table.
Builds and executes a SQL statement for creating a new DB table.
The columns in the new table should be specified as name-definition pairs
(e.g. 'name'=>'string'), where name stands for a column name which will be
properly quoted by the method, and definition stands for the column type which
can contain an abstract DB type. The getColumnType method will be
invoked to convert any abstract type into a physical one.
If a column is specified with definition only (e.g. 'PRIMARY KEY (name,
type)'), it will be directly inserted into the generated SQL.
Parameters
- $table
string $table the name of the table to be created. The name will be properly quoted by
the method.
- $columns
array $columns the columns (name=>definition) in the new table.
- $options
string $options additional SQL fragment that will be appended to the generated SQL.
|
public
|
#
renameTable( string $table, string $newName )
Builds and executes a SQL statement for renaming a DB table.
Builds and executes a SQL statement for renaming a DB table.
Parameters
- $table
string $table the table to be renamed. The name will be properly quoted by the method.
- $newName
string $newName the new table name. The name will be properly quoted by the method.
|
public
|
#
dropTable( string $table )
Builds and executes a SQL statement for dropping a DB table.
Builds and executes a SQL statement for dropping a DB table.
Parameters
- $table
string $table the table to be dropped. The name will be properly quoted by the method.
|
public
|
#
truncateTable( string $table )
Builds and executes a SQL statement for truncating a DB table.
Builds and executes a SQL statement for truncating a DB table.
Parameters
- $table
string $table the table to be truncated. The name will be properly quoted by the
method.
|
public
|
#
addColumn( string $table, string $column, string $type )
Builds and executes a SQL statement for adding a new DB column.
Builds and executes a SQL statement for adding a new DB column.
Parameters
- $table
string $table the table that the new column will be added to. The table name will be
properly quoted by the method.
- $column
string $column the name of the new column. The name will be properly quoted by the
method.
- $type
string $type the column type. The getColumnType method will be invoked to
convert abstract column type (if any) into the physical one. Anything that is
not recognized as abstract type will be kept in the generated SQL. For example,
'string' will be turned into 'varchar(255)', while 'string not null' will become
'varchar(255) not null'.
|
public
|
#
dropColumn( string $table, string $column )
Builds and executes a SQL statement for dropping a DB column.
Builds and executes a SQL statement for dropping a DB column.
Parameters
- $table
string $table the table whose column is to be dropped. The name will be properly quoted
by the method.
- $column
string $column the name of the column to be dropped. The name will be properly quoted
by the method.
|
public
|
#
renameColumn( string $table, string $name, string $newName )
Builds and executes a SQL statement for renaming a column.
Builds and executes a SQL statement for renaming a column.
Parameters
- $table
string $table the table whose column is to be renamed. The name will be properly quoted
by the method.
- $name
string $name the old name of the column. The name will be properly quoted by the
method.
- $newName
string $newName the new name of the column. The name will be properly quoted by the
method.
|
public
|
#
alterColumn( string $table, string $column, string $type )
Builds and executes a SQL statement for changing the definition of a
column.
Builds and executes a SQL statement for changing the definition of a
column.
Parameters
- $table
string $table the table whose column is to be changed. The table name will be properly
quoted by the method.
- $column
string $column the name of the column to be changed. The name will be properly quoted
by the method.
- $type
string $type the new column type. The getColumnType method will be invoked to
convert abstract column type (if any) into the physical one. Anything that is
not recognized as abstract type will be kept in the generated SQL. For example,
'string' will be turned into 'varchar(255)', while 'string not null' will become
'varchar(255) not null'.
|
public
|
#
addForeignKey( string $name, string $table, string|array $columns, string $refTable, string|array $refColumns, string $delete = null, string $update = null )
Builds a SQL statement for adding a foreign key constraint to an existing
table. The method will properly quote the table and column names.
Builds a SQL statement for adding a foreign key constraint to an existing
table. The method will properly quote the table and column names.
Parameters
- $name
string $name the name of the foreign key constraint.
- $table
string $table the table that the foreign key constraint will be added to.
- $columns
string|array $columns the name of the column to that the constraint will be added on. If
there are multiple columns, separate them with commas or pass as an array of
column names.
- $refTable
string $refTable the table that the foreign key references to.
- $refColumns
string|array $refColumns the name of the column that the foreign key references to. If there
are multiple columns, separate them with commas or pass as an array of column
names.
- $delete
string $delete the ON DELETE option. Most DBMS support these options: RESTRICT,
CASCADE, NO ACTION, SET DEFAULT, SET NULL
- $update
string $update the ON UPDATE option. Most DBMS support these options: RESTRICT,
CASCADE, NO ACTION, SET DEFAULT, SET NULL
|
public
|
#
dropForeignKey( string $name, string $table )
Builds a SQL statement for dropping a foreign key constraint.
Builds a SQL statement for dropping a foreign key constraint.
Parameters
- $name
string $name the name of the foreign key constraint to be dropped. The name will be
properly quoted by the method.
- $table
string $table the table whose foreign is to be dropped. The name will be properly
quoted by the method.
|
public
|
#
createIndex( string $name, string $table, string|array $columns, boolean $unique = false )
Builds and executes a SQL statement for creating a new index.
Builds and executes a SQL statement for creating a new index.
Parameters
- $name
string $name the name of the index. The name will be properly quoted by the method.
- $table
string $table the table that the new index will be created for. The table name will be
properly quoted by the method.
- $columns
string|array $columns the column(s) that should be included in the index. If there are
multiple columns, please separate them by commas or pass as an array of column
names. Each column name will be properly quoted by the method, unless a
parenthesis is found in the name.
- $unique
boolean $unique whether to add UNIQUE constraint on the created index.
|
public
|
#
dropIndex( string $name, string $table )
Builds and executes a SQL statement for dropping an index.
Builds and executes a SQL statement for dropping an index.
Parameters
- $name
string $name the name of the index to be dropped. The name will be properly quoted by
the method.
- $table
string $table the table whose index is to be dropped. The name will be properly quoted
by the method.
|
public
|
#
refreshTableSchema( string $table )
Refreshed schema cache for a table
Refreshed schema cache for a table
Parameters
- $table
string $table name of the table to refresh
Since
1.1.9
|
public
|
#
addPrimaryKey( string $name, string $table, string|array $columns )
Builds and executes a SQL statement for creating a primary key, supports
composite primary keys.
Builds and executes a SQL statement for creating a primary key, supports
composite primary keys.
Parameters
- $name
string $name name of the primary key constraint to add
- $table
string $table name of the table to add primary key to
- $columns
string|array $columns comma separated string or array of columns that the primary key will
consist of. Array value can be passed since 1.1.14.
Since
1.1.13
|
public
|
#
dropPrimaryKey( string $name, string $table )
Builds and executes a SQL statement for removing a primary key, supports
composite primary keys.
Builds and executes a SQL statement for removing a primary key, supports
composite primary keys.
Parameters
- $name
string $name name of the constraint to remove
- $table
string $table name of the table to remove primary key from
Since
1.1.13
|