FSCash/migrations/run_migrations.php
fds2610-bot 6f377ea251 V.2.2: Unit-Tests für wichtige Funktionen, automatisch geprüft bei Update
Extrahiert die wichtigste DB-/netzwerkfreie Geschäftslogik (Monatsnavigation,
Admin-/Aktien-Rollenprüfung, Aktien-GuV-Berechnung, OpenFIGI-/Alpha-Vantage-
Antwortauswertung, Eingabevalidierung, CSV-Export, Migrations-Logik) nach
html/lib/fscash.php und deckt sie mit 31 PHPUnit-Tests ab. update.sh
installiert Composer-Abhängigkeiten bei Bedarf und führt die Tests jetzt vor
jedem Deploy aus - bei Fehlschlag wird nichts kopiert oder migriert.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-23 17:54:30 +02:00

61 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../html/lib/fscash.php';
$configPath = $argv[1] ?? '/var/www/html/config.php';
$migrationsDir = __DIR__;
if (!is_file($configPath)) {
fwrite(STDERR, "Config nicht gefunden: $configPath\n");
exit(1);
}
require $configPath; // stellt $pdo bereit
if (!isset($pdo) || !($pdo instanceof PDO)) {
fwrite(STDERR, "config.php hat keine \$pdo-Verbindung geliefert.\n");
exit(1);
}
$pdo->exec(
"CREATE TABLE IF NOT EXISTS schema_migrations (
filename VARCHAR(255) NOT NULL PRIMARY KEY,
applied_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)"
);
$applied = $pdo->query("SELECT filename FROM schema_migrations")->fetchAll(PDO::FETCH_COLUMN);
$files = glob($migrationsDir . '/*.sql') ?: [];
$pending = fscash_pending_migrations($files, $applied);
if (empty($pending)) {
echo "Keine neuen Migrationen.\n";
exit(0);
}
foreach ($pending as $file) {
$name = basename($file);
echo "-> Migration $name ... ";
$sql = file_get_contents($file);
if ($sql === false || trim($sql) === '') {
fwrite(STDERR, "\nMigration $name ist leer oder konnte nicht gelesen werden.\n");
exit(1);
}
try {
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO schema_migrations (filename) VALUES (?)");
$stmt->execute([$name]);
echo "OK\n";
} catch (Throwable $e) {
fwrite(STDERR, "\nFEHLER bei $name: " . $e->getMessage() . "\n");
fwrite(STDERR, "Abbruch. Bereits erfolgreich angewendete Migrationen bleiben markiert, $name muss manuell geprueft werden.\n");
exit(1);
}
}
echo "Alle Migrationen angewendet.\n";