Improve Database-Rename

This commit is contained in:
Alexander Nell 2020-05-16 22:16:07 +02:00
parent fda5790af5
commit 2257e43404

View File

@ -91,13 +91,23 @@ class GameModeSettings implements ConfiguratorMenu, CallbackListener, Communicat
$renameQuery = "ALTER TABLE `" . self::TABLE_SCRIPT_SETTINGS . "` RENAME TO `" . self::TABLE_GAMEMODE_SETTINGS . "`;"; $renameQuery = "ALTER TABLE `" . self::TABLE_SCRIPT_SETTINGS . "` RENAME TO `" . self::TABLE_GAMEMODE_SETTINGS . "`;";
$result = $mysqli->query($renameQuery); $result = $mysqli->query($renameQuery);
if (!$result) { if (!$result) {
if ($mysqli->error === "Table '" . self::TABLE_GAMEMODE_SETTINGS . "' already exists") { if (strpos($mysqli->error, "doesn't exist") !== false) {
return true; // old doesn't exist, good, continue to force creation
} elseif (strpos($mysqli->error, "already exists") !== false) {
// new one exists, drop the old table, get out
$dropQuery = "DROP TABLE `" . self::TABLE_SCRIPT_SETTINGS . "`;";
$result = $mysqli->query($dropQuery);
if (!$result) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
} else { } else {
trigger_error($mysqli->error); // other error (successful rename would not have us got here)
// can't rename, so continue trigger_error($mysqli->error, E_USER_ERROR);
return false;
} }
} }
// else rename happened, continue to force creation
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_GAMEMODE_SETTINGS . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_GAMEMODE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,