fyre/migration

A database migration library.

v5.0.2 2024-11-08 15:08 UTC

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;

Basic Usage

$runner = new MigrationRunner($container, $loader, $connectionManager, $forgeRegistry);

Autoloading

It is recommended to bind the MigrationRunner to the Container as a singleton.

$container->singleton(MigrationRunner::class);

Any dependencies will be injected automatically when loading from the Container.

$runner = $container->use(MigrationRunner::class);

Methods

Clear

Clear loaded migrations.

$runner->clear();

Get Connection

Get the Connection.

$connection = $runner->getConnection();

Get Forge

Get the Forge.

$forge = $runner->getForge();

Get History

Get the MigrationHistory.

$history = $runner->getHistory();

Get Migration

Get a Migration.

  • $version is a number representing the migration version.
$migration = $runner->getMigration($version);

Get Migrations

Get all migrations.

$migrations = $runner->getMigrations();

Get Namespace

Get the namespace.

$namespace = $runner->getNamespace();

Has Migration

Determine whether a migration version exists.

  • $version is a number representing the migration version.
$hasMigration = $runner->hasMigration($version);

Migrate

Migrate to a version.

  • $version is a number representing the migration version, and will default to null.
$runner->migrate($version);

Rollback

Rollback to a version.

  • $version is a number representing the migration version, and will default to null.
$runner->rollback($version);

Set Connection

Set the Connection.

$runner->setConnection($connection);

Set Namespace

Set the namespace.

  • $namespace is a string representing the migration namespace.
$runner->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 Histories

Add

Add a migration version to the history.

  • $version is a number representing the migration version.
$history->add($version);

All

Get the migration history.

$all = $history->all();

Current

Get the current version.

$version = $history->current();

Commands

Migrate

Perform database migrations.

  • --db is the ConnectionManager config key, and will default to ConnectionManager::DEFAULT.
  • --version is the migration version, and will default to null.
$commandRunner->run('db:migrate', ['--db', 'default', '--version', '2']);

Rollback

Perform database rollbacks.

  • --db is the ConnectionManager config key, and will default to ConnectionManager::DEFAULT.
  • --version is the migration version, and will default to null.
$commandRunner->run('db:rollback', ['--db', 'default', '--version', '1']);