Code前端首页关于Code前端联系我们

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

terry 2年前 (2023-09-25) 阅读数 55 #后端开发

数据库迁移类似于数据库的SVN,可以方便地用于系统应用数据结构初始化、新增或删除表、新增或更改表字段等。在团队发展项目中运作。

Thinkphp 提供think-migration扩展用户数据库迁移和数据填充

数据库迁移

1。安装并创建迁移类

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

表名首字母必须大写,如下所示: thinkphp5.1 think-migration扩展数据库迁移和数据填充

系统会自动生成迁移类文件: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将被忽略:

请注意,当自动存在更改方法时,当更改方法时,phin方法将被忽略3。执行

$ php think migrate:run
复制代码

操作的结果如下: thinkphp5.1 think-migration扩展数据库迁移和数据填充

同时在数据库s中生成了third表和migration表。表migrations就是用来记录这个时间的。操作记录:thinkphp5.1 think-migration扩展数据库迁移和数据填充

4。更新数据

重新创建迁移:

$ php think migrate:create ChangeThird
复制代码

然后将要更新的内容写入方法changethinkphp5.1 think-migration扩展数据库迁移和数据填充

change❀ethinkphp5.1 think-migration扩展数据库迁移和数据填充

5.要执行回滚

updown 方法提供 migrate:run 和 mmm 功能,可以通过以下命令访问回滚 中的操作down

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

third down类文件中的方法:♿d如下:

表被删除:thinkphp5.1 think-migration扩展数据库迁移和数据填充

数据填充

1。创建种子

$ php think seed:create Third
复制代码

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

可以看到生成的文件:thinkphp5.1 think-migration扩展数据库迁移和数据填充

2。编辑种子文件:

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

3。开始填充数据库

$ php think seed:run
复制代码

结果如下:thinkphp5.1 think-migration扩展数据库迁移和数据填充

可以指定一个或多个种子

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

复制代码

使用假数据生成器

参见fzaninotto/faker❓安装❓❀❀类库

$ composer require fzaninotto/faker
复制代码

使用faker生成的数据

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();
}
复制代码

结果如下:thinkphp5.1 think-migration扩展数据库迁移和数据填充

作者:iyoung5jin4/4jin/4 https://9♝jin/4 https:/ /9斤/4 . 719584781
来源:掘金
版权归作者所有。商业转载请联系作者获取授权。非商业转载请注明出处。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门