You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Web/Documentation/content/1.x/1-essentials/03-database.md
+66-1Lines changed: 66 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
503
503
```
504
504
505
505
:::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.
507
507
:::
508
508
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.
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
+
509
574
## Multiple databases
510
575
511
576
Tempest supports connecting to multiple databases at once. This can, for example, be useful to transfer data between databases or build multi-tenant systems.
Copy file name to clipboardExpand all lines: src/Web/Documentation/content/main/1-essentials/03-database.md
+66-1Lines changed: 66 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
503
503
```
504
504
505
505
:::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.
507
507
:::
508
508
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.
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
+
509
574
## Multiple databases
510
575
511
576
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