94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Bereinigt das Datenbankschema (Laravel-Reste, ungenutzte/redundante Felder).
|
||
|
|
*
|
||
|
|
* php bin/migrate.php
|
||
|
|
*
|
||
|
|
* Das Skript ist idempotent und kann gefahrlos mehrfach ausgeführt werden.
|
||
|
|
* Es entfernt:
|
||
|
|
* - Tabellen: migrations, failed_jobs, galleries, gallery_items, password_resets
|
||
|
|
* - Spalte users.email_verified_at, users.remember_token
|
||
|
|
* - Spalte tours.status (redundant zu tours.is_cancelled)
|
||
|
|
* - Typ von tours.user_id: VARCHAR -> INT
|
||
|
|
*/
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/src/bootstrap.php';
|
||
|
|
|
||
|
|
$pdo = \App\Core\Database::get()->pdo();
|
||
|
|
|
||
|
|
function columnExists(PDO $pdo, string $table, string $column): bool
|
||
|
|
{
|
||
|
|
$stmt = $pdo->prepare(
|
||
|
|
'SELECT COUNT(*) FROM information_schema.COLUMNS
|
||
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?'
|
||
|
|
);
|
||
|
|
$stmt->execute([$table, $column]);
|
||
|
|
|
||
|
|
return (int) $stmt->fetchColumn() > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
function tableExists(PDO $pdo, string $table): bool
|
||
|
|
{
|
||
|
|
$stmt = $pdo->prepare(
|
||
|
|
'SELECT COUNT(*) FROM information_schema.TABLES
|
||
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?'
|
||
|
|
);
|
||
|
|
$stmt->execute([$table]);
|
||
|
|
|
||
|
|
return (int) $stmt->fetchColumn() > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
$out = function (string $msg): void {
|
||
|
|
echo " - $msg\n";
|
||
|
|
};
|
||
|
|
|
||
|
|
echo "Starte Datenbank-Bereinigung...\n";
|
||
|
|
|
||
|
|
// Unbenutzte Tabellen entfernen
|
||
|
|
foreach (['migrations', 'failed_jobs', 'galleries', 'gallery_items', 'password_resets'] as $table) {
|
||
|
|
if (tableExists($pdo, $table)) {
|
||
|
|
$pdo->exec("DROP TABLE `$table`");
|
||
|
|
$out("Tabelle '$table' entfernt");
|
||
|
|
} else {
|
||
|
|
$out("Tabelle '$table' existiert nicht (übersprungen)");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Redundante/unbenutzte Spalten entfernen
|
||
|
|
foreach ([
|
||
|
|
['users', 'email_verified_at'],
|
||
|
|
['users', 'remember_token'],
|
||
|
|
['tours', 'status'],
|
||
|
|
] as [$table, $column]) {
|
||
|
|
if (columnExists($pdo, $table, $column)) {
|
||
|
|
$pdo->exec("ALTER TABLE `$table` DROP COLUMN `$column`");
|
||
|
|
$out("Spalte '$table.$column' entfernt");
|
||
|
|
} else {
|
||
|
|
$out("Spalte '$table.$column' existiert nicht (übersprungen)");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// tours.user_id von VARCHAR auf INT ändern
|
||
|
|
$stmt = $pdo->prepare(
|
||
|
|
"SELECT DATA_TYPE FROM information_schema.COLUMNS
|
||
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tours' AND COLUMN_NAME = 'user_id'"
|
||
|
|
);
|
||
|
|
$stmt->execute();
|
||
|
|
$type = $stmt->fetchColumn();
|
||
|
|
|
||
|
|
if ($type === 'varchar') {
|
||
|
|
// Sicherstellen, dass keine nicht-numerischen Werte verbleiben
|
||
|
|
$pdo->exec("UPDATE `tours` SET user_id = '0' WHERE user_id NOT REGEXP '^[0-9]+$'");
|
||
|
|
$pdo->exec('ALTER TABLE `tours` MODIFY `user_id` INT NOT NULL');
|
||
|
|
$out("tours.user_id nach INT geändert");
|
||
|
|
} elseif ($type === 'int') {
|
||
|
|
$out('tours.user_id ist bereits INT');
|
||
|
|
} else {
|
||
|
|
$out("tours.user_id hat Typ '$type' (nicht geändert)");
|
||
|
|
}
|
||
|
|
|
||
|
|
echo "Fertig.\n";
|