tp5 数据库迁移工具 migrate&seed
github地址:https://github.com/top-think/think-migration/tree/master
tp5.0时使用migrate工具,composer安装
composer require topthink/think-migration=1.*
注意,tp5.0对应1版本的migration工具,tp5.1及以上对应2版本的migration工具
查看指令#
php think
创建migrate文件(用来建表)
php migrate create:SqUserLogs
在applicaton/database/migrations 文件夹下会创建一个文件 20200318060333_sq_user_logs.php
<?php
use think\migration\Migrator;
use think\migration\db\Column; class SqUserLogs 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() { $table = $this->table('sq_user_logs',array('engine'=>'InnoDB '));//InnoDB //MyISAM $table ->addColumn('uid', 'integer', array('comment'=>'用户在users表中的id')) ->addColumn('access_time','integer',array('comment'=>'出入时间')) ->addColumn('access_status','boolean',array('default'=>0,'comment'=>'出入状态,0表示出门,1表示回家')) ->addIndex(array('id'), array('unique'=>true)) ->create(); } /** * Migrate Up. */ public function up() { // 创建表格时 } /** * Migrate Down. */ public function down() { // 删除表格时 } }
官方文档不能看了,可以通过源码学习使用,在 项目目录/vendor/topthink/thinkmigration/src/command 中可以找到源码
创建seed文件(用来填充测试数据)
php think seed:create SqUserLogs
在application/database/seeds 文件夹下会创建文件 SqUserLogs.php
<?php
use think\migration\Seeder; class SqUserLogs extends Seeder { /** * Run Method. * * Write your database seeder using this method. * * More information on writing seeders is available here: * http://docs.phinx.org/en/latest/seeding.html */ public function run() { $data = [ [1, 1], [2, 2], [1, 1], [3, 2], [1, 1], [2, 2] ]; foreach($data as $v){ $this->table('sq_user_logs')->insert([ 'uid'=>$v[0], 'access_time'=>time() + 360, 'access_status'=>$v[1] ])->save(); } } }
作者:BigBender
出处:https://www.cnblogs.com/BigBender/p/12635731.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!