During the course of developing and maintaining a database-driven application, the structure of the database
being used evolves just like the source code does. For example, during the development of an application,
a new table may be found necessary; after the application is deployed to production, it may be discovered
that an index should be created to improve the query performance; and so on. Because a database structure change
often requires some source code changes, Yii supports the so-called database migration feature that allows
you to keep track of database changes in terms of database migrations which are version-controlled together
with the source code.

The following steps show how database migration can be used by a team during development:

Tim creates a new migration (e.g. creates a new table, changes a column definition, etc.).

Tim commits the new migration into the source control system (e.g. Git, Mercurial).

Doug updates his repository from the source control system and receives the new migration.

Doug applies the migration to his local development database, thereby synchronizing his database
to reflect the changes that Tim has made.

And the following steps show how to deploy a new release with database migrations to production:

Scott creates a release tag for the project repository that contains some new database migrations.

Scott updates the source code on the production server to the release tag.

Scott applies any accumulated database migrations to the production database.

Yii provides a set of migration command line tools that allow you to:

create new migrations;

apply migrations;

revert migrations;

re-apply migrations;

show migration history and status.

All these tools are accessible through the command yii migrate. In this section we will describe in detail
how to accomplish various tasks using these tools. You may also get the usage of each tool via the help
command yii help migrate.

Tip: migrations could affect not only database schema but adjust existing data to fit new schema, create RBAC
hierarchy or clean up cache.

Note: When manipulating data using a migration you may find that using your Active Record classes
for this might be useful because some of the logic is already implemented there. Keep in mind however, that in contrast
to code written in the migrations, who's nature is to stay constant forever, application logic is subject to change.
So when using Active Record in migration code, changes to the logic in the Active Record layer may accidentally break
existing migrations. For this reason migration code should be kept independent from other application logic such
as Active Record classes.

The required name argument gives a brief description about the new migration. For example, if
the migration is about creating a new table named news, you may use the name create_news_table
and run the following command:

yii migrate/create create_news_table

Note: Because the name argument will be used as part of the generated migration class name,
it should only contain letters, digits, and/or underscore characters.

The above command will create a new PHP class file named m150101_185401_create_news_table.php
in the @app/migrations directory. The file contains the following code which mainly declares
a migration class m150101_185401_create_news_table with the skeleton code:

Each database migration is defined as a PHP class extending from yii\db\Migration. The migration
class name is automatically generated in the format of m<YYMMDD_HHMMSS>_<Name>, where

<YYMMDD_HHMMSS> refers to the UTC datetime at which the migration creation command is executed.

<Name> is the same as the value of the name argument that you provide to the command.

In the migration class, you are expected to write code in the up() method that makes changes to the database structure.
You may also want to write code in the down() method to revert the changes made by up(). The up() method is invoked
when you upgrade the database with this migration, while the down() method is invoked when you downgrade the database.
The following code shows how you may implement the migration class to create a news table:

Info: Not all migrations are reversible. For example, if the up() method deletes a row of a table, you may
not be able to recover this row in the down() method. Sometimes, you may be just too lazy to implement
the down(), because it is not very common to revert database migrations. In this case, you should return
false in the down() method to indicate that the migration is not reversible.

Rather than using physical types, when creating a table or column you should use abstract types
so that your migrations are independent of specific DBMS. The yii\db\Schema class defines
a set of constants to represent the supported abstract types. These constants are named in the format
of TYPE_<Name>. For example, TYPE_PK refers to auto-incremental primary key type; TYPE_STRING
refers to a string type. When a migration is applied to a particular database, the abstract types
will be translated into the corresponding physical types. In the case of MySQL, TYPE_PK will be turned
into int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, while TYPE_STRING becomes varchar(255).

You can append additional constraints when using abstract types. In the above example, NOT NULL is appended
to Schema::TYPE_STRING to specify that the column cannot be null.

Info: The mapping between abstract types and physical types is specified by
the $typeMap property in each concrete QueryBuilder class.

Since version 2.0.6, you can make use of the newly introduced schema builder which provides more convenient way of defining column schema.
So the migration above could be written like the following:

Since version 2.0.7 migration console provides a convenient way to create migrations.

If the migration name is of a special form, for example create_xxx_table or drop_xxx_table then the generated migration
file will contain extra code, in this case for creating/dropping tables.
In the following all variants of this feature are described.

The position of the foreignKey keyword in the column description doesn't
change the generated code. That means:

author_id:integer:notNull:foreignKey(user)

author_id:integer:foreignKey(user):notNull

author_id:foreignKey(user):integer:notNull

All generate the same code.

The foreignKey keyword can take a parameter between parenthesis which will be
the name of the related table for the generated foreign key. If no parameter
is passed then the table name will be deduced from the column name.

In the example above author_id:integer:notNull:foreignKey(user) will generate a
column named author_id with a foreign key to the user table while
category_id:integer:defaultValue(1):foreignKey will generate a column
category_id with a foreign key to the category table.

Since 2.0.11, foreignKey keyword accepts a second parameter, separated by whitespace.
It accepts the name of the related column for the foreign key generated.
If no second parameter is passed, the column name will be fetched from table schema.
If no schema exists, primary key isn't set or is composite, default name id will be used.

While performing complex DB migrations, it is important to ensure each migration to either succeed or fail as a whole
so that the database can maintain integrity and consistency. To achieve this goal, it is recommended that you
enclose the DB operations of each migration in a transaction.

An even easier way of implementing transactional migrations is to put migration code in the safeUp() and safeDown()
methods. These two methods differ from up() and down() in that they are enclosed implicitly in a transaction.
As a result, if any operation in these methods fails, all prior operations will be rolled back automatically.

In the following example, besides creating the news table we also insert an initial row into this table.

Note that usually when you perform multiple DB operations in safeUp(), you should reverse their execution order
in safeDown(). In the above example we first create the table and then insert a row in safeUp(); while
in safeDown() we first delete the row and then drop the table.

Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples,
please refer to implicit commit. If this is the case,
you should still implement up() and down(), instead.

The benefit of using the methods provided by yii\db\Migration is that you do not need to explicitly
create yii\db\Command instances and the execution of each method will automatically display useful messages
telling you what database operations are done and how long they take.

Info: yii\db\Migration does not provide a database query method. This is because you normally do not need
to display extra message about retrieving data from a database. It is also because you can use the powerful
Query Builder to build and run complex queries.
Using Query Builder in a migration may look like this:

To upgrade a database to its latest structure, you should apply all available new migrations using the following command:

yii migrate

This command will list all migrations that have not been applied so far. If you confirm that you want to apply
these migrations, it will run the up() or safeUp() method in every new migration class, one after another,
in the order of their timestamp values. If any of the migrations fails, the command will quit without applying
the rest of the migrations.

Tip: In case you don't have command line at your server you may try web shell
extension.

For each migration that has been successfully applied, the command will insert a row into a database table named
migration to record the successful application of the migration. This will allow the migration tool to identify
which migrations have been applied and which have not.

Info: The migration tool will automatically create the migration table in the database specified by
the db option of the command. By default, the database
is specified by the dbapplication component.

Sometimes, you may only want to apply one or a few new migrations, instead of all available migrations.
You can do so by specifying the number of migrations that you want to apply when running the command.
For example, the following command will try to apply the next three available migrations:

yii migrate 3

You can also explicitly specify a particular migration to which the database should be migrated
by using the migrate/to command in one of the following formats:

yii migrate/to 150101_185401 # using timestamp to specify the migration
yii migrate/to "2015-01-01 18:54:01"# using a string that can be parsed by strtotime()
yii migrate/to m150101_185401_create_news_table # using full name
yii migrate/to 1392853618# using UNIX timestamp

If there are any unapplied migrations earlier than the specified one, they will all be applied before the specified
migration is applied.

If the specified migration has already been applied before, any later applied migrations will be reverted.

Instead of actually applying or reverting migrations, sometimes you may simply want to mark that your database
has been upgraded to a particular migration. This often happens when you manually change the database to a particular
state and you do not want the migration(s) for that change to be re-applied later. You can achieve this goal with
the following command:

yii migrate/mark 150101_185401 # using timestamp to specify the migration
yii migrate/mark "2015-01-01 18:54:01"# using a string that can be parsed by strtotime()
yii migrate/mark m150101_185401_create_news_table # using full name
yii migrate/mark 1392853618# using UNIX timestamp

The command will modify the migration table by adding or deleting certain rows to indicate that the database
has been applied migrations to the specified one. No migrations will be applied or reverted by this command.

The migration command comes with a few command-line options that can be used to customize its behaviors:

interactive: boolean (defaults to true), specifies whether to perform migrations in an interactive mode.
When this is true, the user will be prompted before the command performs certain actions.
You may want to set this to false if the command is being used in a background process.

migrationPath: string|array (defaults to @app/migrations), specifies the directory storing all migration
class files. This can be specified as either a directory path or a path alias.
Note that the directory must exist, or the command may trigger an error. Since version 2.0.12 an array can be
specified for loading migrations from multiple sources.

migrationTable: string (defaults to migration), specifies the name of the database table for storing
migration history information. The table will be automatically created by the command if it does not exist.
You may also manually create it using the structure version varchar(255) primary key, apply_time integer.

db: string (defaults to db), specifies the ID of the database application component.
It represents the database that will be migrated using this command.

templateFile: string (defaults to @yii/views/migration.php), specifies the path of the template file
that is used for generating skeleton migration class files. This can be specified as either a file path
or a path alias. The template file is a PHP script in which you can use a predefined variable
named $className to get the migration class name.

fields: array of column definition strings used for creating migration code. Defaults to []. The format of each
definition is COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR. For example, --fields=name:string(12):notNull produces
a string column of size 12 which is not null.

The following example shows how you can use these options.

For example, if we want to migrate a forum module whose migration files
are located within the module's migrations directory, we can use the following
command:

With the above configuration, each time you run the migration command, the backend_migration table
will be used to record the migration history. You no longer need to specify it via the migrationTable
command-line option.

Since 2.0.10 you can use namespaces for the migration classes. You can specify the list of the migration namespaces via
migrationNamespaces. Using of the namespaces for
migration classes allows you usage of the several source locations for the migrations. For example:

Note: migrations applied from different namespaces will create a single migration history, e.g. you might be
unable to apply or revert migrations from particular namespace only.

While operating namespaced migrations: creating new, reverting and so on, you should specify full namespace before
migration name. Note that backslash (\) symbol 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:

Since version 2.0.12 the migrationPath property
also accepts an array for specifying multiple directories that contain migrations without a namespace.
This is mainly added to be used in existing projects which use migrations from different locations. These migrations mainly come
from external sources, like Yii extensions developed by other developers,
which can not be changed to use namespaces easily when starting to use the new approach.

Sometimes using single migration history for all project migrations is not desirable. For example: you may install some
'blog' extension, which contains fully separated functionality and contain its own migrations, which should not affect
the ones dedicated to main project functionality.

If you want several migrations to be applied and tracked down completely separated from each other, you can configure multiple
migration commands which will use different namespaces and migration history tables:

By default, migrations are applied to the same database specified by the dbapplication component.
If you want them to be applied to a different database, you may specify the db command-line option like shown below,

yii migrate --db=db2

The above command will apply migrations to the db2 database.

Sometimes it may happen that you want to apply some of the migrations to one database, while some others to another
database. To achieve this goal, when implementing a migration class you should explicitly specify the DB component
ID that the migration would use, like the following:

The above migration will be applied to db2, even if you specify a different database through the db command-line
option. Note that the migration history will still be recorded in the database specified by the db command-line option.

If you have multiple migrations that use the same database, it is recommended that you create a base migration class
with the above init() code. Then each migration class can extend from this base class.

Tip: Besides setting the db property, you can also operate on different databases
by creating new database connections to them in your migration classes. You then use the DAO methods
with these connections to manipulate different databases.

Another strategy that you can take to migrate multiple databases is to keep migrations for different databases in
different migration paths. Then you can migrate these databases in separate commands like the following: