Skip to content

Commit c635fb5

Browse files
authored
Add database seeder docs (#96)
1 parent 7e18e84 commit c635fb5

File tree

4 files changed

+146
-17
lines changed

4 files changed

+146
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "project",
44
"description": "Documentation website for the Tempest framework",
55
"require": {
6-
"tempest/framework": "dev-main",
6+
"tempest/framework": "dev-database-seeders",
77
"league/commonmark": "^2.7.0",
88
"symfony/yaml": "^7.3.1",
99
"spatie/yaml-front-matter": "^2.1",

composer.lock

Lines changed: 13 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Web/Documentation/content/1.x/1-essentials/03-database.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
503503
```
504504

505505
:::warning
506-
Note that deliberately bypassing migration integrity checks may result in a broken database state. Only use this command when absolutely necessary, if you are confident that your migration files are correct and consistent accross environments.
506+
Note that deliberately bypassing migration integrity checks may result in a broken database state. Only use this command when necessary if you are confident that your migration files are correct and consistent across environments.
507507
:::
508508

509+
## Database seeders
510+
511+
Whenever you need to fill your database with dummy data, you can provide database seeders. These are classes that are used to fill your database with whatever data you want. To get started, you should implement the `\Tempest\Database\DatabaseSeeder` interface.
512+
513+
```php
514+
use Tempest\Database\DatabaseSeeder;
515+
use UnitEnum;
516+
517+
final class BookSeeder implements DatabaseSeeder
518+
{
519+
public function run(null|string|UnitEnum $database): void
520+
{
521+
query(Book::class)
522+
->insert(
523+
title: 'Timeline Taxi',
524+
)
525+
->onDatabase($database)
526+
->execute();
527+
}
528+
}
529+
```
530+
531+
Note how the `$database` property is passed into the `run()` method. In case a user has specified a database for this seeder to run on, this property will reflect that choice.
532+
533+
Running database seeders can be done in two ways: either via the `database:seed` command, or via the `migrate:fresh` command. Not that `database:seed` will always append the seeded data on the existing database.
534+
535+
```console
536+
./tempest database:seed
537+
./tempest migrate:fresh --seed
538+
```
539+
540+
### Multiple seeders
541+
542+
If you want to, you can create multiple seeder classes. Each seeder class could be used to bring the database into a specific state, or you could use multiple seeder classes to seed specific parts of your database.
543+
544+
Whenever you have multiple seeder classes, Tempest will prompt you which ones to run:
545+
546+
```console
547+
./tempest database:seed
548+
549+
│ <em>Which seeders do you want to run?</em>
550+
│ / <dim>Filter...</dim>
551+
│ → ⋅ Tests\Tempest\Fixtures\MailingSeeder
552+
│ ⋅ Tests\Tempest\Fixtures\InvoiceSeeder
553+
```
554+
555+
Both the `database:seed` and `migrate:fresh` commands also allow to pick one specific seeder or run all seeders automatically.
556+
557+
```console
558+
./tempest database:seed --all
559+
./tempest database:seed --seeder="Tests\Tempest\Fixtures\MailingSeeder"
560+
561+
./tempest migrate:fresh --seed --all
562+
./tempest migrate:fresh --seeder="Tests\Tempest\Fixtures\MailingSeeder"
563+
```
564+
565+
### Seeding on multiple databases
566+
567+
Seeders have built-in support for multiple databases, which you can specify with the `--database` option. Continue reading to learn more about multiple databases.
568+
569+
```console
570+
./tempest database:seed --database="backup"
571+
./tempest migrate:fresh --database="main"
572+
```
573+
509574
## Multiple databases
510575

511576
Tempest supports connecting to multiple databases at once. This can, for example, be useful to transfer data between databases or build multi-tenant systems.

src/Web/Documentation/content/main/1-essentials/03-database.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
503503
```
504504

505505
:::warning
506-
Note that deliberately bypassing migration integrity checks may result in a broken database state. Only use this command when absolutely necessary, if you are confident that your migration files are correct and consistent accross environments.
506+
Note that deliberately bypassing migration integrity checks may result in a broken database state. Only use this command when necessary if you are confident that your migration files are correct and consistent across environments.
507507
:::
508508

509+
## Database seeders
510+
511+
Whenever you need to fill your database with dummy data, you can provide database seeders. These are classes that are used to fill your database with whatever data you want. To get started, you should implement the `\Tempest\Database\DatabaseSeeder` interface.
512+
513+
```php
514+
use Tempest\Database\DatabaseSeeder;
515+
use UnitEnum;
516+
517+
final class BookSeeder implements DatabaseSeeder
518+
{
519+
public function run(null|string|UnitEnum $database): void
520+
{
521+
query(Book::class)
522+
->insert(
523+
title: 'Timeline Taxi',
524+
)
525+
->onDatabase($database)
526+
->execute();
527+
}
528+
}
529+
```
530+
531+
Note how the `$database` property is passed into the `run()` method. In case a user has specified a database for this seeder to run on, this property will reflect that choice.
532+
533+
Running database seeders can be done in two ways: either via the `database:seed` command, or via the `migrate:fresh` command. Not that `database:seed` will always append the seeded data on the existing database.
534+
535+
```console
536+
./tempest database:seed
537+
./tempest migrate:fresh --seed
538+
```
539+
540+
### Multiple seeders
541+
542+
If you want to, you can create multiple seeder classes. Each seeder class could be used to bring the database into a specific state, or you could use multiple seeder classes to seed specific parts of your database.
543+
544+
Whenever you have multiple seeder classes, Tempest will prompt you which ones to run:
545+
546+
```console
547+
./tempest database:seed
548+
549+
│ <em>Which seeders do you want to run?</em>
550+
│ / <dim>Filter...</dim>
551+
│ → ⋅ Tests\Tempest\Fixtures\MailingSeeder
552+
│ ⋅ Tests\Tempest\Fixtures\InvoiceSeeder
553+
```
554+
555+
Both the `database:seed` and `migrate:fresh` commands also allow to pick one specific seeder or run all seeders automatically.
556+
557+
```console
558+
./tempest database:seed --all
559+
./tempest database:seed --seeder="Tests\Tempest\Fixtures\MailingSeeder"
560+
561+
./tempest migrate:fresh --seed --all
562+
./tempest migrate:fresh --seeder="Tests\Tempest\Fixtures\MailingSeeder"
563+
```
564+
565+
### Seeding on multiple databases
566+
567+
Seeders have built-in support for multiple databases, which you can specify with the `--database` option. Continue reading to learn more about multiple databases.
568+
569+
```console
570+
./tempest database:seed --database="backup"
571+
./tempest migrate:fresh --database="main"
572+
```
573+
509574
## Multiple databases
510575

511576
Tempest supports connecting to multiple databases at once. This can, for example, be useful to transfer data between databases or build multi-tenant systems.

0 commit comments

Comments
 (0)