Tp5 migrate database migration tool, tp5migrate
Tp5 is quite different from tp3.2.
Migrate is one of them. Through migrate, programmers can create database modification and rollback operations in php code.
First download the migrate extension and run the command line in the current project directory.
1 composer require topthink/think-migration
Use the php think command to check whether the migrate has been successfully downloaded.
Use migrate: create migrate file name (upper-case method) to generate a migrate file under the database
It is possible that the establishment fails. There is no way. Generally, the tp version obtained by composer is too low. consider modifying the migrate version in the composer. json file to 1. * or ^ 1.0.
Re-composer update.
Configure the database in database. php under application
The following is the content of one of the migrate files (there is a default method change () after creation, delete it)
1 use think \ migration \ Migrator; 2 use think \ migration \ db \ Column; 3 4 class CreateUserTable extends Migrator 5 {6 7/** 8 * create user table 9 */10 public function up () {11 $ table = $ this-> table ('user'); 12 $ table-> addColumn ('username', 'string', ['limit' => 30]) 13-> addColumn ('passwork', 'string', ['limit' => 32]) 14-> addColumn ('email ', 'string ', ['limit' => 25]) 15-> addColumn ('lastlogin _ ip', 'string', ['limit' => 15]) 16-> addTimestamps ('create _ time', 'lastlogin _ Time') 17-> addColumn ('status', 'integer', ['limit '=> 1, 'default' => 1]) 18-> setId ('user _ id') 19-> save (); 20} 21 22/** 23 * provides the rollback user table deletion method 24 */25 public function down () {26 $ this-> dropTable ('user '); 27} 28}
For some of the above methods, I did not see any official documents. I have explained it on the Internet.
Using migrate: run will execute all the up methods of migrate.
Migrate: rollback can be used to roll back the previous execution of the migrate file (with the-t 0 parameter rollback all)
You can use migrate: status to view the current migrate execution status.
After the run method is executed, the user table is successfully created.
Very convenient