letuananh1873/db-exporter

快速轻松地将数据库导出为Laravel迁移和所有数据作为Seeder类。

v1.2.5 2018-09-19 12:27 UTC

README

Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

数据库导出工具

快速轻松地将数据库导出为Laravel迁移和所有数据作为Seeder类。这可以通过Artisan命令或控制器动作完成。

请注意,我仅在MySQL数据库上测试了此包。已确认它不与Postgres兼容

安装

"elimuswift/db-exporter"添加到composer.json中的依赖项

{
    "require": {
        "elimuswift/db-exporter": "*"
    },
}

更新composer

php composer.phar update

对于laravel <=5.4,将服务提供者添加到config/app.php

 Elimuswift\DbExporter\DbExportHandlerServiceProvider::class

(可选)发布配置文件。

php artisan vendor:publish --provider="Elimuswift\DbExporter\DbExportHandlerServiceProvider"

在发布配置文件后,请确保更改迁移和种子存储位置。

使用dev-master作为版本要求,以保持前沿

文档

从命令行

导出数据库到迁移

基本用法

php artisan db-exporter:migrations

指定一个数据库

php artisan db-exporter:migrations otherDatabaseName

忽略表

您可以通过逗号分隔来忽略多个表。

php artisan db-exporter:migrations --ignore="table1,table2"

将数据库表数据导出到种子类

此命令将所有数据库表数据导出到种子类。

php artisan db-exporter:seeds

重要:这需要您在config/database.php中更新数据库配置文件**

将迁移/种子上传到存储磁盘

重要:包的备份目标路径应与您期望的磁盘位置匹配

您可以将迁移和/或种子备份到应用程序支持的存储磁盘。

php artisan db-exporter:backup --migrations

或者将种子上传到生产服务器:

php artisan db-exporter:backup --seeds

或者甚至将两者结合

php artisan db-exporter:backup --migrations --seeds

此功能使用Laravel的文件系统.

您必须配置存储并在配置文件中指定磁盘名称。默认磁盘是本地

导出当前数据库

此类将从您的config/database.php文件中导出数据库名称,基于您的'default'选项。

DbExporter::migrate();

导出自定义数据库

DbExporter::migrate('otherDatabaseName');

数据库到种子

这将写入一个包含当前数据库所有数据的种子类。

DbExporter::seed();

播种自定义数据库

只需传递要播种的数据库名称。

DbExporter::seed('myOtherDB');

接下来,您只需在基种子类中添加调用方法

$this->call('nameOfYourSeedClass');

现在您可以从命令行运行

php artisan db:seed,或者,无需添加调用方法:php artisan db:seed --class=您的种子类名称

链式操作

您还可以将迁移和种子的生成结合起来

DbExporter::migrate()->seed();

或者使用

DbExporter::migrateAndSeed();

忽略表

默认情况下,忽略迁移表。您可以使用以下语法添加要忽略的表

DbExporter::ignore('tableToIgnore')->migrateAndSeed();
DbExporter::ignore('table1','table2','table3')->migrateAndSeed();

您还可以传递一个要忽略的表数组。

从配置文件

忽略种子表

如果您想始终忽略某些表,您可以在配置文件中这样做

return [
    'seeds' => [
        'ignore_tables' => [
            'table_to_ignore1',
            'table_to_ignore2'
        ]
    ]
];

使用此配置,每次执行命令php artisan db-exporter:seeds时,都会忽略数组中的表

仅使用选定的表进行种子

另一方面,如果您想始终使用某些表,您也可以在配置文件中这样做

return [
    'seeds' => [
        'use_tables' => [
            'table_to_ignore1',
            'table_to_ignore2'
        ]
    ]
];

使用此配置,每次执行命令php artisan db-exporter:seeds时,只会执行数组中的表

致谢

感谢@nWidart,这是该包的原始创建者 DbExporter。我无法直接使用它,因此我决定重写该包以适应最新的Laravel版本,并添加了一些我自己的功能。