PHP code example of hustlahusky / migrations

1. Go to this page and download the library: Download hustlahusky/migrations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hustlahusky / migrations example snippets


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
    

    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;
}