Home > PHP Framework > ThinkPHP > body text

How thinkPHP uses migrate to implement database migration

藏色散人
Release: 2020-07-29 13:41:45
forward
3928 people have browsed it

The following tutorial column of thinkphp framework will introduce to you how thinkPHP uses migrate to implement database migration. I hope it will be helpful to friends in need!

How thinkPHP uses migrate to implement database migration

thinkPHP uses migrate to implement database migration

thinkPHP’s database migration tool: topthink/think-migration

1: Install topthink/think-migration

Note here that you need to pay attention to your thinkPHP version when installing topthink/think-migration. My thinkPHP version here is 5.1, so you can install version 2.0 of topthink/think-migration. , version 3.0 cannot be installed, select the version that suits you to install

composer require topthink/think-migration=2.0.*
Copy after login

After the installation is completed, execute on the command line:

php think
Copy after login

The following indicates that the migrate installation is successful

How thinkPHP uses migrate to implement database migration

2: Use topthink/think-migration to implement database migration

1: Create a migration class

Execute on the command line

php think migrate:create CreateUser
Copy after login

After the execution is completed, we will Create a migrate migration file in the ./database/migrateions directory

How thinkPHP uses migrate to implement database migration

#2: Implement database migration

migrate method usage documentation: http://docs. phinx.org/en/latest/migrations.html

[1]: Migrate code description:

There are three methods in migrate

up: when migrate:run Execution (provided that the change method does not exist in the file)

down: Execute when migrate:rollback (provided that the change method does not exist in the file)

change:migrate:run and migrate:rollback (If this method exists, up and down will not be executed)

Generally, I delete the change method in the migrate file. The up method is specifically used to add and update tables, and the down method Place delete table and delete field operations

(1)Add new table:

// create the table
$table = $this->table('user', ['id' => 'user_id', 'comment' => '用户表', 'engine' => 'MyISAM', '']);
$table->addColumn('user_name', 'string', ['limit' => 15, 'default' => '', 'comment' => '用户名'])
    ->addColumn('password', 'string', ['limit' => 15, 'default' => '', 'comment' => '密码',])
    ->addColumn('status', 'boolean', ['limit' => 1, 'default' => 0, 'comment' => '状态'])
    ->addIndex(['user_name'], ['unique' => true])//为user_name创建索引并设置唯一(唯一索引)
    ->addTimestamps()//默认生成create_time和update_time两个字段
    ->create();
Copy after login

(2)Update table:

$this->table('user')
    ->addColumn('test', 'string', ['limit' => 15, 'default' => '', 'comment' => '测试'])//在user表中增加一个test字段
    ->update();
Copy after login

(3)Delete table:

$this->table('user')->drop();
Copy after login

(4) Delete field

$this->table('user')
    ->removeColumn('test')//删除user表中的test字段
    ->save();
Copy after login

[2]: migrate command:

There are three commonly used commands for migrate, namely:

php think migrate:create CreateUser  #创建一个迁移类
php think migrate:run  #执行迁移
php think migrate:rollback #迁移回滚
Copy after login

The above is the detailed content of How thinkPHP uses migrate to implement database migration. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!