setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Erstelle neue Datenbank $newDbName falls nicht vorhanden...\n"; $pdo->exec("CREATE DATABASE IF NOT EXISTS `$newDbName` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); // Neues Schema laden $pdo->exec("USE `$newDbName`"); $schema = file_get_contents(__DIR__ . '/schema.sql'); if ($schema) { $pdo->exec($schema); echo "Neues Schema erfolgreich in $newDbName angelegt.\n"; } else { die("Fehler: schema.sql nicht gefunden.\n"); } echo "Starte Import von $oldDbName nach $newDbName...\n"; $tablesToMigrate = [ 'users', 'settings', 'seasons', 'rounds', 'games', 'tipps', 'payments', 'quotas', 'roundmails', 'manual_payouts' ]; foreach ($tablesToMigrate as $table) { echo "Migriere Tabelle: $table...\n"; // Prüfen, ob Tabelle in der alten DB existiert $tableExists = $pdo->query("SELECT 1 FROM information_schema.tables WHERE table_schema = '$oldDbName' AND table_name = '$table'")->fetchColumn(); if (!$tableExists) { echo " Überspringe $table, existiert in $oldDbName nicht.\n"; continue; } // Leeren der neuen Tabelle $pdo->exec("TRUNCATE TABLE `$newDbName`.`$table`"); // Alle Daten aus alter DB holen $rows = $pdo->query("SELECT * FROM `$oldDbName`.`$table`")->fetchAll(PDO::FETCH_ASSOC); if (count($rows) === 0) { echo " Keine Daten in $table.\n"; continue; } // Holen der Spalten der NEUEN Tabelle (um Legacy Felder zu ignorieren) $newColsStmt = $pdo->query("DESCRIBE `$newDbName`.`$table`"); $newCols = $newColsStmt->fetchAll(PDO::FETCH_COLUMN); $inserted = 0; foreach ($rows as $row) { $filteredRow = []; foreach ($newCols as $col) { if (array_key_exists($col, $row)) { $filteredRow[$col] = $row[$col]; } } if (!empty($filteredRow)) { $cols = '`' . implode('`, `', array_keys($filteredRow)) . '`'; $vals = implode(', ', array_fill(0, count($filteredRow), '?')); $stmt = $pdo->prepare("INSERT INTO `$newDbName`.`$table` ($cols) VALUES ($vals)"); $stmt->execute(array_values($filteredRow)); $inserted++; } } echo " $inserted Einträge migriert.\n"; } echo "Import erfolgreich abgeschlossen!\n"; } catch (PDOException $e) { die("Datenbankfehler: " . $e->getMessage() . "\n"); }