ThinkPHP5 database migration tool migration

Migration file function

The main function of the migration file is to manage the structure of the database. In fact, it is an abstraction of a set of SQL statements. The migration file can create tables, delete tables, add fields, delete fields, and basically all database operations. In fact, this is It's like writing SQL statements manually, except that in migration you don't need to write SQL statements manually, you just need to call them according to its regular syntax.

Introduction to the migrate command
migrate:breakpoint Manage breakpoints
migrate:create Create a migration file
migrate:rollback Roll back the last or specific migration
migrate:run Migrate
the database migrate:status​​​Display migration status

1. Thinkphp5.1 uses migration

thinkphp5 provides developers with a complete set of migration solutions, but migration is not installed by default and we need to install it manually. Switch the working directory to the tp5 project and execute:

composer require topthink/think-migration v3.0.2

Default installation version, more version addresses: https://packagist.org/packages/topthink/think-migration​​

Execute the php think command to check whether the installation is successful

2. create command 

Syntax format: php think migrate:create TableName 

​​TableName​​​ Format: camel case with the first letter capitalized. This command is used to create a migration file. For example, here we create a Video migration file: 

php think migrate:create Video

 There is an additional database directory in the root directory of the project, and there is a migration folder. This folder is used to store migration files. When you open it, you can see the migration file of the Video we just created:

created ./database/migrations/202305060932127_video.php

文件格式命名规则:​​时间 + 随机数 + _ + 文件名​​

After the file is created, let’s take a look at its contents:

<?php

use think\migration\Migrator;
use think\migration\db\Column;

class Video extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {

    }
}

Here I need to add fields to the Video file I created above

​​Video​​ table structure is as follows

Field type illustrate
id int primary key
nickname varchar(16) Video name
email varchar(32) Mail
password varchar64) password

Delete the default ​​change​​​ method and create ​​up()​​​ method and ​​down()​​​ method. The ​​up()​​​ method is executed when executing the ​​run​​​​ command, and ​​down()​​​​ is executed when the ​​rollback​​​ command is executed.

<?php

use think\migration\Migrator;
use think\migration\db\Column;

class Video extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $table = $this->table('video');
        $table->addColumn('name', 'string', ['limit' => 16, 'null' => false,'comment'=>'视频名称'])
        ->addColumn('email', 'string', ['limit' => 32, 'null' => false,'comment'=>'邮箱'])
        ->addColumn('password', 'string', ['limit' => 64, 'null' => false,'comment'=>'密码'])
        ->create();
    }
    public function down()
    {
        $this->dropTable('video');
    }

}

In this way, a migration file that can create a video table is created. Next, let's learn the next command.

3. migrate:run command 

After the migration file is created, you still need to execute the run command to modify the database:

Note, please configure config/database.php correctly before performing this step. The database here is prefixed eb_

php think migrate:run

 After the execution is successful, check the database and there are two more tables ​​eb_migrations​​​ and ​​eb_video​​

I defined migration before and only specified three fields: ​​nickname​​​, ​​email​​​, ​​password​​​, but the table created by executing the ​​run​​​ command has four fields. One more id primary key? This is added by thinkphp5 for us by default! Attention, friends! If your primary key field is named id, you don’t need to specify it manually. 

4. migrate:status command 

Description: Display migration status

# php think migrate:status
 Status  Migration ID    Started              Finished             Migration Name 
----------------------------------------------------------------------------------
     up  20230503032741  2023-05-06 17:12:50  2023-05-06 17:12:50  Video

5. migrate:roolback command

Description: Rollback the last or specific migration 

Perform rollback operation

php think migrate:rollback

At this time, check the database and table structure. The database table has been deleted and no longer exists.

6. Add new table fields
  

For secondary operations on the table, you cannot edit it twice in the same script, you can only create a new script.

Next, add new fields to the table eb_video and regenerate the table structure.

php think migrate:create Video2

created ./database/migrations/202305060932127_video2.php

Video2 file content

<?php

use think\migration\Migrator;

class Video2 extends Migrator
{
    public function up()
    {
        $table = $this->table('video');
        $table->addColumn('upload_time', 'string', ['limit' => 11, 'null' => false,'comment'=>'上传时间'])
            ->addColumn('user_id', 'string', ['limit' => 11, 'null' => false,'comment'=>'上传用户ID'])
            ->save();
    }
}

data migration

php think migrate:run

Guess you like

Origin blog.csdn.net/qq_32450471/article/details/130530203