Skip to content

Commit 99bf997

Browse files
authored
chore(docs): update migration docs (#1569)
1 parent b11116a commit 99bf997

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

docs/1-essentials/03-database.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,16 @@ When you're persisting objects to the database, you'll need table to store its d
452452

453453
Thanks to [discovery](../4-internals/02-discovery), `.sql` files and classes implementing the {b`Tempest\Database\DatabaseMigration`} interface are automatically registered as migrations, which means they can be stored anywhere.
454454

455-
```php app/CreateBookTable.php
456-
use Tempest\Database\DatabaseMigration;
455+
```php
456+
use Tempest\Database\MigratesUp;
457457
use Tempest\Database\QueryStatement;
458458
use Tempest\Database\QueryStatements\CreateTableStatement;
459-
use Tempest\Database\QueryStatements\DropTableStatement;
460459

461-
final class CreateBookTable implements DatabaseMigration
460+
final class CreateBookTable implements MigratesUp
462461
{
463462
public string $name = '2024-08-12_create_book_table';
464463

465-
public function up(): QueryStatement|null
464+
public function up(): QueryStatement
466465
{
467466
return new CreateTableStatement('books')
468467
->primary()
@@ -471,11 +470,6 @@ final class CreateBookTable implements DatabaseMigration
471470
->datetime('published_at', nullable: true)
472471
->belongsTo('books.author_id', 'authors.id');
473472
}
474-
475-
public function down(): QueryStatement|null
476-
{
477-
return new DropTableStatement('books');
478-
}
479473
}
480474
```
481475

@@ -493,6 +487,26 @@ The file name of `{txt}.sql` migrations and the `{txt}{:hl-type:$name:}` propert
493487

494488
Note that when using migration classes combined with query statements, Tempest will take care of the SQL dialect for you, there's support for MySQL, Postgresql, and SQLite. When using raw sql files, you'll have to pick a hard-coded SQL dialect, depending on your database requirements.
495489

490+
### Up- and down migrations
491+
492+
Tempest's recommendation is to only use up-migrations, which move the database's schema forward. There is also the option to create down-migrations, migrations that can roll back the schema of the database to a previous state. Dealing with down migrations is tricky, though, especially in production environments. That's why you need to explicitly implement another interface to do so: {`Tempest\Database\MigratesDown`}.
493+
494+
```php
495+
use Tempest\Database\MigratesDown;
496+
use Tempest\Database\QueryStatement;
497+
use Tempest\Database\QueryStatements\DropTableStatement;
498+
499+
final class CreateBookTable implements MigratesDown
500+
{
501+
public string $name = '2024-08-12_drop_book_table';
502+
503+
public function down(): QueryStatement
504+
{
505+
return new DropTableStatement('books');
506+
}
507+
}
508+
```
509+
496510
### Applying migrations
497511

498512
A few [console commands](../3-console/02-building-console-commands) are provided to work with migrations. They are used to apply, rollback, or erase and re-apply them. When deploying your application to production, you should use the `php tempest migrate:up` to apply the latest migrations.
@@ -501,7 +515,7 @@ A few [console commands](../3-console/02-building-console-commands) are provided
501515
{:hl-comment:# Applies migrations that have not been run in the current environment:}
502516
./tempest migrate:up
503517

504-
{:hl-comment:# Rolls back every migration:}
518+
{:hl-comment:# Execute the down migrations:}
505519
./tempest migrate:down
506520

507521
{:hl-comment:# Drops all tables and rerun migrate:up:}

0 commit comments

Comments
 (0)