From 59c2afe0e89403202cc51826ac9a8631756dd1cf Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 31 Oct 2017 09:45:29 +0100 Subject: [PATCH] UpgradeSelectionDlg support for 2.12-->2.17, ver. 2.17.722 --- .../Forms/NoBenchOrDatabaseDlg.Designer.cs | 169 ++++++------ .../Forms/NoBenchOrDatabaseDlg.cs | 18 +- .../Forms/NoBenchOrDatabaseDlg.resx | 4 +- .../Forms/UpgradeSelectionDlg.Designer.cs | 39 ++- .../Forms/UpgradeSelectionDlg.cs | 245 +++++++++++++++--- TestBenchFramework/Program.cs | 34 ++- TestBenchFramework/Properties/AssemblyInfo.cs | 4 +- 7 files changed, 356 insertions(+), 157 deletions(-) diff --git a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.Designer.cs b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.Designer.cs index 9e1c73a48..f60ec60a5 100644 --- a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.Designer.cs +++ b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.Designer.cs @@ -31,84 +31,96 @@ namespace TBF.Forms /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NoBenchOrDatabaseDlg)); - this.exitButton = new System.Windows.Forms.Button(); - this.retryButton = new System.Windows.Forms.Button(); - this.configureButton = new System.Windows.Forms.Button(); - this.pictureBox1 = new System.Windows.Forms.PictureBox(); - this.errorMessageLabel = new System.Windows.Forms.Label(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); - this.SuspendLayout(); - // - // exitButton - // - this.exitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.exitButton.Location = new System.Drawing.Point(42, 68); - this.exitButton.Name = "exitButton"; - this.exitButton.Size = new System.Drawing.Size(246, 33); - this.exitButton.TabIndex = 0; - this.exitButton.Text = "Exit"; - this.exitButton.UseVisualStyleBackColor = true; - this.exitButton.Click += new System.EventHandler(this.exitButton_Click); - // - // retryButton - // - this.retryButton.Location = new System.Drawing.Point(42, 146); - this.retryButton.Name = "retryButton"; - this.retryButton.Size = new System.Drawing.Size(246, 33); - this.retryButton.TabIndex = 1; - this.retryButton.Text = "Retry database connection"; - this.retryButton.UseVisualStyleBackColor = true; - this.retryButton.Click += new System.EventHandler(this.retryButton_Click); - // - // configureButton - // - this.configureButton.Location = new System.Drawing.Point(42, 107); - this.configureButton.Name = "configureButton"; - this.configureButton.Size = new System.Drawing.Size(246, 33); - this.configureButton.TabIndex = 2; - this.configureButton.Text = "Configure test benches (password required)"; - this.configureButton.UseVisualStyleBackColor = true; - this.configureButton.Click += new System.EventHandler(this.configureButton_Click); - // - // pictureBox1 - // - this.pictureBox1.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.ErrorImage"))); - this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); - this.pictureBox1.InitialImage = null; - this.pictureBox1.Location = new System.Drawing.Point(42, 21); - this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(32, 32); - this.pictureBox1.TabIndex = 3; - this.pictureBox1.TabStop = false; - // - // errorMessageLabel - // - this.errorMessageLabel.AutoSize = true; - this.errorMessageLabel.Location = new System.Drawing.Point(80, 31); - this.errorMessageLabel.Name = "errorMessageLabel"; - this.errorMessageLabel.Size = new System.Drawing.Size(160, 13); - this.errorMessageLabel.TabIndex = 4; - this.errorMessageLabel.Text = "Cannot connect to the database"; - // - // NoBenchOrDatabaseDlg - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.CancelButton = this.exitButton; - this.ClientSize = new System.Drawing.Size(328, 194); - this.Controls.Add(this.errorMessageLabel); - this.Controls.Add(this.pictureBox1); - this.Controls.Add(this.configureButton); - this.Controls.Add(this.retryButton); - this.Controls.Add(this.exitButton); - this.Name = "NoBenchOrDatabaseDlg"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "Error"; - this.Load += new System.EventHandler(this.NoDatabaseMsgDlg_Load); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); - this.ResumeLayout(false); - this.PerformLayout(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NoBenchOrDatabaseDlg)); + this.exitButton = new System.Windows.Forms.Button(); + this.retryButton = new System.Windows.Forms.Button(); + this.configureButton = new System.Windows.Forms.Button(); + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.errorMessageLabel = new System.Windows.Forms.Label(); + this.upgradeButton = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.SuspendLayout(); + // + // exitButton + // + this.exitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.exitButton.Location = new System.Drawing.Point(42, 68); + this.exitButton.Name = "exitButton"; + this.exitButton.Size = new System.Drawing.Size(388, 33); + this.exitButton.TabIndex = 0; + this.exitButton.Text = "Exit"; + this.exitButton.UseVisualStyleBackColor = true; + this.exitButton.Click += new System.EventHandler(this.exitButton_Click); + // + // retryButton + // + this.retryButton.Location = new System.Drawing.Point(42, 146); + this.retryButton.Name = "retryButton"; + this.retryButton.Size = new System.Drawing.Size(388, 33); + this.retryButton.TabIndex = 1; + this.retryButton.Text = "Retry database connection"; + this.retryButton.UseVisualStyleBackColor = true; + this.retryButton.Click += new System.EventHandler(this.retryButton_Click); + // + // configureButton + // + this.configureButton.Location = new System.Drawing.Point(42, 107); + this.configureButton.Name = "configureButton"; + this.configureButton.Size = new System.Drawing.Size(388, 33); + this.configureButton.TabIndex = 2; + this.configureButton.Text = "Configure test benches (password required)"; + this.configureButton.UseVisualStyleBackColor = true; + this.configureButton.Click += new System.EventHandler(this.configureButton_Click); + // + // pictureBox1 + // + this.pictureBox1.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.ErrorImage"))); + this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); + this.pictureBox1.InitialImage = null; + this.pictureBox1.Location = new System.Drawing.Point(42, 21); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(32, 32); + this.pictureBox1.TabIndex = 3; + this.pictureBox1.TabStop = false; + // + // errorMessageLabel + // + this.errorMessageLabel.AutoSize = true; + this.errorMessageLabel.Location = new System.Drawing.Point(80, 31); + this.errorMessageLabel.Name = "errorMessageLabel"; + this.errorMessageLabel.Size = new System.Drawing.Size(160, 13); + this.errorMessageLabel.TabIndex = 4; + this.errorMessageLabel.Text = "Cannot connect to the database"; + // + // upgradeButton + // + this.upgradeButton.Location = new System.Drawing.Point(42, 185); + this.upgradeButton.Name = "upgradeButton"; + this.upgradeButton.Size = new System.Drawing.Size(388, 33); + this.upgradeButton.TabIndex = 5; + this.upgradeButton.Text = "Upgrade the database"; + this.upgradeButton.UseVisualStyleBackColor = true; + this.upgradeButton.Click += new System.EventHandler(this.upgradeButton_Click); + // + // NoBenchOrDatabaseDlg + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.exitButton; + this.ClientSize = new System.Drawing.Size(466, 240); + this.Controls.Add(this.upgradeButton); + this.Controls.Add(this.errorMessageLabel); + this.Controls.Add(this.pictureBox1); + this.Controls.Add(this.configureButton); + this.Controls.Add(this.retryButton); + this.Controls.Add(this.exitButton); + this.Name = "NoBenchOrDatabaseDlg"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Error"; + this.Load += new System.EventHandler(this.NoDatabaseMsgDlg_Load); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); } @@ -119,5 +131,6 @@ namespace TBF.Forms private System.Windows.Forms.Button configureButton; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Label errorMessageLabel; + private System.Windows.Forms.Button upgradeButton; } } \ No newline at end of file diff --git a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.cs b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.cs index b72fedfd9..b33a7d8fb 100644 --- a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.cs +++ b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.cs @@ -33,19 +33,9 @@ namespace TBF.Forms if (!ShowRetry) retryButton.Enabled = false; } - private void exitButton_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Abort; - } - - private void retryButton_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Retry; - } - - private void configureButton_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.OK; - } + private void exitButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Abort; } /// Exit + private void configureButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.OK; } /// Configure test benches + private void retryButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Retry; } /// Retry connection to the database + private void upgradeButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Yes; } /// Upgrade the database } } diff --git a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.resx b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.resx index e42c6fc53..b753a8577 100644 --- a/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.resx +++ b/TestBenchFramework/Forms/NoBenchOrDatabaseDlg.resx @@ -229,8 +229,8 @@ iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 - YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlwSFlzAAAOvwAA - Dr8BOAVTJAAACG1JREFUSEu1lmlQW9cVx/mG0GIZZCczGcZjvCQEiLHNYpABAQKEwBjTeqmXCfkQ3Mx4 + YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlwSFlzAAAOvgAA + Dr4B6kKxwAAACG1JREFUSEu1lmlQW9cVx/mG0GIZZCczGcZjvCQEiLHNYpABAQKEwBjTeqmXCfkQ3Mx4 xp58SlMnY/dLmxl3TJIZu3UhBAQBsYNsisBoByS0C21oMUhCYIQkbFzqxo4n9C89TElqt/3SM1dv3tM7 93fOPfecc1/M+n+UQCCgnJzs5PPrb9787OrVK5cvY+AGj/gTr6Cwofoaea0Bh8PR1dl5/do1Ho8nlUqd TudKOPxDVHCDR/yJV1CAGpQ3pv2bvMJAMBgcGBior6+XSCQhPDhm/CKRu4Nvv3Xb8vsv9J9fm77xRwev diff --git a/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs b/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs index bdc5127b6..5e0b3c79c 100644 --- a/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs +++ b/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs @@ -28,37 +28,49 @@ /// private void InitializeComponent() { - this.upgradeDbButton = new System.Windows.Forms.Button(); + this.upgradeDb_215_217_Button = new System.Windows.Forms.Button(); this.upgradeDb_216_217_Button = new System.Windows.Forms.Button(); + this.upgradeDb_212_217_Button = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // upgradeDbButton + // upgradeDb_215_217_Button // - this.upgradeDbButton.Location = new System.Drawing.Point(12, 12); - this.upgradeDbButton.Name = "upgradeDbButton"; - this.upgradeDbButton.Size = new System.Drawing.Size(260, 34); - this.upgradeDbButton.TabIndex = 0; - this.upgradeDbButton.Text = "Upgrade database from ver. 2.15 to ver. 2.17"; - this.upgradeDbButton.UseVisualStyleBackColor = true; - this.upgradeDbButton.Click += new System.EventHandler(this.upgradeDbButton_Click); + this.upgradeDb_215_217_Button.Location = new System.Drawing.Point(12, 52); + this.upgradeDb_215_217_Button.Name = "upgradeDb_215_217_Button"; + this.upgradeDb_215_217_Button.Size = new System.Drawing.Size(260, 34); + this.upgradeDb_215_217_Button.TabIndex = 1; + this.upgradeDb_215_217_Button.Text = "Upgrade database from ver. 2.15 to ver. 2.17"; + this.upgradeDb_215_217_Button.UseVisualStyleBackColor = true; + this.upgradeDb_215_217_Button.Click += new System.EventHandler(this.upgradeDb_215_217_Button_Click); // // upgradeDb_216_217_Button // - this.upgradeDb_216_217_Button.Location = new System.Drawing.Point(12, 52); + this.upgradeDb_216_217_Button.Location = new System.Drawing.Point(12, 92); this.upgradeDb_216_217_Button.Name = "upgradeDb_216_217_Button"; this.upgradeDb_216_217_Button.Size = new System.Drawing.Size(260, 34); - this.upgradeDb_216_217_Button.TabIndex = 1; + this.upgradeDb_216_217_Button.TabIndex = 2; this.upgradeDb_216_217_Button.Text = "Upgrade database from ver. 2.16 to ver. 2.17"; this.upgradeDb_216_217_Button.UseVisualStyleBackColor = true; this.upgradeDb_216_217_Button.Click += new System.EventHandler(this.upgradeDb_216_217_Button_Click); // + // upgradeDb_212_217_Button + // + this.upgradeDb_212_217_Button.Location = new System.Drawing.Point(12, 12); + this.upgradeDb_212_217_Button.Name = "upgradeDb_212_217_Button"; + this.upgradeDb_212_217_Button.Size = new System.Drawing.Size(260, 34); + this.upgradeDb_212_217_Button.TabIndex = 0; + this.upgradeDb_212_217_Button.Text = "Upgrade database from ver. 2.12 to ver. 2.17"; + this.upgradeDb_212_217_Button.UseVisualStyleBackColor = true; + this.upgradeDb_212_217_Button.Click += new System.EventHandler(this.upgradeDb_212_217_Button_Click); + // // UpgradeSelectionDlg // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); + this.Controls.Add(this.upgradeDb_212_217_Button); this.Controls.Add(this.upgradeDb_216_217_Button); - this.Controls.Add(this.upgradeDbButton); + this.Controls.Add(this.upgradeDb_215_217_Button); this.Name = "UpgradeSelectionDlg"; this.Text = "Upgrade selection"; this.ResumeLayout(false); @@ -67,8 +79,9 @@ #endregion - private System.Windows.Forms.Button upgradeDbButton; + private System.Windows.Forms.Button upgradeDb_215_217_Button; private System.Windows.Forms.Button upgradeDb_216_217_Button; + private System.Windows.Forms.Button upgradeDb_212_217_Button; } } \ No newline at end of file diff --git a/TestBenchFramework/Forms/UpgradeSelectionDlg.cs b/TestBenchFramework/Forms/UpgradeSelectionDlg.cs index ba6a64ff8..bad0a6870 100644 --- a/TestBenchFramework/Forms/UpgradeSelectionDlg.cs +++ b/TestBenchFramework/Forms/UpgradeSelectionDlg.cs @@ -1,54 +1,30 @@ using System; using System.Windows.Forms; +using System.Collections.Generic; +using log4net; using MySql.Data.MySqlClient; -using Results; namespace TBF.Forms { public partial class UpgradeSelectionDlg : Form { - int batchFrom; - int batchTo; + private static readonly ILog log = LogManager.GetLogger(typeof(UpgradeSelectionDlg)); public UpgradeSelectionDlg() { InitializeComponent(); - batchFrom = 1; - batchTo = Program.LocalSettings.BatchNr - 1; } - private void upgradeDbButton_Click(object sender, EventArgs e) - { - string[] commands = new string[] - { - "ALTER TABLE `testrslt` ADD `ErrorFlagsMd` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `ErrorMaster`, ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `ErrorFlagsMd`, ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", - "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", - }; - - if (Common(new MySqlConnection(Results.DB.ConnectionString), commands)) - { - upgradeDbButton.Enabled = false; - } - } - - private void upgradeDb_216_217_Button_Click(object sender, EventArgs e) - { - string[] commands = new string[] - { - "ALTER TABLE `testrslt` ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", - "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", - }; - - if (Common(new MySqlConnection(Results.DB.ConnectionString), commands)) - { - upgradeDb_216_217_Button.Enabled = false; - } - } - - bool Common(MySqlConnection dbConn, string[] commands) + /// + /// Common part of database upgrade code + /// + /// Database connection + /// Array of SQL commands to be executed + /// true = successful + bool UpgradeCommon(MySqlConnection dbConn, string[] commands) { bool success = false; - + try { dbConn.Open(); @@ -58,13 +34,15 @@ namespace TBF.Forms MySqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = cmdText; cmd.ExecuteNonQuery(); + cmd.Dispose(); } - MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); 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)); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally @@ -74,5 +52,200 @@ namespace TBF.Forms return success; } + + + ///--------------------------------------------------------------------- + private void upgradeDb_212_217_Button_Click(object sender, EventArgs e) + { + string[] configDbCommands = new string[] + { + /// 2.12 -> 2.13 + "ALTER TABLE `procedure` ADD `Protected` BOOLEAN NOT NULL DEFAULT '0' AFTER `ProtocolTitle`, ADD `MustBeComplete` BOOLEAN NOT NULL DEFAULT '0' AFTER `Protected`, ADD `ManualCtrlDisabled` BOOLEAN NOT NULL DEFAULT '0' AFTER `MustBeComplete`", + /// 2.13 -> 2.14 + "ALTER TABLE `test` ADD `DoControlWaterTemp` BOOLEAN NOT NULL DEFAULT '0' AFTER `Zeroing`, ADD `TempLimLo` FLOAT NOT NULL DEFAULT '15' AFTER `DoControlWaterTemp`, ADD `TempLimHi` FLOAT NOT NULL DEFAULT '25' AFTER `TempLimLo`", + /// 2.14 -> 2.15 + "ALTER TABLE `procedure` ADD `AltProcName` VARCHAR(255) NULL DEFAULT NULL , ADD `AltProcPeriod` INT NOT NULL DEFAULT '0';", + //"ALTER TABLE `procedure` DROP `ProtocolTitle`;", /// Commented out to maintain compatibility with an old ResultsBrowser + }; + + string[] resultsDbCommands = new string[] + { + /// 2.12 -> 2.13 + "ALTER TABLE `watermeter` ADD `Remark` VARCHAR( 2000 ) NULL DEFAULT NULL AFTER `ResultCode`", + "ALTER TABLE `batch` ADD `Dirty` BOOLEAN NOT NULL DEFAULT '0' AFTER `EndTime`", + /// 2.13 -> 2.14 +#if !IPERL + "ALTER TABLE `testrslt` ADD `FlowMean` FLOAT NOT NULL DEFAULT '0' AFTER `TempDivMax`", +#endif + "ALTER TABLE `testrslt` ADD `TestDone` BOOLEAN NOT NULL DEFAULT '1' AFTER `RepetitionNr`, ADD `Remark` VARCHAR( 2000 ) NULL DEFAULT NULL AFTER `TestDone`", + "ALTER TABLE `testdata` ADD `DoControlWaterTemp` BOOLEAN NOT NULL DEFAULT '0' AFTER `Uncertainty`, ADD `TempLimLo` FLOAT NOT NULL DEFAULT '15' AFTER `DoControlWaterTemp`, ADD `TempLimHi` FLOAT NOT NULL DEFAULT '25' AFTER `TempLimLo`", + /// 2.14 -> 2.15 + "ALTER TABLE `watermeter` ADD `ArchivePath` VARCHAR(255) NULL DEFAULT NULL AFTER `ResultCode`;", + /// 2.15 -> 2.17 + "ALTER TABLE `testrslt` ADD `ErrorFlagsMd` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `ErrorMaster`, ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `ErrorFlagsMd`, ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", + "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", + }; + + if (UpgradeCommon(new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands) && + UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands)) + { + MySqlConnection dbConn = new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString); + + try + { + dbConn.Open(); + + int maxItemNr = 0; + MySqlCommand cmd = dbConn.CreateCommand(); + cmd.CommandText = "SELECT ItemNr FROM component"; + MySqlDataReader dr = cmd.ExecuteReader(); + while (dr.Read()) + { + int itemNr = dr.GetInt32(0); + if (itemNr > maxItemNr) maxItemNr = itemNr; + } + dr.Close(); + + cmd = dbConn.CreateCommand(); + cmd.CommandText = "UPDATE component SET ClassName = 'RegisterReader for standing start/stop' WHERE ClassName = 'Fixed-Start-Register-Reader'"; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + cmd = dbConn.CreateCommand(); + cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.iPerlCommunication' WHERE ClassName = 'iPerlCommunication'"; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + cmd = dbConn.CreateCommand(); + cmd.CommandText = "UPDATE component SET ClassName = 'WaterMeter' WHERE ClassName = 'iPerl'"; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + for (int i = 1; i <= 40; i++) + { + Text = string.Format("Upgrade selection: {0}/40", i); + + /// Read original iPerl water meter parameters + int id = 0; + string parameters = string.Empty; + + cmd = dbConn.CreateCommand(); + cmd.CommandText = string.Format("SELECT Id, Parameters FROM Component WHERE Name = 'iPerl{0}' AND ClassName = 'WaterMeter'", i); + dr = cmd.ExecuteReader(); + if (dr.Read()) + { + id = dr.GetInt32(0); + parameters = dr.GetString(1); + } + dr.Close(); + cmd.Dispose(); + + if (id != 0) + { + /// Rename iPerl water meter + cmd = dbConn.CreateCommand(); + cmd.CommandText = string.Format("UPDATE component SET Name = 'WM{0}' WHERE Name = 'iPerl{0}'", i); + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + /// Create iPerl Head + cmd = dbConn.CreateCommand(); + cmd.CommandText = string.Format("INSERT INTO component(ItemNr, Name, ClassName, Parentname, Mode, Logging, Parameters) VALUES ({0}, 'iPerl{1}', 'RegisterReader for iPerl', '', 'Normal', 'Off', @params)", + maxItemNr + 1, i); + cmd.Parameters.AddWithValue("@params", parameters.Replace("WaterMeterCfg", "IperlHeadCfg")); + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + /// Clone procedure parameters + IList idList = new List(); + IList parList = new List(); + IList procIdList = new List(); + cmd = dbConn.CreateCommand(); + cmd.CommandText = string.Format("SELECT Id, Parameters, Procedure_id FROM componentprocedure WHERE CmpntName = 'iPerl{0}'", i); + dr = cmd.ExecuteReader(); + while (dr.Read()) + { + idList.Add(dr.GetInt32(0)); + parList.Add(dr.GetString(1)); + procIdList.Add(dr.GetInt32(2)); + } + dr.Close(); + cmd.Dispose(); + + if ((idList.Count == parList.Count) && (parList.Count == procIdList.Count)) + { + for (int j = 0; j < parList.Count; j++) + { + cmd = dbConn.CreateCommand(); + cmd.CommandText = string.Format("INSERT INTO componentprocedure(CmpntName, Parameters, Procedure_id) VALUES ('WM{0}', @params, {1})", + i, procIdList[j]); + cmd.Parameters.AddWithValue("@params", parList[j].Replace("Q4", "Qmax") + .Replace("Q3", "Qn") + .Replace("Q2", "Qt") + .Replace("Q1", "Qmin") + .Replace("", "true\r\n ")); + cmd.ExecuteNonQuery(); + cmd.Dispose(); + } + } + } + } + + /// Delete 'Parmeters' of iPerl water meters + cmd = dbConn.CreateCommand(); + cmd.CommandText = "UPDATE component SET Parameters = '\r\n\r\n' WHERE ClassName = 'WaterMeter'"; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + Text = string.Format("Upgrade selection"); + MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); + upgradeDb_212_217_Button.Enabled = false; + } + 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)); + MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + finally + { + dbConn.Close(); + } + } + } + + + ///--------------------------------------------------------------------- + private void upgradeDb_215_217_Button_Click(object sender, EventArgs e) + { + string[] commands = new string[] + { + "ALTER TABLE `testrslt` ADD `ErrorFlagsMd` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `ErrorMaster`, ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `ErrorFlagsMd`, ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", + "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", + }; + + if (UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), commands)) + { + MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); + upgradeDb_215_217_Button.Enabled = false; + } + } + + + ///--------------------------------------------------------------------- + private void upgradeDb_216_217_Button_Click(object sender, EventArgs e) + { + string[] commands = new string[] + { + "ALTER TABLE `testrslt` ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", + "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", + }; + + if (UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), commands)) + { + MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); + upgradeDb_216_217_Button.Enabled = false; + } + } } } diff --git a/TestBenchFramework/Program.cs b/TestBenchFramework/Program.cs index fac8ca871..5e3e0df87 100644 --- a/TestBenchFramework/Program.cs +++ b/TestBenchFramework/Program.cs @@ -305,14 +305,14 @@ namespace TBF Results.DB.DbType = Config.Data.CurrentBench.WaterMetersDBSettings.DbType; Results.DB.ConnectionString = Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString; Results.DB.LoadSharedData(); - int maxBatchNr = Results.DB.GetMaxSavedBatchNr(); + int maxBatchNr = Results.DB.GetMaxSavedBatchNr(); if (!Config.Data.CurrentBench.IsRealBench || Program.LocalSettings.BatchNr <= maxBatchNr) - { - log.FatalFormat("Max. BatchNr in DB = {0}, LocalSettings.BatchNr = {1}", maxBatchNr, Program.LocalSettings.BatchNr); - Program.LocalSettings.BatchNr = maxBatchNr + 1; - Program.LocalSettings.Save(); - log.FatalFormat("LocalSettings.BatchNr updated to {0}", Program.LocalSettings.BatchNr); - } + { + log.FatalFormat("Max. BatchNr in DB = {0}, LocalSettings.BatchNr = {1}", maxBatchNr, Program.LocalSettings.BatchNr); + Program.LocalSettings.BatchNr = maxBatchNr + 1; + Program.LocalSettings.Save(); + log.FatalFormat("LocalSettings.BatchNr updated to {0}", Program.LocalSettings.BatchNr); + } /// This is to trigger an exception in case of a power user and no Config database IList listOfProcedures = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config) @@ -343,18 +343,28 @@ namespace TBF /// Ask what to do switch ((new Forms.NoBenchOrDatabaseDlg { Message = Strings.NoDatabaseMsg, ShowRetry = true }).ShowDialog()) { - case DialogResult.Abort: return; /// Exit program - case DialogResult.Retry: break; /// Retry the loop + case DialogResult.Abort: + return; /// Exit program + + case DialogResult.Retry: + break; /// Retry connection to the database, stay inside the loop + + case DialogResult.Yes: + new UpgradeSelectionDlg().ShowDialog(); + break; /// Stay inside the loop + default: /// Open 'DatabaseSetingsDlg' and retry DB connect on OK if (new Users.Forms.LoginDlg(true).ShowDialog() == DialogResult.Cancel || new BenchesDlg().ShowDialog() == DialogResult.Cancel) { - return; /// Exit program + return; /// Exit program } Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName); - if (Program.LocalSettings == null) return; /// Fatal error - break; /// Retry the loop + if (Program.LocalSettings == null) + return; /// Fatal error -> Exit program + else + break; /// Stay inside the loop } } } // if benchname is OK diff --git a/TestBenchFramework/Properties/AssemblyInfo.cs b/TestBenchFramework/Properties/AssemblyInfo.cs index 171ad7bec..5b48d0e8b 100644 --- a/TestBenchFramework/Properties/AssemblyInfo.cs +++ b/TestBenchFramework/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ using System.Runtime.InteropServices; // Build Number // Revision // -[assembly: AssemblyVersion("2.17.721.1")] -[assembly: AssemblyFileVersion("2.17.721.1")] +[assembly: AssemblyVersion("2.17.722.1")] +[assembly: AssemblyFileVersion("2.17.722.1")]