fix missing DEFAULT values for mysql DB table dumps

This commit is contained in:
UnknownShadow200 2017-10-02 15:11:10 +11:00
parent 1648ea7d11
commit c4439e05a2
5 changed files with 47 additions and 49 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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);
}
/// <summary> Prints/dumps the table schema of the given table. </summary>
public abstract void PrintSchema(string table, TextWriter w);
// == Higher level column functions ==

View File

@ -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<string> tmp = new List<string>();
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

View File

@ -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[] {