makeabledk/laravel-production-seeding
Composer 安装命令:
composer require makeabledk/laravel-production-seeding
包简介
README 文档
README
This package provides a handy way to work with production seeding in Laravel.
Consider you have database tables you want to keep in-sync across environments. For instance some configuration table where you need to ensure a fixed number of rows, but not necessarily overwrite the values.
Laravel Production Seeding helps you achieve this in a styleful manner!
Inspired by Edward Coleridge Smith's Laracon talk on the subject: How to avoid database migration hell (YouTube)
--
Makeable is web- and mobile app agency located in Aarhus, Denmark.
Install
You can install this package via composer:
composer require makeabledk/laravel-production-seeding
Usage
Create a Laravel seeder as you normally would. Next implement the SyncStrategy, and apply the fixed rows onto your model from the run method.
Finally you can run the seeder as part of your deployment process to ensure all environments implements the same basic blueprint.
Examples
Syncing a configuration table
Sync a database configuration table with an array-blueprint. Note how we hardcode the service-id, but allow for separate keys.
class ConfigSeeder extends Seeder { use \Makeable\ProductionSeeding\SyncStrategy; public $rows = [ [ 'key' => 'some_service_id', 'value' => '123456' ], [ 'key' => 'some_service_public_key', ], [ 'key' => 'some_service_private_key', ], ]; public function run() { $this->apply($this->rows, Config::class, 'key'); } }
Results in
| id | key | value |
|----|--------------------------|--------|
| 1 | some_service_id | 123456 |
| 2 | some_service_public_key | NULL |
| 3 | some_service_private_key | NULL |
Syncing rows with an order
Sometimes you may want to have a fixed list of items and then apply a certain order in the database.
This can easily be achieved with a AppendsSortOrder trait. Re-arranging the array items will change the order value in the database, but leave the id's intact.
class LanguageSeeder extends Seeder { use \Makeable\ProductionSeeding\SyncStrategy, \Makeable\ProductionSeeding\AppendsSortOrder; protected $sortKey = 'order'; // default if omitted public $rows = [ [ 'code' => 'en_GB', 'name' => 'English (GB)', ], [ 'code' => 'en_US', 'name' => 'English (US)', ], [ 'code' => 'da_DK', 'name' => 'Danish', ], ]; public function run() { $this->apply($this->rows, LanguagesModel::class, 'code'); } }
Results in...
| id | code | name | order |
|----|-------|--------------|-------|
| 1 | en_GB | English (GB) | 1 |
| 2 | en_US | English (US) | 2 |
| 3 | da_DK | Danish | 3 |
You could then re-arrange the array and run the seeder again. Then you could have...
| id | code | name | order |
|----|-------|--------------|-------|
| 1 | en_GB | English (GB) | 2 |
| 2 | en_US | English (US) | 1 |
| 3 | da_DK | Danish | 3 |
Pretty cool, right?
Appending a configuration table
Using previous sync-strategy, any rows manually added to the database table would be deleted on seeding.
If this is not the behaviour you wish for, you may instead use AppendStrategy which would only append the new rows.
Let's assume our previous config table has an existing row:
| id | key | value |
|----|--------------------------|------------|
| 1 | github_username | makeabledk |
Next we setup our ConfigSeeder with an AppendStrategy:
class ConfigSeeder extends Seeder { use \Makeable\ProductionSeeding\AppendStrategy; public $rows = [ [ 'key' => 'some_service_id', 'value' => '123456' ], [ 'key' => 'some_service_public_key', ], [ 'key' => 'some_service_private_key', ], ]; public function run() { $this->apply($this->rows, Config::class, 'key'); } }
Now our table would have the existing row plus the 3 new ones:
| id | key | value |
|----|--------------------------|------------|
| 1 | github_username | makeabledk |
| 2 | some_service_id | 123456 |
| 3 | some_service_public_key | NULL |
| 4 | some_service_private_key | NULL |
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
You can run the tests with:
composer test
Contributing
We are happy to receive pull requests for additional functionality. Please see CONTRIBUTING for details.
Credits
- Rasmus Christoffer Nielsen
- Edward Coleridge Smith: How to avoid database migration hell (YouTube)
- All Contributors
License
Attribution-ShareAlike 4.0 International. Please see License File for more information.
makeabledk/laravel-production-seeding 适用场景与选型建议
makeabledk/laravel-production-seeding 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.74k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 makeabledk/laravel-production-seeding 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makeabledk/laravel-production-seeding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: CC-BY-SA-4.0
- 更新时间: 2017-06-27