fyre / migration
A database migration library.
Requires
- fyre/command: ^4.0
- fyre/filesystem: ^2.0
- fyre/forge: ^4.0
- fyre/loader: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^10
README
FyreMigration is a free, open-source migration library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/migration
In PHP:
use Fyre\Migration\MigrationRunner;
Migration Runners
Clear
Clear loaded migrations.
MigrationRunner::clear();
Get Connection
Get the Connection.
$connection = MigrationRunner::getConnection();
Get Forge
Get the Forge.
$forge = MigrationRunner::getForge();
Get History
Get the MigrationHistory.
$history = MigrationRunner::getHistory();
Get Migration
Get a Migration.
$version
is a number representing the migration version.
$migration = MigrationRunner::getMigration($version);
Get Migrations
Get all migrations.
$migrations = MigrationRunner::getMigrations();
Get Namespace
Get the namespace.
$namespace = MigrationRunner::getNamespace();
Has Migration
Check if a migration version exists.
$version
is a number representing the migration version.
$hasMigration = MigrationRunner::hasMigration($version);
Migrate
Migrate to a version.
$version
is a number representing the migration version, and will default to null.
MigrationRunner::migrate($version);
Rollback
Rollback to a version.
$version
is a number representing the migration version, and will default to null.
MigrationRunner::rollback($version);
Set Connection
Set the Connection.
$connection
is the Connection.
MigrationRunner::setConnection($connection);
Set Namespace
Set the namespace.
$namespace
is a string representing the migration namespace.
MigrationRunner::setNamespace($namespace);
Migrations
Migrations can be created by extending \Fyre\Migration\Migration
, ensuring all below methods are implemented.
Your migrations must be placed in the same namespace as defined by the setNamespace
method above.
Migration classes should follow the naming convention of Migration_{version}
where {version}
is the version number.
Down
Perform a "down" migration.
$migration->down();
Up
Perform an "up" migration.
$migration->up();
Migration History Registry
use Fyre\Forge\MigrationHistoryRegistry;
Get History
Get the MigrationHistory for a Connection.
$connection
is a Connection.
$history = MigrationHistoryRegistry::getHistory($connection);
Set Handler
Set a MigrationHistory handler for a Connection class.
$connectionClass
is a string representing the Connection class name.$historyClass
is a string representing the MigrationHistory class name.
MigrationHistoryRegistry::setHandler($connectionClass, $historyClass);
Migration Histories
Add
Add a Migration to the history.
$migration
is a Migration.
$history->add($migration);
All
Get the migration history.
$all = $history->all();
Current
Get the current version.
$version = $history->current();