justinkekeocha/database-dump

此包可以帮助您避免在未导出数据库备份的情况下运行laravel migrate:fresh命令时丢失数据库记录。

4.1 2024-08-22 12:32 UTC

This package is auto-updated.

Last update: 2024-09-22 12:42:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包通过创建数据库备份来增强migrate:fresh命令,允许您进行迁移更改,然后使用以前的数据重新填充数据库。这对于需要在运行迁移之前保留数据的开发者特别有用。

利用内存高效的方法,此包从备份文件中流式传输记录,确保在任何给定时间只有一个记录在内存中。这种方法允许它处理理论上无限大小的文件,而不会耗尽内存。

受phpMyAdmin中的导出功能启发,此包不仅允许您恢复数据,还提供了在播种过程中更改数据的灵活性。

内容

安装

您可以通过composer安装此包

composer require justinkekeocha/database-dump

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="database-dump-config"

以下是发布配置文件的内容

return [

    /*
     *  Enable or disable the package.
    */
    'enable' => true,

    /*
     *  Set the folder generated dumps should be save in.
    */

    'folder' => database_path('dumps/'),

    /*
     *  Set the chunk length of data to be processed at once.
    */
    'chunk_length' => 5000,

    /*
    *  Set the maximum stream length of data to be processed at once.
    *  This is the maximum size a row in a table is expected to have in your database
    *  This is set to a reasonable default of 1MB
    *  If your database rows are larger than this, you may want to increase this value.
    *  Read more: https://php.ac.cn/manual/en/function.stream-get-line.php
    */

    'stream_length' => (2 * 1024 * 1024),
];

用法

备份数据库数据

# Dump database data before running migrations
php artisan migrate:fresh

# Dump database data
php artisan database:dump

使用备份文件填充数据库

从DatabaseSeeder加载备份文件,并通过在播种类中的$this->call方法传递备份表

# database/seeders/DatabaseSeeder.php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use Database\Seeders\UserSeeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {

         $databaseDump = DatabaseDump::getLatestDump("save/2024_04_14_233109.json");

        $this->command->outputComponents()->info("Using dump:  $databaseDump->filePath");


        $this->call([
            UserSeeder::class,
        ], parameters: compact('databaseDump'));
    }
}

备份表数据现在可用在各个播种文件中,您现在可以使用提供的数据填充表

# database/seeders/UserSeeder.php

namespace Database\Seeders;

use App\Models\User;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run($databaseDump): void
    {
        $databaseDump->seed(User::class);

        //You can also use table name instead of model.

        $databaseDump->seed('users');
    }
}

您可以在播种之前操作行

# database/seeders/CountrySeeder.php

namespace Database\Seeders;

use App\Models\Country;

class CountrySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run($databaseDump): void
    {
        $databaseDump->seed(Country::class, formatRowCallback: function ($row) {
                //331.69 ms
                return [
                    'id' => $row['id'],
                    'name' => $row['name'],
                    'code' => 22
                ];

                //OR

                //338.95 ms
                $changes = [
                    'code' => '22'
                ];

                return  collect($row)->only(['id', 'name'])->merge($changes)->all();
    });
    }
}

获取特定备份文件

use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;

//Get dump by position in array of directory listing
//Array starts from latest dump file in specified config('database-dump.folder')
DatabaseDump::getDump(1); //Get second dump in the array.

//Get dump by dump file name
DatabaseDump::getDump("2024_01_08_165939.json");

//Get the latest dump
DatabaseDump::getLatestDump();

填充表

use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use App\Models\Country;
use App\Models\Timezone;
use App\Models\User;

DatabaseDump::getLatestDump()->seed(User::class);

//You can seed multiple tables at once.
DatabaseDump::getLatestDump()->seed(Country::class)
->seed(Timezone::class)
->seed(User::class);

当从同一备份文件进行播种时,在已实例化的类上调用播种方法更有效。这是因为当第一次调用播种方法时,它会读取整个文件,生成一个存储文件中表偏移量的模式,然后开始播种操作。此模式创建是为了后续在同一实例(显然是同一文件)上的播种调用将仅移动到上次找到表的位置并从偏移量开始读取。

use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use App\Models\Country;
use App\Models\Timezone;
use App\Models\User;

//Whole file will be read 3 times
DatabaseDump::getLatestDump()->seed(Country::class);
DatabaseDump::getLatestDump()->seed(Timezone::class);
DatabaseDump::getLatestDump()->seed(User::class);


//Whole file will be read only once.
DatabaseDump::getLatestDump()->seed(Country::class)
->seed(Timezone::class)
->seed(User::class);

示例

示例备份可以在此处找到 这里

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献

安全漏洞

请查阅我们关于如何报告安全漏洞的安全策略 这里

致谢

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅 许可文件