-
-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A five-minute tour. Every snippet runs as-is. We will use the CLI; the same thing is possible from code.
vendor/bin/barbarian initThis writes a barbarian.json template into the current directory and
creates the migrations/ folder it points at:
{
"dsn": "mysql:host=localhost;dbname=test;charset=utf8mb4",
"username": "root",
"password": "",
"folder": "/path/to/migrations/",
"table_name": "migrations_versions",
"namespace": null
}Edit it to match your database. See Configuration for every key.
vendor/bin/barbarian create
# -> Migration created: Migration_20240101000000create never connects to the database — it only needs the configuration to
know where and under which namespace to write the file. The generated class
is empty:
<?php
declare(strict_types=1);
use InitPHP\Barbarian\MigrationAbstract;
use InitPHP\Barbarian\QueryInterface;
class Migration_20240101000000 extends MigrationAbstract
{
public function up(QueryInterface $query): bool
{
return true;
}
public function down(QueryInterface $query): bool
{
return true;
}
}public function up(QueryInterface $query): bool
{
$query->query('CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
return true;
}
public function down(QueryInterface $query): bool
{
$query->query('DROP TABLE IF EXISTS `users`');
return true;
}Returning true tells Barbarian to record the change. Returning false (or
throwing) leaves the recorded state untouched — see Writing Migrations.
vendor/bin/barbarian status [pending] Migration_20240101000000
pending means the migration has been discovered but never applied.
vendor/bin/barbarian up # runs up() for every pending/down migration
# -> Migration_20240101000000::up() has been executed.
vendor/bin/barbarian status
# -> [up] Migration_20240101000000
vendor/bin/barbarian down # runs down() in reverse order
# -> Migration_20240101000000::down() has been executed.
vendor/bin/barbarian status
# -> [down] Migration_20240101000000You can also target a single migration by timestamp or full class name:
vendor/bin/barbarian up --version=20240101000000
vendor/bin/barbarian down --version=Migration_20240101000000require 'vendor/autoload.php';
use InitPHP\Barbarian\Migrations;
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
$migrations = new Migrations($pdo, __DIR__ . '/migrations', [
'namespace' => null,
'migrationTable' => 'migrations_versions',
]);
foreach ($migrations->getMigrations() as $class) {
$migrations->upMigration(new $class());
}- Writing Migrations — the contract in detail.
- CLI Commands — every command and flag.
- Programmatic Usage — driving the manager directly.
- Database Drivers — MySQL, SQLite, PostgreSQL.
initphp/barbarian · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Guide
Reference
Practical Guides
Help