search
HomePHP FrameworkThinkPHPthinkPHP uses migrate to migrate database

thinkPHP uses migrate to migrate database

May 27, 2020 am 09:08 AM
thinkphp

thinkPHP uses migrate to migrate database

thinkPHP database migration tool: topthink/think-migration

1: Install topthink/think-migration

Note here that you install topthink/ You need to pay attention to your thinkPHP version when thinking-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. Choose the version that suits you to install

composer require topthink/think-migration=2.0.*

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

php think

The following indicates that migrate is installed successfully

thinkPHP uses migrate to migrate database

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

1: Create a migration class

Execute on the command line

php think migrate:create CreateUser

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

thinkPHP uses migrate to migrate database

2: Implement database migration

[1]: migrate code description:

There are three methods in migrate

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

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

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

Under normal circumstances, I usually delete the change method in the migrate file, and the up method specifically places the operations of adding and updating tables. The down method places the delete table and delete field operations

(1) 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();

(2) Update table:

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

(3) Delete table:

$this->table('user')->drop();

(4) Delete field

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

[2]: migrate command:

There are three commonly used commands for migrate, which are:

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

Recommended tutorial: 《

TP5

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

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment