FSCash/migrations/run_migrations.php
fds2610-bot cf7371fb40 Deploy-Skript update.sh mit DB-Migrationssystem hinzugefügt
Kopiert den git-Stand nach /var/www/html (config.php bleibt unangetastet),
prüft PHP-Syntax vorab und wendet ausstehende Migrationen aus migrations/
über eine schema_migrations-Tracking-Tabelle an. Tests werden ausgeführt,
sobald welche existieren.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-23 15:18:03 +02:00

63 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
$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') ?: [];
sort($files, SORT_STRING);
$pending = array_values(array_filter(
$files,
fn(string $f): bool => !in_array(basename($f), $applied, true)
));
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";