snowbuilds/laravel-seeder-reset

Reset Laravel seeders before running

0.0.3 2023-08-13 23:38 UTC

This package is auto-updated.

Last update: 2024-04-14 01:10:18 UTC


README

Laravel SeederReset Package Logo

Latest Version on Packagist Total Downloads GitHub Actions

Introduction

Prompt developers to truncate tables and delete old data before executing seeders. Great for seeding projects that prohibit duplicate data!

Laravel SeederReset Package Logo

Installation

You can install the package via composer:

composer require snowbuilds/laravel-seeder-reset

Usage

Include the SnowBuilds\SeederReset\Concerns\SeederTruncate trait in your seeder class. Next time you run the seeder, you will be prompted to truncate seeders. When choose to truncate, the specified models are truncated before running the seeder:

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
    use SeederTruncate;

    public $truncate = [
        App\Models\User::class,
        App\Models\Comment::class,
        App\Models\Post::class,
    ];

    public function run () {
        //
    }
}

List tables to truncate

When the truncate property is not enough, you can return an array from the getTruncate method:

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
    use SeederTruncate;

    public function getTruncate() {
        return [
            App\Models\User::class,
            App\Models\Comment::class,
            App\Models\Post::class,
        ]
    };

    public function run () {
        //
    }
}

Reset Seeders - Call Truncate

Sometimes you may have seeders which call other seeders. If you only want to be prompted once you can invoke the truncate method:

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use SeederTruncate;

    public function run () {
        $this->truncate([
            UserSeeder::class,
            RecipeSeeder::class,
        ]);

        $this->call([
            UserSeeder::class,
            RecipeSeeder::class,
        ]);
    }
}

Reset Seeders - Replace Call

If you are looking for something a little more implicit, you can replace the call method with the reset method, which will reset each seeder before invoking:

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use SeederTruncate;

    public function run () {
        $this->reset([
            UserSeeder::class,
            RecipeSeeder::class,
        ]);
    }
}

Override Seeder

If you want to keep the same API but want to call other seeders we recommend extending SnowBuilds\SeederReset\Seeder:

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use SnowBuilds\SeederReset\Seeder;

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

Hooks

Sometimes truncating is not enough, and you need delete specific rows before seeding. We included beforeTruncate and afterTruncate hooks which can be used to delete whatever you want. If the hook fails the operation will rollback.

Important The truncate operation is not performed in a transaction due to MySql limitations. If a hook fails, tables that were truncated will remain empty.

use SnowBuilds\SeederReset\Concerns\SeederTruncate;

use SnowBuilds\SeederReset\Seeder;

class DatabaseSeeder extends Seeder
{
    public function beforeTruncate()
    {
        User::moderators()->delete();
        User::customers()->delete();
    }

    public function run () {
        $this->call([
            UserSeeder::class,
            RecipeSeeder::class,
        ]);
    }
}

Roadmap

  • Truncate tables from list of models
  • Truncate using table names
  • Delete data using queries

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email dev@snowlaboratory.com instead of using the issue tracker.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Credits

License

The MIT License (MIT). Please see License File for more information.