Skip to content

Schema Files

A schema file returns a callable that receives a Schema instance.

<?php

use Schemage\DSL\Schema;
use Schemage\DSL\Table;

return function (Schema $schema): void {
    // Define the desired schema here.
};

The schema file is the source of truth. Schemage compares it with the current database and calculates the operations required to make both schemas match.

Defining tables

$schema->table('users', function (Table $t): void {
    $t->id();
    $t->string('name');
});
$schema->table('posts', function (Table $t): void {
    $t->id();

    $t->bigInt('user_id')
        ->index('posts_user_id_index')
        ->constrained('users', 'id', 'posts_user_id_foreign')
        ->cascadeOnDelete();

    $t->string('title');
    $t->text('body');
});

Renaming tables

$schema->table('feedback', function (Table $t): void {
    $t->id();
    $t->text('body');
})->renamed('comments');

Table renames are explicit. Schemage does not infer them from similar table structures.