请问大家如果迁移管理一个 3w 条初始数据的数据表?

我有个数据表,默认数据有 3w 条,我用 migrations 创建了数据表结构,这 3w 条数据该怎么管理好呢?
不太想用 seed xx data 里写 3w 条数据的形式去处理,除了这种形式之外,我想着还可以弄个 sql 文件或者 csv 文件,在 seed xx data 里执行导入。
大家有什么其他好的方法吗?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

导出这个表结构和数据的sql文件,然后创建一个command,初始化的时候调用command,导入sql文件

public function __construct()
{
        parent::__construct();

        $this->process = new Process(sprintf(
            'mysql -h%s -u%s -p%s %s < %s',
            config('database.connections.mysql.host'),
            config('database.connections.mysql.username'),
            config('database.connections.mysql.password'),
            config('database.connections.mysql.database'),
            storage_path('app/backups/xxx.sql')
        ));
}
4年前 评论
大胆的番茄 (楼主) 4年前
大胆的番茄 (楼主) 3年前
大胆的番茄 (楼主) 3年前
讨论数量: 9

导出这个表结构和数据的sql文件,然后创建一个command,初始化的时候调用command,导入sql文件

public function __construct()
{
        parent::__construct();

        $this->process = new Process(sprintf(
            'mysql -h%s -u%s -p%s %s < %s',
            config('database.connections.mysql.host'),
            config('database.connections.mysql.username'),
            config('database.connections.mysql.password'),
            config('database.connections.mysql.database'),
            storage_path('app/backups/xxx.sql')
        ));
}
4年前 评论
大胆的番茄 (楼主) 4年前
大胆的番茄 (楼主) 3年前
大胆的番茄 (楼主) 3年前

mysql -u -p

ues database

source /path/sql.sql

4年前 评论

可以考虑用 SQLite 来存储,然后写一个命令,去读数据在写入到目标数据库。

4年前 评论

导出这个表结构和数据的sql文件,然后创建一个command,初始化的时候调用command,导入sql文件

public function __construct()
{
        parent::__construct();

        $this->process = new Process(sprintf(
            'mysql -h%s -u%s -p%s %s < %s',
            config('database.connections.mysql.host'),
            config('database.connections.mysql.username'),
            config('database.connections.mysql.password'),
            config('database.connections.mysql.database'),
            storage_path('app/backups/xxx.sql')
        ));
}
4年前 评论
大胆的番茄 (楼主) 4年前
大胆的番茄 (楼主) 3年前
大胆的番茄 (楼主) 3年前
/**
 * 按行读取文件
 */
if (!function_exists('getLines')) {
    function getLines($file) {
        $f = fopen($file, 'r');
        try {
            while ($line = fgets($f)) {
                yield $line;
            }
        } finally {
            fclose($f);
        }
    }
}

// 迁移文件的 up 函数
foreach (getLines(__DIR__ . '/data.csv') as $n => $line) {
    if ($n === 0) continue;

    $data = explode(',', $line);

    DB::table('test')->insert([
        'name'  => $data[0],
        'email' => $data[1],
    ]);
}
4年前 评论
大胆的番茄 (作者) (楼主) 4年前
黑哈尔

今天刚好要导入一批数据,70多万条,sql文件130多M。
使用最佳答案的方法导入时,因为文件太大返回了MySQL server has gone away的错误。
所以把这个方法稍微改造了一下,加上了 --max_allowed_packet=1024M 选项来配置允许的文件大小。

    /**
     * @void
     */
    public function handle()
    {

        $mysql = trim(`which mysql`);

        if (is_executable($mysql)) {
            $execute_command = sprintf(
                'mysql --max_allowed_packet=1024M -h%s -u%s -p%s %s < %s',
                config('database.connections.mysql.host'),
                config('database.connections.mysql.username'),
                config('database.connections.mysql.password'),
                config('database.connections.mysql.database'),
                storage_path('app/database/cnarea20181031.sql')
            );
            $status = false;
            system($execute_command, $status);
        }else {
            $this->info('未安装mysql客户端');
        }

        $this->info('Area data was successfully inited.');
    }
3年前 评论

咱这时候放下php不行吗,直接mysql操作不香吗? :cry:

3年前 评论

导出表到sql文件

mysqldump -uxxx -pxxx 数据库 表 > /文件位置

导入,可以先登录数据库

mysql -uxxx -p
use 数据库
source /文件位置
3年前 评论

首先,如果这是你个人开发,直接导出sql文件不就行了吗,如果是正规项目,你本身就不该去迁移数据,db:seed看起来只是让你填充项目的初始必需数据而已。

3年前 评论
大胆的番茄 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!