承接 makeabledk/laravel-production-seeding 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

makeabledk/laravel-production-seeding

最新稳定版本:v3.2.0

Composer 安装命令:

composer require makeabledk/laravel-production-seeding

包简介

README 文档

README

Latest Version on Packagist Build Status StyleCI

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

License

Attribution-ShareAlike 4.0 International. Please see License File for more information.

统计信息

  • 总下载量: 2.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: CC-BY-SA-4.0
  • 更新时间: 2017-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固