Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "project",
"description": "Documentation website for the Tempest framework",
"require": {
"tempest/framework": "dev-main",
"tempest/framework": "dev-database-seeders",
"league/commonmark": "^2.7.0",
"symfony/yaml": "^7.3.1",
"spatie/yaml-front-matter": "^2.1",
Expand Down
27 changes: 13 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion src/Web/Documentation/content/1.x/1-essentials/03-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
```

:::warning
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.
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.
:::

## Database seeders

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.

```php
use Tempest\Database\DatabaseSeeder;
use UnitEnum;

final class BookSeeder implements DatabaseSeeder
{
public function run(null|string|UnitEnum $database): void
{
query(Book::class)
->insert(
title: 'Timeline Taxi',
)
->onDatabase($database)
->execute();
}
}
```

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.

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.

```console
./tempest database:seed
./tempest migrate:fresh --seed
```

### Multiple seeders

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.

Whenever you have multiple seeder classes, Tempest will prompt you which ones to run:

```console
./tempest database:seed

│ <em>Which seeders do you want to run?</em>
│ / <dim>Filter...</dim>
│ → ⋅ Tests\Tempest\Fixtures\MailingSeeder
│ ⋅ Tests\Tempest\Fixtures\InvoiceSeeder
```

Both the `database:seed` and `migrate:fresh` commands also allow to pick one specific seeder or run all seeders automatically.

```console
./tempest database:seed --all
./tempest database:seed --seeder="Tests\Tempest\Fixtures\MailingSeeder"

./tempest migrate:fresh --seed --all
./tempest migrate:fresh --seeder="Tests\Tempest\Fixtures\MailingSeeder"
```

### Seeding on multiple databases

Seeders have built-in support for multiple databases, which you can specify with the `--database` option. Continue reading to learn more about multiple databases.

```console
./tempest database:seed --database="backup"
./tempest migrate:fresh --database="main"
```

## Multiple databases

Tempest supports connecting to multiple databases at once. This can, for example, be useful to transfer data between databases or build multi-tenant systems.
Expand Down
67 changes: 66 additions & 1 deletion src/Web/Documentation/content/main/1-essentials/03-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,74 @@ You may use the `migrate:rehash` command to bypass migration integrity checks an
```

:::warning
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.
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.
:::

## Database seeders

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.

```php
use Tempest\Database\DatabaseSeeder;
use UnitEnum;

final class BookSeeder implements DatabaseSeeder
{
public function run(null|string|UnitEnum $database): void
{
query(Book::class)
->insert(
title: 'Timeline Taxi',
)
->onDatabase($database)
->execute();
}
}
```

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.

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.

```console
./tempest database:seed
./tempest migrate:fresh --seed
```

### Multiple seeders

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.

Whenever you have multiple seeder classes, Tempest will prompt you which ones to run:

```console
./tempest database:seed

│ <em>Which seeders do you want to run?</em>
│ / <dim>Filter...</dim>
│ → ⋅ Tests\Tempest\Fixtures\MailingSeeder
│ ⋅ Tests\Tempest\Fixtures\InvoiceSeeder
```

Both the `database:seed` and `migrate:fresh` commands also allow to pick one specific seeder or run all seeders automatically.

```console
./tempest database:seed --all
./tempest database:seed --seeder="Tests\Tempest\Fixtures\MailingSeeder"

./tempest migrate:fresh --seed --all
./tempest migrate:fresh --seeder="Tests\Tempest\Fixtures\MailingSeeder"
```

### Seeding on multiple databases

Seeders have built-in support for multiple databases, which you can specify with the `--database` option. Continue reading to learn more about multiple databases.

```console
./tempest database:seed --database="backup"
./tempest migrate:fresh --database="main"
```

## Multiple databases

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