Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvandertuijn committed Oct 5, 2024
1 parent f2897b1 commit 1fbe94c
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 299 deletions.
59 changes: 51 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

![Laravel After Seeders](https://cdn.davidvandertuijn.nl/github/laravel-after-seeders.png)

This library adds seeder functionality with ***versioning*** support for [Laravel](https://laravel.com/), making it ideal for a ***production environment***. Seeders are stored in the *database/after_seeders* directory in JSON format. The execution progress of each seeder is tracked in the after_seeders table, ensuring that each seeder is only run once.
This library adds seeder functionality with ***versioning*** support for [Laravel](https://laravel.com/), making it ideal for a ***production environment***. Seeders are stored in the *database/after_seeders* directory in JSON format. The execution progress of each seeder is tracked in the after_seeders table, ensuring that each seeder is only seed once.

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/davidvandertuijn)

Expand Down Expand Up @@ -37,7 +37,7 @@ php artisan vendor:publish --provider="Davidvandertuijn\LaravelAfterSeeders\Serv
Use the command below to create a complete seeder based on existing records in a database table.

```shell
php artisan after-seeders:generate my_table
php artisan after-seeders:make my_table
```

The command will prompt you to include or exclude specific columns from the table. If you skip a column, it won’t be included in the seeder file. You can also define a range of record IDs to include.
Expand All @@ -57,7 +57,7 @@ Enter the ending ID [13]: 13
After completion, you’ll get the following message:

```
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... SUCCESS
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... DONE
```

And the seeder will look like this:
Expand All @@ -79,16 +79,16 @@ And the seeder will look like this:

### Create a New Seeder Manually

Use the following command to generate an empty “after seeder” file. This option is ideal if you’re already familiar with the JSON structure and prefer to manually input the data.
Use the following command to create an empty “after seeder” file. This option is ideal if you’re already familiar with the JSON structure and prefer to manually input the data.

```shell
php artisan after-seeders:make my_table
php artisan after-seeders:placeholder my_table
```

You’ll see the following output upon success:

```
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... SUCCESS
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... DONE
```

A basic structure for the JSON file will look like this:
Expand All @@ -110,7 +110,7 @@ If you use [Navicat for MySQL](https://www.navicat.com/en/products/navicat-for-m
To execute pending “after seeders,” run the following command:

```shell
php artisan after-seeders:run
php artisan after-seeders:seed
```

The command checks whether the specified table and columns exist. If not, the seeder will be skipped.
Expand All @@ -119,7 +119,7 @@ After completion, you’ll get the following message:

```
Run batch "X".
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... SUCCESS
database/after_seeders/YYYY_MM_DD_XXXXXX_my_table.json ... 1.23ms DONE
```

---
Expand All @@ -131,3 +131,46 @@ If the table has a ***created_at*** column and it’s missing from the seeder, t
***Update Or Insert***

If the seeder contains an ***id*** column, the [updateOrInsert](https://laravel.com/docs/10.x/queries#update-or-insert) method will be used. Otherwise, the [insert](https://laravel.com/docs/10.x/queries#insert-statements) method will be applied.

### Tags

You can use tags to organize and manage your seeders more effectively. By adding a tag, you can group seeders and seed specific groups when needed.

#### Create a Seeder with a Tag

To generate a seeder based on existing records and assign a tag, use the --tag option:

```shell
php artisan after-seeders:make my_table --tag=my_tag
```

Similarly, you can create an empty seeder and assign a tag:

```shell
php artisan after-seeders:placeholder my_table --tag=my_tag
```

Running Seeders by Tag

When running seeders, you can specify a tag to seed only the seeders that belong to that group:

```shell
php artisan after-seeders:seed --tag=my_tag
```
This allows you to selectively execute seeders based on tags, making it easier to control which seeders are applied in different environments or scenarios.

### Deployment

When deploying your application, you can execute seeders with specific tags using the deployment command. Before doing this, you’ll need to define the tags in your project’s configuration file located at *config/after_seeders.php.*

```php
'tags' => [
...
],
```

To run the seeders during deployment, simply use the following command:

```shell
php artisan after-seeders:deploy
```
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"Davidvandertuijn\\LaravelAfterSeeders\\ServiceProvider"
]
}
},
"support": {
"url": "https://www.buymeacoffee.com/davidvandertuijn"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

use Exception;

class InvalidJson extends Exception {}
class ColumnNotFoundException extends Exception {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

use Exception;

class TableNotFound extends Exception {}
class ColumnsNotAddedException extends Exception {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

use Exception;

class ColumnNotFound extends Exception {}
class InvalidJsonException extends Exception {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

use Exception;

class ColumnsNotAdded extends Exception {}
class TableNotFoundException extends Exception {}
10 changes: 6 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Davidvandertuijn\LaravelAfterSeeders;

use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Generate as GenerateCommand;
use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Deploy as DeployCommand;
use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Make as MakeCommand;
use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Run as RunCommand;
use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Placeholder as PlaceholderCommand;
use Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders\Seed as SeedCommand;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;

Expand Down Expand Up @@ -39,9 +40,10 @@ public function boot()
// Commands

$this->commands([
GenerateCommand::class,
DeployCommand::class,
MakeCommand::class,
RunCommand::class,
PlaceholderCommand::class,
SeedCommand::class,
]);
}
}
52 changes: 52 additions & 0 deletions src/app/Console/Commands/AfterSeeders/Deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Davidvandertuijn\LaravelAfterSeeders\app\Console\Commands\AfterSeeders;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;

class Deploy extends Command
{
/**
* @var string
*/
protected $signature = 'after-seeders:deploy';

/**
* @var string
*/
protected $description = 'Deploy after seeders';

/**
* Handle.
*/
public function handle(): void
{
$tags = $this->getTags();

if (empty($tags)) {
$this->components->warn('There are no tags available.');

return;
}

foreach ($tags as $tag) {
$this->components->info(sprintf(
'Deployment tag "%s"',
$tag
));

$this->call('after-seeders:seed', [
'--tag' => $tag,
]);
}
}

/**
* Get Tags.
*/
public function getTags()
{
return Config::get('after_seeders.tags');
}
}
Loading

0 comments on commit 1fbe94c

Please sign in to comment.