Like source code, the structure of a database evolves as a database-driven application is developed and maintained. For example, during development, a new table may be added; Or, after the application goes live, it may be discovered that an additional index is required. It is important to keep track of these structural database changes (called **migration**), just as changes to the source code is tracked using version control. If the source code and the database become out of sync, bugs will occur, or the whole application might break. For this reason, Yii provides a database migration
tool that can keep track of database migration history, apply new migrations, or revert existing ones.
Like source code, the structure of a database evolves as a database-driven application is developed and maintained. For example, during development, a new table may be added, or after the application goes live it may be discovered that an additional index is required. It is important to keep track of these structural database changes (called **migration**), just as changes to the source code is tracked using version control. If the source code and the database become out of sync, bugs will occur, or the whole application might break. For this reason, Yii provides a database migration
tool that can keep track of your database migration history, apply new migrations, or revert existing ones.
The following steps show how database migration is used by a team during development:
...
...
@@ -72,7 +72,7 @@ cases, the migration is called irreversible, meaning the database cannot be roll
a previous state. When a migration is irreversible, as in the above generated code, the `down()`
method returns `false` to indicate that the migration cannot be reverted.
As an example, let's show the migration about creating a news table.
As an example, let's show the migration for creating a news table.
```php
...
...
@@ -97,11 +97,11 @@ class m101129_185401_create_news_table extends \yii\db\Migration
}
```
The base class [[\yii\db\Migration]] exposes a database connection via `db`
property. You can use it for manipulating data and schema of a database.
The base class [[\yii\db\Migration]] exposes a database connection via the `db`
property. You can use it for manipulating data and the schema of a database.
The column types used in this example are abstract types that will be replaced
by Yii with the corresponding types depended on your database management system.
by Yii with the corresponding types depending on your database management system.
You can use them to write database independent migrations.
For example `pk` will be replaced by `int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY`
for MySQL and `integer PRIMARY KEY AUTOINCREMENT NOT NULL` for sqlite.
...
...
@@ -110,7 +110,7 @@ of available types. You may also use the constants defined in [[yii\db\Schema]]
define column types.
> Note: You can add constraints and other custom table options at the end of the table description by
> specifying them as simple string. For example in the above migration, after `content` attribute definition
> specifying them as a simple string. For example, in the above migration, after the `content` attribute definition
> you can write `'CONSTRAINT ...'` or other custom options.
...
...
@@ -118,9 +118,9 @@ Transactional Migrations
------------------------
While performing complex DB migrations, we usually want to make sure that each
migration succeed or fail as a whole so that the database maintains the
migration succeeds or fail as a whole so that the database maintains its
consistency and integrity. In order to achieve this goal, we can exploit
DB transactions. We could use special methods `safeUp` and `safeDown` for these purposes.
DB transactions. We use the special methods `safeUp` and `safeDown` for these purposes.
```php
...
...
@@ -156,7 +156,7 @@ When your code uses more then one query it is recommended to use `safeUp` and `s
> Note: Not all DBMS support transactions. And some DB queries cannot be put
> into a transaction. In this case, you will have to implement `up()` and
> `down()`, instead. And for MySQL, some SQL statements may cause
> `down()`, instead. In the case of MySQL, some SQL statements may cause