fyre/migration

A database migration library.

v4.0.2 2024-08-20 11:46 UTC

This package is auto-updated.

Last update: 2024-09-20 11:57:36 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;

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.

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.

$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.

$history->add($migration);

All

Get the migration history.

$all = $history->all();

Current

Get the current version.

$version = $history->current();