ThinkPHP 数据迁移使用

ThinkPHP数据库迁移工具可以将数据库结构和数据很容易的在不同的数据库之间管理迁移,使用起来也很方便。

composer require topthink/think-migration  //如果是 TP5 可能出现错误要安装 1.0版本
#composer require topthink/think-migration=1.0.* 
//执行命令,创建一个操作文件,一定要用大驼峰写法,如下
php think migrate:create AnyClassNameYouWant

执行后就会在项目根目录多一个database目录自动创建一个文件,就可以在里面添加你想要的操作了

可以使用 php think ,查看相关的命令,命令如下

ThinkPHP 数据迁移使用

php think migrate:create 创建迁移文件

php think migrate:run 运行迁移文件

php think migrate:rollback 回滚

php think migrate:status 查看迁移状态

迁移文件示例

<?php

use think\migration\Migrator;
use think\migration\db\Column;
 
class  AnyClassNameYouWant 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()
    {
        // create the table
        $table  =  $this->table('users',array('engine'=>'MyISAM'));
        $table->addColumn('username', 'string',array('limit'  =>  15,'default'=>'','comment'=>'用户名,登陆使用'))
        ->addColumn('password', 'string',array('limit'  =>  32,'default'=>md5('123456'),'comment'=>'用户密码')) 
        ->addColumn('login_status', 'boolean',array('limit'  =>  1,'default'=>0,'comment'=>'登陆状态'))
        ->addColumn('login_code', 'string',array('limit'  =>  32,'default'=>0,'comment'=>'排他性登陆标识'))
        ->addColumn('last_login_ip', 'integer',array('limit'  =>  11,'default'=>0,'comment'=>'最后登录IP'))
        ->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间'))
        ->addColumn('is_delete', 'boolean',array('limit'  =>  1,'default'=>0,'comment'=>'删除状态,1已删除'))
        ->addIndex(array('username'), array('unique'  =>  true))
        ->create();
    }

}

字段相关设置可以参考下面

如果你想设置int 类型可以这样写
->addColumn('a', 'integer', ['limit' => '10','default' => 0,'signed'=>false,'comment'=>''])

如果你想设置bigint 类型可以这样写
->addColumn('g', 'biginteger', ['limit' => '10','default' => 0,'signed'=>false,'comment'=>'备注'])

如果你想设置tinyint类型可以这样写
->addColumn('h', 'integer', ['limit' => '255','default' => 0,'signed'=>false,'comment'=>''])

如果你想设置decimal类型可以这样写
->addColumn('j', 'decimal', ['precision'=>4,'scale'=>'2','default' => '0.00','comment'=>''])

如果你想设置set类型可以这样写
->addColumn('i', 'set', ['values' => ['1','2'],'default'=>'1','comment'=>''])

如果你想设置text类型可以这样写
->addColumn('k', 'text', ['default' => '','comment'=>''])

如果你想设置longtext类型可以这样写
->addColumn('l', 'text', ['limit' => '4294967295','default' => '','comment'=>''])

设置唯一索引 unique
->addIndex('order_id', array('unique' => true))

原创文章,作者:admin,如若转载,请注明出处:https://ntib.cn/320.html

(0)
adminadmin
上一篇 2023年11月22日 下午11:09
下一篇 2023年12月5日

相关推荐

发表回复

登录后才能评论
WeChat