-
-
Notifications
You must be signed in to change notification settings - Fork 0
Programmatic Usage
You can drive Barbarian directly from PHP, without the CLI — useful for deploy scripts, test bootstrapping or framework integration.
require 'vendor/autoload.php';
use InitPHP\Barbarian\Migrations;
$pdo = new PDO('sqlite:' . __DIR__ . '/app.db');
$migrations = new Migrations($pdo, __DIR__ . '/migrations', [
'namespace' => 'App\\Migrations',
'migrationTable' => 'migrations_versions',
]);Constructing the manager has two side effects:
- It ensures the version table exists (creating it if necessary).
- It scans the folder and discovers the migration classes.
// version name (short class name) => fully-qualified class name
$map = $migrations->getMigrations();
foreach ($map as $version => $class) {
echo $version, ' => ', $class, PHP_EOL;
}
// Problems found while scanning (e.g. files without a valid class):
foreach ($migrations->getErrors() as $error) {
fwrite(STDERR, $error . PHP_EOL);
}A file is reported in getErrors() (and omitted from
getMigrations()) when it does not declare a
class implementing MigrationInterface.
use App\Migrations\Migration_20240101000000;
$migration = new Migration_20240101000000();
$migrations->upMigration($migration); // apply
$migrations->downMigration($migration); // revertBoth methods return bool:
-
upMigration()returnstruewhenup()ran and the state was recorded;falsewhen the migration was already up (and not forced) orup()returnedfalse. -
downMigration()returnstruewhendown()ran and the state was recorded;falsewhen the migration was never applied, is already down (and not forced), ordown()returnedfalse.
foreach ($migrations->getMigrations() as $class) {
$migrations->upMigration(new $class());
}To revert everything safely, iterate in reverse:
foreach (array_reverse($migrations->getMigrations()) as $class) {
$migrations->downMigration(new $class());
}Both methods accept a second bool $force argument to run again even when the
migration is already in the target state:
$migrations->upMigration($migration, true); // re-run up()
$migrations->downMigration($migration, true); // re-run down()use InitPHP\Barbarian\MigrationStatus;
// A single migration (by instance or version name); null = never applied:
$migrations->status($migration); // ?MigrationStatus
$migrations->status('Migration_20240101000000'); // ?MigrationStatus
// Every discovered migration, keyed by version name:
foreach ($migrations->getStatuses() as $version => $status) {
echo $version, ': ', $status?->name ?? 'pending', PHP_EOL;
}This is exactly what the status CLI
command renders.
The recorded state is modelled by MigrationStatus:
use InitPHP\Barbarian\MigrationStatus;
MigrationStatus::Down->value; // 0
MigrationStatus::Up->value; // 1These integers are exactly what is stored in the version table's status
column; comparisons are made on the integer value, so the library does not rely
on the PDO driver returning a particular PHP type.
use InitPHP\Barbarian\Console\ConfigLoader;
use InitPHP\Barbarian\Console\ManagerFactory;
$config = ConfigLoader::fromFile(__DIR__ . '/barbarian.json');
$migrations = ManagerFactory::fromConfig($config);| Failure | Exception |
|---|---|
| Missing folder, unreadable/invalid config, failed connection | MigrationException |
| Invalid table name | InvalidArgumentException |
| Database error while executing a statement | \PDOException |
See Exceptions for the full picture.
initphp/barbarian · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Guide
Reference
Practical Guides
Help