justinkekeocha/database-dump
Composer 安装命令:
composer require justinkekeocha/database-dump
包简介
This package will save you from loosing database records, supposing you run the laravel migrate:fresh command without exporting a database dump
README 文档
README
This package enhances the migrate:fresh command by creating a dump of your database, allowing you to make migration changes and then re-seed the database with the previous data. This is particularly useful for developers who need to preserve their data before running migrations.
Utilizing a memory-efficient method, this package streams records from the dump file, ensuring only one record is in memory at any given time. This approach allows it to handle theoretically infinite file sizes without exhausting memory.
Inspired by the export function in phpMyAdmin, this package not only enables you to restore your data but also provides the flexibility to alter the data during the seeding process.
Contents
Installation
You can install the package via composer:
composer require justinkekeocha/database-dump
You can publish the config file with:
php artisan vendor:publish --tag="database-dump-config"
These are the contents of the published config file:
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://www.php.net/manual/en/function.stream-get-line.php */ 'stream_length' => (2 * 1024 * 1024), ];
Usage
Dump database data
# Dump database data before running migrations php artisan migrate:fresh # Dump database data php artisan database:dump
Seed database with dump file
Load dump file from DatabaseSeeder and pass the dump tables through the $this->call method in the seeder class:
# 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')); } }
The dump tables data are now available in individual seeder files and you can now seed the table with the data provided:
# 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'); } }
You can manipulate the rows before seeding:
# 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(); }); } }
Get specific dump file
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();
Seed table
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);
When seeding from the same dump file, it is more efficient to call the seed method on the already instantiated class. This is because when the seed method is called first, it reads the whole file and generates a schema that stores the offset of the tables in the file before it starts the seeding action. This schema is created so subsequent seed calls on the same instance (obviously the same file) will just move to the file offset where the table was last found and start reading from the offset.
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);
Sample
Sample dump can be found here
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
justinkekeocha/database-dump 适用场景与选型建议
justinkekeocha/database-dump 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 16, 最近一次更新时间为 2023 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「database-dump」 「justinkekeocha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 justinkekeocha/database-dump 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 justinkekeocha/database-dump 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 justinkekeocha/database-dump 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 141
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-31