thinkphp5.1 think-migration扩展数据库迁移和数据填充

2020-08-1021:40:59后端程序开发Comments2,052 views字数 3440阅读模式

数据库迁移类似于数据库的SVN,可方便用于团队开发工程中的系统应用数据结构初始化、新建或删除表、新增或修改表字段等操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

Thinkphp提供了think-migration扩展用户数据库的迁移和数据填充文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

数据库迁移

1. 安装和创建迁移类

$ composer require topthink/think-migration=2.0.*
复制代码
$ php think migrate:create Third
复制代码

表名称首字母需大写,如下图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

系统会自动生成迁移类文件:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

2. 编辑操作方法

use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter; //如创建MYSQL特有字段,需导入该命名空间

class Test extends Migrator
{

    public function change()
    {
        $table = $this->table('third', ['engine' => 'InnoDB', 'collation' => 'utf8_bin', 'comment' => '测试表']);
        $table->addColumn('member_id', 'integer', ['limit' => 10, 'signed' => false, 'default' => '0', 'comment' => 'MYSQL:int'])//unsigned:('signed' => false)
                ->addColumn('thirdid', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'signed' => false, 'default' => '0', 'comment' => 'MYSQL:tinyint'])//需导入命名空间
                ->addColumn('platform', 'string', ['limit' => 30, 'default' => '', 'comment' => 'MYSQL:varchar'])
                ->addColumn('fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0', 'comment' => 'decimal'])//decimal(10,2):('precision' => 10, 'scale' => 2)
                ->addColumn('money', 'float', ['precision' => 10, 'scale' => 2, 'default' => '0', 'comment' => 'float'])
                ->addColumn('openid', 'text', ['default' => '', 'comment' => 'text'])
                ->addColumn('content', 'text', ['limit' => MysqlAdapter::TEXT_LONG, 'default' => '', 'comment' => 'MYSQL:longtext'])
                ->addColumn('is_show', 'enum', ['values' => ['false', 'ture'], 'default' => 'false', 'comment' => 'enum'])
                ->addColumn('delete_time', 'integer', ['limit' => 10, 'signed' => false, 'null' => true, 'default' => NULL, 'comment' => 'delete_time'])//null:('null' => true)
                ->addIndex(['member_id'], ['name' => 'member_id'])
                ->addIndex(['thirdid', 'member_id'], ['unique' => true, 'name' => 'thirdid'])
                ->create();

        $rows = [ //插入数据
            [
                'member_id' => 1,
                'platform'  => 'zhifubao',
            ],
            [
                'member_id' => 2,
                'platform'  => 'weixin',
            ],
        ];
        $table->insert($rows)->save();
    }
}
复制代码

或使用updown方法,但使用change方法后,updown方法将被忽略:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

Please be aware that when a change method exists, Phinx will automatically ignore the up and down methods文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

public function up()
{
    // add column type DOUBLE
    $this->execute("ALTER TABLE zxcms_test ADD COLUMN much_money DOUBLE(10,2) unsigned NOT NULL DEFAULT '0.00'");
}

public function down()
{
    $this->dropTable('third');
}
复制代码

3. 执行操作

$ php think migrate:run
复制代码

结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

同时数据库中会生成third表和migrations表,migrations表用于记录本次操作的记录:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

4. 更新数据

再次创建migrate:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

$ php think migrate:create ChangeThird
复制代码

再将需要更新的内容写到change方法中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

public function change()
{
    $table = $this->table('third');
    $table->changeColumn('platform', 'string', ['limit' => 255])
            ->addColumn('password', 'string', array('after' => 'platform','limit' => 32, 'default' => md5('123456'), 'comment' => '用户密码'))
            ->save();
}
复制代码

再次执行操作:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

5. 执行回滚

updown方法提供 migrate:runmigrate:rollback功能,可通过以下命令回滚down中的操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

$ php think migrate:rollback -t third
复制代码

third类文件中的down方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

public function down()
{
	$this->dropTable('third');
}
复制代码

结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

third表被删除:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

数据填充

1. 创建seed

$ php think seed:create Third
复制代码

如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

可看到生成的文件:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

2. 编辑seed文件:

public function run()
{
    $data[] = [
            'member_id' => 1,
            'thirdid' => '1',
            'platform' => 'zhansan',
        ];
    $this->table('third')->insert($data)->save();
}
复制代码

3. 开始进行数据库填充

$ php think seed:run
复制代码

结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

可指定一个或多个seed文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

$ php think seed:run -s Third
$ php think seed:run -s Third -s member

复制代码

使用假数据生成器

参考 fzaninotto/faker文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

安装faker类库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

$ composer require fzaninotto/faker
复制代码

使用faker生成数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

public function run()
{
    $faker = Faker\Factory::create('zh_CN');//选择中文库
    $data = [];
    for ($i = 0; $i < 100; $i++) {
        $data[] = [
            'member_id' => $faker->randomDigit,
			'thirdid' => $faker->randomDigit,
            'platform' => $faker->word,
            'openid' => sha1($faker->password),
            'content' => $faker->realText(50),
        ];
    }
    $this->table('third')->insert($data)->save();
}
复制代码

结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

thinkphp5.1 think-migration扩展数据库迁移和数据填充

作者:iyoung5
链接:https://juejin.im/post/6844904045719584781
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/19982.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/19982.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定