Repository navigation
Database migrations
Greg Bowler edited this page Mar 3, 2026
·
8 revisions
Here, we will run migrations as numbered SQL files and let the migrator track progress and integrity.
Default location:
query/_migration/
Example files:
0001-create-user.sql0002-add-created-at.sql0003-index-email.sql
Rules to keep in mind:
- migration files must use the
.sqlextension - numbering must be continuous, with no gaps
- file hashes are stored and checked for integrity
use Gt\Database\Connection\Settings;
use Gt\Database\Migration\Migrator;
$settings = new Settings(__DIR__ . "/query", Settings::DRIVER_SQLITE, __DIR__ . "/app.sqlite");
$migrator = new Migrator($settings, __DIR__ . "/query/_migration");
$migrator->createMigrationTable();
$files = $migrator->getMigrationFileList();
$migrator->checkFileListOrder($files);
$current = $migrator->getMigrationCount();
$migrator->checkIntegrity($files, $current);
$migrator->performMigration($files, $current);Now we can rerun the migration process safely and only apply new files.
This package includes bin/migrate:
php bin/migrate execute
php bin/migrate execute --force--force recreates the schema for non-sqlite drivers.
The command reads project configuration from database.* keys, including:
database.query_pathdatabase.migration_pathdatabase.migration_tabledatabase.driverdatabase.schemadatabase.hostdatabase.portdatabase.usernamedatabase.password
Example code: example/05-database-migrations.php
PHP.GT/Database is a separately maintained component used by PHP.GT/WebEngine.