First migrated version
This commit is contained in:
parent
12b2cd91f8
commit
acb7618086
6 changed files with 268 additions and 2 deletions
94
import.php
Normal file
94
import.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
// Import Script für tippspiel2027
|
||||
// Dieses Skript verbindet sich mit einer alten Datenbank (wo der Live-Dump importiert wurde)
|
||||
// und kopiert die relevanten Daten in die neue tippspiel2027 Datenbank.
|
||||
// Tote Features wie "Spieler Ligen" und "grünes Trikot" werden ignoriert.
|
||||
|
||||
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
||||
$user = getenv('DB_USER') ?: 'root';
|
||||
$pass = getenv('DB_PASS') ?: '';
|
||||
$oldDbName = getenv('DB_OLD_NAME') ?: 'tippspiel_legacy';
|
||||
$newDbName = getenv('DB_NEW_NAME') ?: 'tippspiel2027';
|
||||
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;charset=utf8mb4", $user, $pass);
|
||||
$pdo->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");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue