如源代码,数据库结构演变成为一个数据库驱动程序来进行开发与维护。例如,在开发过程中,可以添加一个新表,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
如源代码,数据库结构演变成为一个数据库驱动程序来进行开发与维护。例如,在开发过程中,可以添加一个新表,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.
tool that can keep track of your database migration history, apply new migrations, or revert existing ones.
下列步骤显示了一个团队在开发过程中如何进行 database migration:
下列步骤显示了一个团队在开发过程中如何进行 database migration:
1. Tim 创建了一个新的 migration (如创建一个新表,更改一个列定义,等)。
1. Tim 创建了一个新的 migration (如创建一个新表,更改一个列定义,等)。
2. Tim 向版本控制系统提交了一个新的 migration (如 Git, Mercurial)。
2. Tim 向版本控制系统提交了一个新的 migration (如 Git, Mercurial)。
3. Doug 从版本控制系统中更新了自己的资料库并接收新的 migration。
3. Doug 从版本控制系统中更新了自己的资料库并接收新的 migration。
4. Doug 将 migration 应用到自己本地开发的数据库,从而同步他的数据库来反映 Tim 所做的更改。
4. Doug 将 migration 应用到自己本地开发的数据库,从而同步他的数据库来反映 Tim 所做的更改。
class m101129_185401_create_news_table extends \yii\db\Migration
class m101129_185401_create_news_table extends \yii\db\Migration
@ -56,8 +56,8 @@ class m101129_185401_create_news_table extends \yii\db\Migration
}
}
```
```
注意和类名相同的文件名,并且遵循
注意和类名相同的文件名,并且遵循
`m<timestamp>_<name>` 模式,where:
`m<timestamp>_<name>` 模式,where:
* `<timestamp>` refers to the UTC timestamp (in the
* `<timestamp>` refers to the UTC timestamp (in the
format of `yymmdd_hhmmss`) when the migration is created,
format of `yymmdd_hhmmss`) when the migration is created,
@ -66,13 +66,13 @@ format of `yymmdd_hhmmss`) when the migration is created,
In the class, the `up()` method should contain the code implementing the actual database
In the class, the `up()` method should contain the code implementing the actual database
migration. In other words, the `up()` method executes code that actually changes the database. The `down()` method may contain code that reverts the changes made by `up()`.
migration. In other words, the `up()` method executes code that actually changes the database. The `down()` method may contain code that reverts the changes made by `up()`.
Sometimes, it is impossible for the `down()` to undo the database migration. 例如,if the migration deletes
Sometimes, it is impossible for the `down()` to undo the database migration. 例如,if the migration deletes
table rows or an entire table, that data cannot be recovered in the `down()` method. In such
table rows or an entire table, that data cannot be recovered in the `down()` method. In such
cases, the migration is called irreversible, meaning the database cannot be rolled back to
cases, the migration is called irreversible, meaning the database cannot be rolled back to
a previous state. When a migration is irreversible, as in the above generated code, the `down()`
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.
method returns `false` to indicate that the migration cannot be reverted.
作为一个例子,让我们来展示 migration 是如何创建一个新表的。
作为一个例子,让我们来展示 migration 是如何创建一个新表的。
```php
```php
@ -97,8 +97,8 @@ class m101129_185401_create_news_table extends \yii\db\Migration
}
}
```
```
基类 [[\yii\db\Migration]] 通过 `db` 属性
基类 [[\yii\db\Migration]] 通过 `db` 属性
展示数据库的链接。You can use it for manipulating data and the schema of a database.
展示数据库的链接。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
The column types used in this example are abstract types that will be replaced
by Yii with the corresponding types depending on your database management system.
by Yii with the corresponding types depending on your database management system.
@ -117,9 +117,9 @@ define column types.
Transactional Migrations
Transactional Migrations
------------------------
------------------------
当进行复杂的 DB migrations 时,we usually want to make sure that each
当进行复杂的 DB migrations 时,we usually want to make sure that each
migration succeeds or fail as a whole so that the database maintains its
migration succeeds or fail as a whole so that the database maintains its
consistency and integrity. 为了实现这一目标,可以应用
consistency and integrity. 为了实现这一目标,可以应用
DB transactions. We use the special methods `safeUp` and `safeDown` for these purposes.
DB transactions. We use the special methods `safeUp` and `safeDown` for these purposes.
```php
```php
@ -163,25 +163,25 @@ When your code uses more then one query it is recommended to use `safeUp` and `s
Applying Migrations
Applying Migrations
-------------------
-------------------
为了应用所有的可用的新 migrations (i.e., make the local database up-to-date),
为了应用所有的可用的新 migrations (i.e., make the local database up-to-date),
运行如下命令:
运行如下命令:
```
```
yii migrate
yii migrate
```
```
该命令将显示所有的新的 migrations 列表。如果你确定想要应用
该命令将显示所有的新的 migrations 列表。如果你确定想要应用
migrations,可以在每一个新的 migration 类中运行 `up()` 方法,一个
migrations,可以在每一个新的 migration 类中运行 `up()` 方法,一个
接一个,按照类名中的 timestamp 值的顺序。
接一个,按照类名中的 timestamp 值的顺序。
应用 migration 之后,migration 工具将会在 `migration` 表中
应用 migration 之后,migration 工具将会在 `migration` 表中
做一个记录。This allows the tool to identify which migrations
做一个记录。This allows the tool to identify which migrations
have been applied and which have not. If the `migration` table does not exist,
have been applied and which have not. If the `migration` table does not exist,
the tool will automatically create it in the database specified by the `db`
the tool will automatically create it in the database specified by the `db`