💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 数据迁移 >[success]在这里以`ThinkPHP5.0`为例,在ThinkPHP5中提供了数据库迁移工具`migration`。 > ## 1. 什么是`migration` >一种数据库的版本控制,让团队在修改数据库结构的同时,保持彼此的进度一致。帮你更简单的管理数据库。基于原生 thinkPHP 5.0 命令行工具,融入了 Phinx 的数据库迁移 > >[danger]特别团队协作开发时,这一功能非常方便。 > ## 2. `migration`的安装 >使用`Composer` 进行安装 > ~~~ composer require topthink/think-migration ~~~ >[danger]在安装时一定要注意版本,如果您选择的是ThinkPHP5.0那要么安装的`migration`版本也应为1.*。如果您使的是ThinkPHP5.1,可以直接使用上述命令安装最新版本即可。 > 1.* 版本的安装 ~~~ composer require topthink/think-migration 1.* ~~~ ## 3. `migration`的使用 >安装好后,即可以通过 `ThinkPHP`的命令行进行查看其相关的命令 > ~~~ php think ~~~ ~~~ Available commands: build Build Application Dirs clear Clear runtime file help Displays help for a command list Lists commands make make:controller Create a new resource controller class make:model Create a new model class migrate migrate:breakpoint Manage breakpoints migrate:create Create a new migration migrate:rollback Rollback the last or to a specific migration migrate:run Migrate the database migrate:status Show migration status optimize optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production. optimize:config Build config and common file cache. optimize:route Build route cache. optimize:schema Build database schema cache. seed seed:create Create a new database seeder seed:run Run database seeders ~~~ >[dagner]这时命令中就多数了数据迁移相关的命令 > >[danger]### 使用前,要先配置数据库(为空数据库即可) ### 1. 创建迁移类 >创建迁移类,首字母必须为大写 > ~~~ php think migrate:create Users ~~~ >[danger]`Users` 为表名称(不含前辍) > >第一次执行的时候会确认是否创建迁移目录; >确认之后; >这时会在项目根目录下执行的该命令; >那么在项目跟目录下会看到database/migrations/20170711153001_Users.php; >默认有一个change方法 迁移类实例 ~~~ <?php use Phinx\Migration\AbstractMigration; class Users extends AbstractMigration { /** * Change Method. */ public function change() { // create the table $table = $this->table('users',array('engine'=>'MyISAM')); $table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名,登陆使用')) ->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码')) ->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态')) ->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陆标识')) ->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP')) ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间')) ->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除')) ->addIndex(array('username'), array('unique' => true)) ->create(); } /** * Migrate Up. */ public function up() { } /** * Migrate Down. */ public function down() { } } ~~~ >[danger]注意:在设置数据表字段时会自动添加名为`id`的自增主键,如果想自定义自增主键的话,需要如下操作。 > ~~~php <?php public function change() { $table = $this->table('user',array('engine'=>'MyISAM')); $table->setId('user_id')->setPrimaryKey('user_id') ->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名')) ->create(); } ?> ~~~ >[danger]注意:若不需要自增主键的话,请如下操作 ~~~ <?php public function change() { $table = $this->table('user',array('engine'=>'MyISAM')); $table->setId(false)->setPrimaryKey('user_id') ->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名')) ->create(); } ?> ~~~ >[danger] #### 除了使用`change`方法操作之外,还可以使用`up`和`down`方法来创建。 ~~~ public function up () { $table = $this->table('user',array('engine'=>'MyISAM')); $table->setId('user_id') //关闭自动设置主键 ->setPrimaryKey('user_id') //设置主键 ->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名')) ->create(); } public function down () { $this->dropTable('user'); ~~~ ### 2. 常用命令 #### 1.执行数据迁移( 创建数据表) ~~~ php think migrate:run ~~~ #### 2. 执行回滚(删除重新操作) >[danger]调整表结构时需要执行,执行时会清空表中现在的数据 ~~~ php think migrate:rollback #回滚所有 php think migrate:rollback -t 0 ~~~