/// /// Copyright (c) 2016-2023 Sensus Slovensko a.s. /// using System; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections.Generic; using log4net; using MySql.Data.MySqlClient; using NHibernate; using Common; using Results; using Results.Entities; using TBF.Resources; namespace TBF.UI.Settings { public partial class UpgradeSelectionDlg : Form { private static readonly ILog log = LogManager.GetLogger(typeof(UpgradeSelectionDlg)); int batchFrom; int batchTo; Shared.ModelessActivityForm form; System.Threading.Thread formThread; public UpgradeSelectionDlg() { InitializeComponent(); upgradeConfigDBCheckBox.Checked = true; upgradeResultsDBCheckBox.Checked = true; } void ActivityStart() { form = new Shared.ModelessActivityForm() { Message = "Upgrading database", FontFamily = "Arial", FontSize = 24, FontStyle = FontStyle.Regular, BackgroundColor = Color.Green, }; formThread = new System.Threading.Thread(() => form.ShowDialog()); formThread.Start(); } void ActivityEnd() { if (form != null && formThread != null) { form.CloseForm(null, new EventArgs()); formThread.Join(); } form = null; formThread = null; } /// /// Common part of database upgrade code /// /// Database connection /// Array of SQL commands to be executed /// true = successful bool ExecuteCommands(MySqlConnection dbConn, string[] commands) { if (commands.Length == 0) return true; bool success = false; try { dbConn.Open(); foreach (var cmdText in commands) { MySqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = cmdText; cmd.ExecuteNonQuery(); cmd.Dispose(); } success = true; } catch (Exception exc) { log.ErrorFormat(string.Format("Exception when upgrading DB: {0}", exc.Message)); if (exc.InnerException != null) log.ErrorFormat(string.Format("Inner exception: {0}", exc.InnerException.Message)); ActivityEnd(); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } return success; } private void upgradePipesButton_Click(object sender, EventArgs e) { ActivityStart(); try { var session = TBF.DB.CreateSession(DBKind.Config); var components = session.QueryOver() .List(); var allPipes = session.QueryOver() .Where(x => x.ClassName == "Various.Drawing.Pipes") .List(); foreach (var pipes in allPipes) { pipes.Parameters = UpgradePipes(pipes.Parameters, components); session.SaveOrUpdate(pipes); } session.Flush(); ActivityEnd(); MessageBox.Show("Upgrade completed", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } string UpgradePipes(string parameters, IList components) { StringBuilder sb = new StringBuilder(); int startIx = 0; int pos = parameters.IndexOf("", startIx); while (pos >= 0) { sb.Append(parameters.Substring(startIx, pos - startIx + "".Length)); startIx = pos + "".Length; pos = parameters.IndexOf("", startIx); if (pos >= 0) { sb.Append(UpgradeOnePipe(parameters.Substring(startIx, pos - startIx), components)); sb.Append(""); startIx = pos + "".Length; pos = parameters.IndexOf("", startIx); } } sb.Append(parameters.Substring(startIx)); return sb.ToString(); } string UpgradeOnePipe(string edge, IList components) { string[] items = edge.Split(new char[] { '~' }); int startId; int endId; if (items.Length != 4 || (startId = GetId(items[0], components)) == 0 || (endId = GetId(items[2], components)) == 0) { return edge; } return string.Format("{0}~{1}~{2}~{3}", startId, items[1], endId, items[3]); } int GetId(string name, IList components) { foreach (var c in components) if (c.Name == name) return c.Id; return 0; } private void upgradeDb_301_302_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `test` ADD `TempControl` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `TempLimHi`;", "ALTER TABLE `test` DROP `DoControlWaterTemp`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_301_302_Button.Enabled = false; } } private void upgradeDb_302_303_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `test` CHANGE `Qtg` `Qtg` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Qfrom` `Qfrom` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Qto` `Qto` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Volume` `Volume` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `TestTime` `TestTime` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `measurementcorrection` CHANGE `Measurement` `Measurement` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `measurementcorrection` CHANGE `Correction` `Correction` DOUBLE NOT NULL DEFAULT '0';", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { if (upgradeConfigDBCheckBox.Checked) { ISession session = null; try { session = TBF.DB.CreateSession(DBKind.Config); var tests = session.QueryOver().List(); foreach (var t in tests) { string singlePrecStr = Convert.ToSingle(t.Qtg).ToString(); if (t.Qtg.ToString().Length > 2 * singlePrecStr.Length) t.Qtg = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Qfrom).ToString(); if (t.Qfrom.ToString().Length > 2 * singlePrecStr.Length) t.Qfrom = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Qto).ToString(); if (t.Qto.ToString().Length > 2 * singlePrecStr.Length) t.Qto = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Volume).ToString(); if (t.Volume.ToString().Length > 2 * singlePrecStr.Length) t.Volume = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.TestTime).ToString(); if (t.TestTime.ToString().Length > 2 * singlePrecStr.Length) t.TestTime = double.Parse(singlePrecStr); session.SaveOrUpdate(t); } var mcorrections = session.QueryOver().List(); foreach (var mc in mcorrections) { string singlePrecStr = Convert.ToSingle(mc.Measurement).ToString(); if (mc.Measurement.ToString().Length > 2 * singlePrecStr.Length) mc.Measurement = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(mc.Correction).ToString(); if (mc.Correction.ToString().Length > 2 * singlePrecStr.Length) mc.Correction = double.Parse(singlePrecStr); session.SaveOrUpdate(mc); } session.Flush(); } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Test flow rounding after a conversion to double failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (session != null && session.IsOpen) session.Close(); } } ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_302_303_Button.Enabled = false; } } private void upgradeDb_303_304_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `outputpath` ADD `RegVPositions` VARCHAR(255) NULL DEFAULT NULL AFTER `Scale`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_303_304_Button.Enabled = false; } } private void upgradeDb_304_305_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `ptest` ADD `Method` VARCHAR(255) NULL DEFAULT NULL AFTER `Evaluate`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_304_305_Button.Enabled = false; } } private void upgradeDb_305_306_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { }; string[] resultsDbCommands = new string[] { "ALTER TABLE `batch` CHANGE `StartTime` `StartTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00';", "ALTER TABLE `batch` CHANGE `EndTime` `EndTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00';", "ALTER TABLE `batch` ADD `SaveTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00' AFTER `EndTime`;", #if !(MUNICH && LANG_DE) "ALTER TABLE `watermeterdata` ADD `Qnames` TINYINT(4) NOT NULL DEFAULT '1' AFTER `Mounting`;", #endif "ALTER TABLE `watermeterdata` ADD `ErrLimHiQ` DOUBLE NOT NULL DEFAULT '2' AFTER `Q1_Qmin`," + " ADD `ErrLimLoQ` DOUBLE NOT NULL DEFAULT '5' AFTER `ErrLimHiQ`;", }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_305_306_Button.Enabled = false; } } private void upgradeDb_306_307_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { }; string[] resultsDbCommands = new string[] { "ALTER TABLE `watermeterdata` ADD `PulsesPerLtrAux` DOUBLE NOT NULL DEFAULT '1' AFTER `ApprovalInfoAux`;", }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_306_307_Button.Enabled = false; } } } }