← Code-Übersicht

run-migrations.php

Pfad: run-migrations.php

Ext: php

Größe: 1297 Bytes

Geändert: 2026-07-16T16:59:35+02:00

Frühere Version vom 2026-07-16T16:59:35+02:00 · zur aktuellen Fassung

<?php
declare(strict_types=1);

use Demo\Autoloader;
use Demo\Infrastructure\Config\CredentialsProvider;
use Demo\Infrastructure\Persistence\MigrationRunner;
use Demo\Infrastructure\Persistence\PdoConnectionFactory;

require __DIR__ . '/src/Autoloader.php';
(new Autoloader(__DIR__ . '/src'))->register();

if (PHP_SAPI !== 'cli') {
    fwrite(STDERR, "Nur CLI-Aufruf erlaubt\n");
    exit(2);
}

$baseline = null;
foreach (array_slice($argv, 1) as $argument) {
    if (preg_match('/^--baseline=(\d{1,4})$/', $argument, $match) === 1) {
        $baseline = (int) $match[1];
        continue;
    }
    fwrite(STDERR, "Unbekanntes Argument: {$argument}\n");
    exit(2);
}

$credentialsDir = getenv('APP_CREDENTIALS_DIR') ?: __DIR__ . '/credentials';
$credentials = (new CredentialsProvider($credentialsDir))->database();
$pdo = (new PdoConnectionFactory($credentials))->create();
$runner = new MigrationRunner($pdo, __DIR__ . '/migrations', getenv('USER') ?: 'migration-cli');

try {
    $result = $runner->run($baseline);
    fwrite(STDOUT, json_encode(['ok' => true, 'changes' => $result], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) . "\n");
} catch (Throwable $error) {
    fwrite(STDERR, json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_THROW_ON_ERROR) . "\n");
    exit(1);
}