Documentation · Chapter 07

Version the structure
like code.

A migration is a small ordered step that applies a change with up() and reverses it with down().

We replace reading-list's temporary storage with SQLite without changing the application's purpose.

Create the migration

phpaml — zsh
aml make:migration create_books_table

AML adds a timestamp prefix to the file under runtime/database/migrations. This ordering prevents changes from being applied randomly.

Describe the table

phpaml — zsh
use AML\Data\Connection;
use AML\Data\Migrations\Migration;
use AML\Data\Schema\{Schema, Table};

return new class extends Migration {
    public function up(Connection $connection): void
    {
        (new Schema($connection))->create('books', function (Table $table): void {
            $table->id();
            $table->string('title', 180);
            $table->string('author', 120);
            $table->timestamps();
        });
    }

    public function down(Connection $connection): void
    {
        (new Schema($connection))->dropIfExists('books');
    }
};

Apply, inspect, reverse

phpaml — zsh
aml data:migrate
aml data:status
aml data:rollback --steps 1
aml data:migrate

The migrator remembers what already ran: rerunning data:migrate applies only pending files. A lock prevents two processes from changing the schema simultaneously.

Rollback can mean data loss

Dropping a table in down() also removes its rows. Use rollback in controlled development and always back up production.