Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Allow seeders to be called only once #39812

Merged
merged 1 commit into from
Nov 29, 2021

Conversation

sebastiandedeyne
Copy link
Contributor

This PR adds a callOnce method to the Seeder class to call seeders only once (similar to require_once in PHP).

This makes seeders more composable. Say you have an application with Post and Page models, and they each can belong to a Category.

PostSeeder and PageSeeder expect CategorySeeder to run first to ensure there are categories in the database. The DatabaseSeeder respects this order:

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            CategorySeeder::class,
            PageSeeder::class,
            PostSeeder::class,
        ]);
    }
}

However, you can't run PageSeeder or PostSeeder on their own anymore. Moving $this->call(CategorySeeder::class) to PageSeeder and PostSeeder isn't a good solution either, because then DatabaseSeeder will call CategorySeeder twice.

With callOnce, you call dependent seeders where there necessary.

class PageSeeder extends Seeder
{
    public function run()
    {
        $this->callOnce(CategorySeeder::class);

        // …
}

class PostSeeder extends Seeder
{
    public function run()
    {
        $this->callOnce(CategorySeeder::class);

        // …
}

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            PageSeeder::class,
            PostSeeder::class,
        ]);
    }
}

This enabled developers to cherry pick which seeders they want to run.

An alternative solution would be to add a $before property to seeders.

class PageSeeder extends Seeder
{
    protected $before = [
        CategorySeeder::class,
    ];
}

I opted for callOnce because it's consistent with the existing call method, is more discoverable, and requires less changes.


This is a non-breaking change, but I targeted it towards 9.x since it's a new feature and Laravel 9 isn't too far away. I'll rework this for 8.x it preferred.

I haven't added any tests yet because I want to discuss the API first. If this is something worth merging, I'll update the PR accordingly.

@driesvints driesvints marked this pull request as draft November 29, 2021 13:21
@driesvints
Copy link
Member

Hi there, we've temporarily disabled our PostgreSQL build until we've managed to fix it. I've marked your PR as draft for now. Please rebase with master and mark this PR as ready when all tests pass again. Thanks.

@sebastiandedeyne sebastiandedeyne marked this pull request as ready for review November 29, 2021 16:53
@taylorotwell taylorotwell merged commit 63bb589 into laravel:master Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants