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: docs/1-essentials/03-database.md
+25-11Lines changed: 25 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -452,17 +452,16 @@ When you're persisting objects to the database, you'll need table to store its d
452
452
453
453
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.
454
454
455
-
```php app/CreateBookTable.php
456
-
use Tempest\Database\DatabaseMigration;
455
+
```php
456
+
use Tempest\Database\MigratesUp;
457
457
use Tempest\Database\QueryStatement;
458
458
use Tempest\Database\QueryStatements\CreateTableStatement;
459
-
use Tempest\Database\QueryStatements\DropTableStatement;
460
459
461
-
final class CreateBookTable implements DatabaseMigration
460
+
final class CreateBookTable implements MigratesUp
462
461
{
463
462
public string $name = '2024-08-12_create_book_table';
464
463
465
-
public function up(): QueryStatement|null
464
+
public function up(): QueryStatement
466
465
{
467
466
return new CreateTableStatement('books')
468
467
->primary()
@@ -471,11 +470,6 @@ final class CreateBookTable implements DatabaseMigration
471
470
->datetime('published_at', nullable: true)
472
471
->belongsTo('books.author_id', 'authors.id');
473
472
}
474
-
475
-
public function down(): QueryStatement|null
476
-
{
477
-
return new DropTableStatement('books');
478
-
}
479
473
}
480
474
```
481
475
@@ -493,6 +487,26 @@ The file name of `{txt}.sql` migrations and the `{txt}{:hl-type:$name:}` propert
493
487
494
488
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.
495
489
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
+
496
510
### Applying migrations
497
511
498
512
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
501
515
{:hl-comment:# Applies migrations that have not been run in the current environment:}
502
516
./tempest migrate:up
503
517
504
-
{:hl-comment:#Rolls back every migration:}
518
+
{:hl-comment:#Execute the down migrations:}
505
519
./tempest migrate:down
506
520
507
521
{:hl-comment:# Drops all tables and rerun migrate:up:}
0 commit comments