Home  >  Article  >  Web Front-end  >  Detailed explanation of migrate database migration tool in thinkphp5

Detailed explanation of migrate database migration tool in thinkphp5

小云云
小云云Original
2018-05-21 11:23:085297browse

tp5 is very different from tp3.2

migrate is one of them. Through migrate, programmers can create database modification rollback and other operations in the PHP code.

First download the migrate extension and execute the command line in the current project directory

composer require topthink/think-migration

You can check whether migrate is successfully downloaded by using the php think command

Use migrate:create migrate file name (capital camel case method) to generate the migrate file under the database

It is possible that the creation fails and prompts that there is no method. Generally, the tp version obtained by composer is too low, consider modifying it. The migrate version in the composer.json file is 1.* or ^1.0

Re-composer update

In database.php under application Configure the database

The following is the content of one of the migrate files (there is a default method change() after creation, delete it)

 use think\migration\Migrator;
 use think\migration\db\Column;
 
 class CreateUserTable extends Migrator
 {
   
   /**
   * 建立用户表
   */
   public function up(){
     $table = $this->table('user');
     $table->addColumn('username' , 'string' , ['limit' => 30])
        ->addColumn('passwork' , 'string' , ['limit' => 32])
        ->addColumn('email' , 'string' , ['limit' => 25])
        ->addColumn('lastlogin_ip' , 'string' , ['limit' => 15])
        ->addTimestamps('create_time' , 'lastlogin_time')
        ->addColumn('status' , 'integer' , ['limit' => 1 , 'default' => 1])
        ->setId('user_id')
        ->save();
   }
   
   /**
   * 提供回滚的删除用户表方法
   */
   public function down(){
     $this->dropTable('user');
   }
 }

Some of the above methods, the official document I I didn’t see it. What I saw on the Internet was Xiao Teng’s explanation.

Using migrate:run will execute all the up methods of migrate.

You can roll back the last execution through migrate:rollback. migrate file (with -t 0 parameter to roll back all)

You can check the current migration execution status through migrate: status

After executing the run method, the user table is successfully created

Very convenient

Related recommendations:

Microsoft launches MySQL to SQL Server database migration tool

The above is the detailed content of Detailed explanation of migrate database migration tool in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn