From edcdfa95c4ec318b6856a5ab7d7c4637228841c3 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Mon, 23 Jan 2023 04:10:54 -0500 Subject: [PATCH] Fixes unable to create new DB when it doesnt exists. --- Source/Framework/Database/DatabaseLoader.cs | 7 +++-- Source/Framework/Database/DatabaseUpdater.cs | 30 ------------------ Source/Framework/Database/MySqlBase.cs | 32 ++++++++++++++++++-- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Source/Framework/Database/DatabaseLoader.cs b/Source/Framework/Database/DatabaseLoader.cs index 6c0bd179f..6ea9b2945 100644 --- a/Source/Framework/Database/DatabaseLoader.cs +++ b/Source/Framework/Database/DatabaseLoader.cs @@ -101,8 +101,8 @@ namespace Framework.Database // Create temporary query to use external MySQL CLi try { - using BinaryWriter binaryWriter = new(File.Open(temp, FileMode.Create, FileAccess.Write)); - binaryWriter.Write($"CREATE DATABASE `{connectionObject.Database}` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); + using StreamWriter streamWriter = new(File.Open(temp, FileMode.Create, FileAccess.Write)); + streamWriter.Write($"CREATE DATABASE `{connectionObject.Database}` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); } catch (Exception) { @@ -131,6 +131,9 @@ namespace Framework.Database if (_updateFlags == 0) Log.outInfo(LogFilter.SqlUpdates, "Automatic database updates are disabled for all databases!"); + if (_updateFlags != 0 && !DBExecutableUtil.CheckExecutable()) + return false; + if (!OpenDatabases()) return false; diff --git a/Source/Framework/Database/DatabaseUpdater.cs b/Source/Framework/Database/DatabaseUpdater.cs index a37e329a9..2eb68256c 100644 --- a/Source/Framework/Database/DatabaseUpdater.cs +++ b/Source/Framework/Database/DatabaseUpdater.cs @@ -24,9 +24,6 @@ namespace Framework.Database if (!result.IsEmpty() && !result.IsEmpty()) return true; - if (!DBUpdaterUtil.CheckExecutable()) - return false; - Log.outInfo(LogFilter.SqlUpdates, $"Database {_database.GetDatabaseName()} is empty, auto populating it..."); string path = GetSourceDirectory(); @@ -71,9 +68,6 @@ namespace Framework.Database public bool Update() { - if (!DBUpdaterUtil.CheckExecutable()) - return false; - Log.outInfo(LogFilter.SqlUpdates, $"Updating {_database.GetDatabaseName()} database..."); string sourceDirectory = GetSourceDirectory(); @@ -465,28 +459,4 @@ namespace Framework.Database Apply, Rehash } - - static class DBUpdaterUtil - { - static string mysqlExecutablePath; - - public static string GetMySQLExecutable() - { - return mysqlExecutablePath; - } - - public static bool CheckExecutable() - { - string mysqlExePath = ConfigMgr.GetDefaultValue("MySQLExecutable", ""); - if (mysqlExePath.IsEmpty() || !File.Exists(mysqlExePath)) - { - Log.outFatal(LogFilter.SqlUpdates, $"Didn't find any executable MySQL binary at \'{mysqlExePath}\' or in path, correct the path in the *.conf (\"MySQLExecutable\")."); - return false; - } - - // Correct the path to the cli - mysqlExecutablePath = mysqlExePath; - return true; - } - } } diff --git a/Source/Framework/Database/MySqlBase.cs b/Source/Framework/Database/MySqlBase.cs index 32a60438e..3c1f4037f 100644 --- a/Source/Framework/Database/MySqlBase.cs +++ b/Source/Framework/Database/MySqlBase.cs @@ -1,11 +1,13 @@ // Copyright (c) CypherCore All rights reserved. // Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information. +using Framework.Configuration; using Framework.Threading; using MySqlConnector; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Text; using System.Threading.Tasks; using System.Transactions; @@ -286,7 +288,7 @@ namespace Framework.Database // Invokes a mysql process which doesn't leak credentials to logs Process process = new(); - process.StartInfo = new(DBUpdaterUtil.GetMySQLExecutable()); + process.StartInfo = new(DBExecutableUtil.GetMySQLExecutable()); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; @@ -294,9 +296,11 @@ namespace Framework.Database process.StartInfo.Arguments = args; process.Start(); + + process.WaitForExit(); + Log.outInfo(LogFilter.SqlUpdates, process.StandardOutput.ReadToEnd()); Log.outError(LogFilter.SqlUpdates, process.StandardError.ReadToEnd()); - process.WaitForExit(); if (process.ExitCode != 0) { @@ -421,4 +425,28 @@ namespace Framework.Database public abstract void PreparedStatements(); } + + public static class DBExecutableUtil + { + static string mysqlExecutablePath; + + public static string GetMySQLExecutable() + { + return mysqlExecutablePath; + } + + public static bool CheckExecutable() + { + string mysqlExePath = ConfigMgr.GetDefaultValue("MySQLExecutable", ""); + if (mysqlExePath.IsEmpty() || !File.Exists(mysqlExePath)) + { + Log.outFatal(LogFilter.SqlUpdates, $"Didn't find any executable MySQL binary at \'{mysqlExePath}\' or in path, correct the path in the *.conf (\"MySQLExecutable\")."); + return false; + } + + // Correct the path to the cli + mysqlExecutablePath = mysqlExePath; + return true; + } + } }