承接 garanaw/seedable-migrations 相关项目开发

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

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

garanaw/seedable-migrations

Composer 安装命令:

composer require garanaw/seedable-migrations

包简介

Allows to self seed migrations in a more controlled way

README 文档

README

Tests Linting

About

Laravel provides a migration system with seeding. However, the seeding system is not very flexible. This package provides a more flexible way to seed your database with seed files linked to migrations.

You can run your seeding after each migration, or at the end of the batch. You can also specify seeder files that should run on the UP method or the DOWN method of your migrations.

Install

To install the package, run the following command:

composer require garanaw/seedable-migrations

After installing the package, you need to publish the migration stubs:

php artisan vendor:publish --provider="Garanaw\SeedableMigrations\SeedableMigrationsServiceProvider"

If you don't want to publish the stubs, you will need to extend your migrations manually from the Garanaw\SeedableMigrations\Migration class.

The new migration file

The migration files will look a bit different now when you run the command php artisan make:migration. The new migration files will look like this:

<?php

declare(strict_types=1);

use Garanaw\SeedableMigrations\Blueprint;
use Garanaw\SeedableMigrations\Enum\SeedAt;
use Garanaw\SeedableMigrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $this->schema->create(table: $this->getTable(), callback: static function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    public function getTable(): string
    {
        return $this->table;
    }

    public function seedAt(): SeedAt
    {
        return SeedAt::EACH;
    }

    public function down(): void
    {
        $this->schema->dropIfExists($this->getTable());
    }
}

A few things to note here:

  • The schema now is a property of the migration class. If you prefer the facade, you can still use it with Schema::create().
  • The up() and down() now have a return typehint of void. This is not mandatory, but it is recommended.
  • Two new methods are added: getTable() and seedAt(). The getTable() method is used to get the table name of the migration.
  • The seedAt() method is used to specify when the seeder should run. You can return SeedAt::EACH to run the seeder after each migration, or SeedAt::BATCH to run the seeder at the end of the batch. If you don't want to run the seeder, you can return SeedAt::NONE.

The new seeder file

The seeder files will look a bit different now when you run the command php artisan make:seeder. The new seeder files will look like this:

<?php

declare(strict_types=1);

use Garanaw\SeedableMigrations\Seeder;

return MySeeder extends Seeder
{
    public function run(): bool
    {
        // Your seeder code here
    }

    public function getData(): array
    {
        return [];
    }
}

A few things to note here:

  • The run() method is now mandatory. This is where you should put your seeder code.
  • The getData() method is used to return the data that should be seeded. This method is mandatory now as it follows an interface. If you don't want to seed anything, you can return an empty array.
  • The run() method now has a return typehint of bool. This is mandatory, as it follows an interface, and it's used to store the failed seeders.
  • If your seeder contains a method called configure(), it will be called through the Container::call(). This means that you can use dependency injection in your seeder class to add extra configuration if needed.
  • Alternatively, you can create a __construct() method in your seeder class. This method will be called before the run() method, and you can use dependency injection in it, but it will require the parameters from the parent class.

How to use

To run your migrations, you can use the php artisan migrate command as usual. The seeding will be done automatically.

To specify the seeder files associated to a migration, you will need to add some methods to the migration file:

    public function seedersUp(): \Illuminate\Support\Collection
    {
        return collect([
            MySeeder::class,
        ]);
    }

    public function seedersDown(): \Illuminate\Support\Collection
    {
        return collect([
            MySeeder::class,
        ]);
    }

The seedersUp() method is used to specify the seeder files that should run on the UP method of the migration. The seedersDown() method is used to specify the seeder files that should run on the DOWN method of the migration. None of them is mandatory. If you don't specify any seeder file, the seeder will not run.

Another optional method is the shouldSeed() method. This method is used to specify if the seeder should run or not. This method is useful if you want to run the seeder only on specific environments. The default implementation is:

    public function shouldSeed(): bool
    {
        return true;
    }

Within the shouldSeed() method, you can use the app()->environment() method to check the environment, or any other logic to determine if the seeder should run or not.

To configure your seeder, you only need to add a configure() method to your seeder class:

    public function configure(
        \Illuminate\Config\Repository $config,
        \Illuminate\Foundation\Application $app,
        \Illuminate\Contracts\Console\Kernel $artisan,
        \Illuminate\Contracts\Debug\ExceptionHandler $exceptionHandler,
        App\Providers\RouteServiceProvider $routeServiceProvider,
        \Psr\Log\LoggerInterface $logger,
    ): void {
        // Your configuration code here. You can inject anything that is configured in your container
    }

Security Vulnerabilities

If you discover a security vulnerability, please create an issue using the issue tracker.

License

The Seedable Migrations library is open-sourced software licensed under the MIT license.

garanaw/seedable-migrations 适用场景与选型建议

garanaw/seedable-migrations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「migrations」 「migration」 「laravel」 「seed」 「seeder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 garanaw/seedable-migrations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 garanaw/seedable-migrations 我们能提供哪些服务?
定制开发 / 二次开发

基于 garanaw/seedable-migrations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-03