elimuswift/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

对于 <=5.4 的laravel,将服务提供者添加到 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"

将数据库表数据导出到 Seeder 类

此命令将导出您的所有数据库表数据到一个 Seeder 类。

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');

数据库到 Seeder

这将写入一个 Seeder 类,其中包含当前数据库的所有数据。

DbExporter::seed();

播种自定义数据库

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

DbExporter::seed('myOtherDB');

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

$this->call('nameOfYourSeedClass');

现在您可以从命令行运行

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

链式调用

您也可以合并迁移和种子的生成

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

或者

DbExporter::migrateAndSeed();

忽略表

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

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

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

从配置文件

忽略 Seeder 的表

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

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

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

仅使用所选表为 Seeder

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

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

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

鸣谢

感谢 @nWidart,这是包 DbExporter 的原始创作者。我无法让它按原样工作,因此决定重新编写包以适应最新的 Laravel 版本,并添加了一些自己的功能。