hustlahusky/migrations

Framework-agnostic, small migrations library

1.0.1 2023-01-20 08:38 UTC

This package is auto-updated.

Last update: 2024-10-29 12:05:39 UTC


README

hustlahusky/migrations

Framework-agnostic, small migrations library

GitHubPackagistInstallationUsage

Installation

Via Composer

$ composer require hustlahusky/migrations

Usage

use Hustlahusky\Migrations\DefaultNamingStrategy;
use Hustlahusky\Migrations\Migrator;
use Hustlahusky\Migrations\PhpSourceLocator;
use Hustlahusky\Migrations\SourceLocatorInterface;
use function Hustlahusky\Migrations\lock;

/**
 * @var \PDO $pdo
 */

$namingStrategy = new DefaultNamingStrategy();
$sourceLocator = new PhpSourceLocator(MIGRATIONS_DIR, $namingStrategy);

// Look at tests/MysqlPdoStorage.php for storage implementation example
$storage = new MysqlPdoStorage($pdo, 'migrations', $namingStrategy);
$migrator = new Migrator($storage, $sourceLocator);

// Create migration file
$sourceLocator->registerMigration(
    new \DateTimeImmutable(),
    <<<PHP
    <?php

    declare(strict_types=1);
    
    namespace App\Migrations;
    
    use Hustlahusky\Migrations\MigrationInterface;
    
    return new class implements MigrationInterface {
        public function up(): void
        {
        }
        
        public function down(): void
        {
        }
    };
    PHP
);

// Install all new migrations
lock($storage, static fn () => $migrator->install(0));

// Rollback all installed migrations
lock($storage, static fn () => $migrator->rollback(0));

// Rollback last installed migration
lock($storage, static fn () => $migrator->rollback(1));

// List migrations
foreach ($migrator->getIterator() as $info) {
    echo $info->getName()
        . "\t" . $info->getStatus()
        . "\t" . $info->getCreatedAt()->format('Y-m-d H:i:s')
        . "\t" . (null !== $info->getInstalledAt() ? $info->getInstalledAt()->format('Y-m-d H:i:s') : '')
        . "\t" . ($info->sourceModified() ? 'modified' : '')
        . "\t" . ($info->sourceNotFound() ? 'notfound' : '')
        . \PHP_EOL;
}

Credits

License

The MIT License (MIT). Please see license file for more information.