From c4439e05a2addfbdda2843e7c9f91360f6f9033e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 2 Oct 2017 15:11:10 +1100 Subject: [PATCH] fix missing DEFAULT values for mysql DB table dumps --- MCGalaxy/Database/Backends/MySQL.cs | 26 +++++++++++++++ MCGalaxy/Database/Backends/SQLite.cs | 15 ++++++++- MCGalaxy/Database/IDatabaseBackend.cs | 4 +++ MCGalaxy/Server/BackupDB.cs | 47 +-------------------------- MCGalaxy/Server/Server.DB.cs | 4 +-- 5 files changed, 47 insertions(+), 49 deletions(-) diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index 62b253b72..7b657de51 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.IO; using System.Text; using MySql.Data.MySqlClient; @@ -121,6 +122,31 @@ namespace MCGalaxy.SQL { } sql.AppendLine(); } + } + + public override void PrintSchema(string table, TextWriter w) { + string pri = ""; + w.WriteLine("CREATE TABLE IF NOT EXISTS `{0}` (", table); + + using (DataTable schema = Database.Fill("DESCRIBE `" + table + "`")) { + string[] data = new string[schema.Columns.Count]; + foreach (DataRow row in schema.Rows) { + for (int col = 0; col < schema.Columns.Count; col++) { + data[col] = row[col].ToString(); + } + + if (data[3].CaselessEq("pri")) pri = data[0]; + string value = data[2].CaselessEq("no") ? "NOT NULL" : "DEFAULT NULL"; + if (data[4].Length > 0) value += " DEFAULT '" + data[4] + "'"; + if (data[5].Length > 0) value += " " + data[5]; + + string suffix = pri.Length == 0 && row == schema.Rows[schema.Rows.Count - 1] ? "" : ","; + w.WriteLine("`{0}` {1} {2}{3}", row[0], row[1], value, suffix); + } + } + + if (pri.Length > 0) w.Write("PRIMARY KEY (`{0}`)", pri); + w.WriteLine(");"); } public override void AddColumn(string table, ColumnDesc col, string colAfter) { diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index ee52423f6..284f33c9e 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.SQLite; +using System.IO; using System.Text; namespace MCGalaxy.SQL { @@ -129,7 +130,19 @@ namespace MCGalaxy.SQL { } sql.AppendLine(); } - } + } + + public override void PrintSchema(string table, TextWriter w) { + const string syntax = "SELECT sql FROM sqlite_master WHERE tbl_name = @0 AND type = 'table'"; + using (DataTable schema = Database.Fill(syntax + CaselessWhereSuffix, table)) { + foreach (DataRow row in schema.Rows) { + string sql = row[0].ToString(); + sql = sql.Replace(" " + table, " `" + table + "`"); + sql = sql.Replace("CREATE TABLE `" + table + "`", "CREATE TABLE IF NOT EXISTS `" + table + "`"); + w.WriteLine(sql + ";"); + } + } + } public override void AddColumn(string table, ColumnDesc col, string colAfter) { ValidateTable(table); diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index 6db0ed398..622996672 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.IO; using System.Text; namespace MCGalaxy.SQL { @@ -91,6 +92,9 @@ namespace MCGalaxy.SQL { Database.Execute(syntax); } + /// Prints/dumps the table schema of the given table. + public abstract void PrintSchema(string table, TextWriter w); + // == Higher level column functions == diff --git a/MCGalaxy/Server/BackupDB.cs b/MCGalaxy/Server/BackupDB.cs index 6f71713e3..50ccdefea 100644 --- a/MCGalaxy/Server/BackupDB.cs +++ b/MCGalaxy/Server/BackupDB.cs @@ -51,58 +51,13 @@ namespace MCGalaxy { sql.WriteLine("-- --------------------------------------------------------"); sql.WriteLine("-- Table structure for table `{0}`", tableName); sql.WriteLine(); - WriteTableSchema(tableName, sql); + Database.Backend.PrintSchema(tableName, sql); TableDumper dumper = new TableDumper(); dumper.DumpTable(sql, tableName); dumper.sql = null; } - static void WriteTableSchema(string tableName, StreamWriter sql) { - if (ServerConfig.UseMySQL) { - string pri = ""; - sql.WriteLine("CREATE TABLE IF NOT EXISTS `{0}` (", tableName); - - using (DataTable schema = Database.Fill("DESCRIBE `" + tableName + "`")) { - string[] rowParams = new string[schema.Columns.Count]; - foreach (DataRow row in schema.Rows) { - //Save the info contained to file - List tmp = new List(); - for (int col = 0; col < schema.Columns.Count; col++) - tmp.Add(row[col].ToString()); - - rowParams = tmp.ToArray(); - rowParams[2] = (rowParams[2].CaselessEq("no") ? "NOT " : "DEFAULT ") + "NULL"; - pri += (rowParams[3].CaselessEq("pri") ? rowParams[0] + ";" : ""); - sql.WriteLine("`{0}` {1} {2}" + (rowParams[5].Equals("") ? "" : " {5}") + (pri.Equals("") && row == schema.Rows[schema.Rows.Count - 1] ? "" : ","), rowParams); - } - } - - if (pri.Length > 0) { - string[] tmp = pri.Substring(0, pri.Length - 1).Split(';'); - sql.Write("PRIMARY KEY (`"); - foreach (string prim in tmp) { - sql.Write(prim); - sql.Write("`" + (tmp[tmp.Length - 1].Equals(prim) ? ")" : ", `")); - } - } - sql.WriteLine(");"); - } else { - const string syntax = "SELECT sql FROM sqlite_master WHERE tbl_name = @0 AND type = 'table'"; - using (DataTable tableSQL = Database.Fill(syntax + Database.Backend.CaselessWhereSuffix, tableName)) - { - //just print out the data in the table. - foreach (DataRow row in tableSQL.Rows) { - string sqlSyntax = row[0].ToString(); - sqlSyntax = sqlSyntax.Replace(" " + tableName, " `" + tableName + "`"); - sqlSyntax = sqlSyntax.Replace("CREATE TABLE `" + tableName + "`", "CREATE TABLE IF NOT EXISTS `" + tableName + "`"); - sql.WriteLine(sqlSyntax + ";"); - } - } - } - sql.WriteLine(); - } - internal static void ReplaceDatabase(Stream sql) { using (FileStream backup = File.Create("backup.sql")) BackupDatabase(new StreamWriter(backup), false); // backup diff --git a/MCGalaxy/Server/Server.DB.cs b/MCGalaxy/Server/Server.DB.cs index 14e7d14b3..a7a71d253 100644 --- a/MCGalaxy/Server/Server.DB.cs +++ b/MCGalaxy/Server/Server.DB.cs @@ -38,8 +38,8 @@ namespace MCGalaxy { new ColumnDesc("totalCuboided", ColumnType.Int64), new ColumnDesc("totalKicked", ColumnType.Int24), new ColumnDesc("TimeSpent", ColumnType.VarChar, 20), - new ColumnDesc("color", ColumnType.VarChar, 6), - new ColumnDesc("title_color", ColumnType.VarChar, 6), + new ColumnDesc("color", ColumnType.VarChar, 2), + new ColumnDesc("title_color", ColumnType.VarChar, 2), }; static ColumnDesc[] createOpstats = new ColumnDesc[] {