75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
// Einmalige Schema-Migration für bestehende Datenbanken:
|
|
// - users.bankname, users.blz und users.kontonr entfernen
|
|
// - users.paypal ergänzen
|
|
//
|
|
// Ab sofort gelten für Auszahlungen nur noch IBAN und PayPal.
|
|
//
|
|
// Aufruf (im App-Container):
|
|
// docker compose exec app php migrate_users_paypal.php [datenbank]
|
|
//
|
|
// Standard-Ziel: DB_NEW_NAME, Standard "tippspiel2027".
|
|
// Das Skript ist idempotent und kann gefahrlos mehrfach ausgeführt werden.
|
|
|
|
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
|
$user = getenv('DB_USER') ?: 'root';
|
|
$pass = getenv('DB_PASS') ?: '';
|
|
$targetDb = $argv[1] ?? (getenv('DB_NEW_NAME') ?: 'tippspiel2027');
|
|
|
|
if (!preg_match('/^[A-Za-z0-9_]+$/', $targetDb)) {
|
|
die("Fehler: Ungültiger Datenbankname '$targetDb'.\n");
|
|
}
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;charset=utf8mb4", $user, $pass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$existsStmt = $pdo->prepare(
|
|
"SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?"
|
|
);
|
|
$existsStmt->execute([$targetDb]);
|
|
if (!$existsStmt->fetchColumn()) {
|
|
die("Fehler: Datenbank '$targetDb' nicht gefunden.\n");
|
|
}
|
|
|
|
$columns = $pdo->query("SHOW COLUMNS FROM `$targetDb`.`users`")->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$changes = [];
|
|
if (!in_array('paypal', $columns, true)) {
|
|
$changes[] = "ADD COLUMN `paypal` varchar(255) DEFAULT NULL AFTER `iban`";
|
|
}
|
|
if (in_array('bankname', $columns, true)) {
|
|
$changes[] = "DROP COLUMN `bankname`";
|
|
}
|
|
if (in_array('blz', $columns, true)) {
|
|
$changes[] = "DROP COLUMN `blz`";
|
|
}
|
|
if (in_array('kontonr', $columns, true)) {
|
|
$changes[] = "DROP COLUMN `kontonr`";
|
|
}
|
|
|
|
if (empty($changes)) {
|
|
echo "Schema von '$targetDb'.users ist bereits aktuell. Keine Änderungen nötig.\n";
|
|
exit(0);
|
|
}
|
|
|
|
$droppedBankFields = array_intersect(['bankname', 'blz', 'kontonr'], $columns);
|
|
|
|
$sql = "ALTER TABLE `$targetDb`.`users` " . implode(', ', $changes);
|
|
$pdo->exec($sql);
|
|
|
|
echo "Migration in '$targetDb'.users abgeschlossen:\n";
|
|
foreach ($changes as $change) {
|
|
echo " - $change\n";
|
|
}
|
|
if (!empty($droppedBankFields)) {
|
|
echo "Hinweis: " . implode(', ', $droppedBankFields) . " wurden unwiderruflich entfernt. "
|
|
. "Falls die Daten noch benötigt werden, vorher export_accounts.php ausführen.\n";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Datenbankfehler: " . $e->getMessage() . "\n");
|
|
} catch (\Throwable $e) {
|
|
die("Fehler: " . $e->getMessage() . "\n");
|
|
}
|