A small command-line tool for database migrations, built on top of dakujem/migrun.
piko/migrate provides a simple wrapper around Migrun to run, rollback, and inspect migrations from your terminal.
- Lightweight CLI for migrations
- Built on reliable migration orchestration from
dakujem/migrun - Supports:
- applying pending migrations
- rolling back applied migrations
- displaying migration status
- Configurable migrations directory
- PHP 8+
- A PDO-compatible database (MySQL, SQLite, PostgreSQL, etc.)
- Composer
composer require piko/migrateThen use the generated binary:
./vendor/bin/migrate --helpcomposer install
php ./bin/migrate --helpThe CLI reads database settings from environment variables:
DSNDB_USERNAME(optional depending on DSN)DB_PASSWORD(optional depending on DSN)
You can also create an env.php file in your working directory. If present, it will be loaded automatically.
Example env.php:
<?php
return [
'DSN' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8mb4',
'DB_USERNAME' => 'my_user',
'DB_PASSWORD' => 'my_password',
];By default, migrations are loaded from:
./migrations
You can override this directory with -p / --path.
A migration file should return an object exposing up(PDO $db) and down(PDO $db) methods.
Example:
<?php
return new class {
public function up(PDO $db): void
{
$db->exec('CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(120) NOT NULL)');
}
public function down(PDO $db): void
{
$db->exec('DROP TABLE users');
}
};migrate [options] <command>-h, --helpShow help-p, --pathPath to migration directory (default:./migrations)
migrate runRoll back the latest migration:
migrate rollbackRoll back multiple migrations:
migrate rollback -s 3migrate statusThe status output includes:
up: already applieddown: pendingMISSING: applied in history but migration file is no longer present
Run tests:
composer testRun static analysis and coding style checks:
composer lint