From 7e6bef90086422121b17582b09ae5fd83b9b995f Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 3 Oct 2017 03:04:06 +0200 Subject: [PATCH] DB upgrade via menu Settings/Upgrade from 2.15 to 2.17 implemented, ver. 2.17.684 --- .../Forms/UpgradeSelectionDlg.Designer.cs | 68 +++------------ .../Forms/UpgradeSelectionDlg.cs | 82 +++++-------------- TestBenchFramework/MainWnd.cs | 2 +- TestBenchFramework/Properties/AssemblyInfo.cs | 4 +- Users/Entities/User.cs | 1 + 5 files changed, 36 insertions(+), 121 deletions(-) diff --git a/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs b/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs index dcbe1ad8a..5bf58808f 100644 --- a/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs +++ b/TestBenchFramework/Forms/UpgradeSelectionDlg.Designer.cs @@ -28,78 +28,34 @@ /// private void InitializeComponent() { - this.pressureRepresentationButton = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); - this.label2 = new System.Windows.Forms.Label(); - this.textBox1 = new System.Windows.Forms.TextBox(); - this.textBox2 = new System.Windows.Forms.TextBox(); + this.upgradeDbButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // pressureRepresentationButton + // upgradeDbButton // - this.pressureRepresentationButton.Location = new System.Drawing.Point(12, 61); - this.pressureRepresentationButton.Name = "pressureRepresentationButton"; - this.pressureRepresentationButton.Size = new System.Drawing.Size(260, 35); - this.pressureRepresentationButton.TabIndex = 0; - this.pressureRepresentationButton.Text = "Upgrade pressure representation"; - this.pressureRepresentationButton.UseVisualStyleBackColor = true; - this.pressureRepresentationButton.Click += new System.EventHandler(this.pressureRepresentationButton_Click); - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(12, 13); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(70, 13); - this.label1.TabIndex = 1; - this.label1.Text = "From batch #"; - // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(12, 38); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(60, 13); - this.label2.TabIndex = 2; - this.label2.Text = "To batch #"; - // - // textBox1 - // - this.textBox1.Location = new System.Drawing.Point(103, 10); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(58, 20); - this.textBox1.TabIndex = 3; - // - // textBox2 - // - this.textBox2.Location = new System.Drawing.Point(103, 35); - this.textBox2.Name = "textBox2"; - this.textBox2.Size = new System.Drawing.Size(58, 20); - this.textBox2.TabIndex = 4; + 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); // // 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.textBox2); - this.Controls.Add(this.textBox1); - this.Controls.Add(this.label2); - this.Controls.Add(this.label1); - this.Controls.Add(this.pressureRepresentationButton); + this.Controls.Add(this.upgradeDbButton); this.Name = "UpgradeSelectionDlg"; this.Text = "Upgrade selection"; this.ResumeLayout(false); - this.PerformLayout(); } #endregion - private System.Windows.Forms.Button pressureRepresentationButton; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox textBox1; - private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.Button upgradeDbButton; + } } \ No newline at end of file diff --git a/TestBenchFramework/Forms/UpgradeSelectionDlg.cs b/TestBenchFramework/Forms/UpgradeSelectionDlg.cs index 050f469bc..61f28655a 100644 --- a/TestBenchFramework/Forms/UpgradeSelectionDlg.cs +++ b/TestBenchFramework/Forms/UpgradeSelectionDlg.cs @@ -1,13 +1,7 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; -using Results.Entities; -using NHibernate; +using MySql.Data.MySqlClient; +using Results; namespace TBF.Forms { @@ -21,74 +15,38 @@ namespace TBF.Forms InitializeComponent(); batchFrom = 1; batchTo = Program.LocalSettings.BatchNr - 1; - - textBox1.Text = batchFrom.ToString(); - textBox2.Text = batchTo.ToString(); } - private void pressureRepresentationButton_Click(object sender, EventArgs e) + private void upgradeDbButton_Click(object sender, EventArgs e) { - try + MySqlConnection dbConn = new MySqlConnection(Results.DB.ConnectionString); + string[] commands = new string[] { - batchFrom = int.Parse(textBox1.Text); - batchTo = int.Parse(textBox2.Text); - if (batchFrom < 1 || batchFrom > batchTo) throw new Exception(); - } - catch - { - MessageBox.Show("Wrong 'From batch #' or 'To batch #'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return; - } - - if (MessageBox.Show("Are you sure ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; + "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`;", + }; try { - ISession session = Results.DB.CreateSession(); + dbConn.Open(); - IList bFrom = session.QueryOver().Where(x => (x.BatchNr == batchFrom)).List(); - IList bTo = session.QueryOver().Where(x => (x.BatchNr == batchTo)).List(); - - if (bFrom.Count != 1) throw new Exception("No start batch"); - if (bTo.Count != 1) throw new Exception("No end batch"); - - IList testResults = session.QueryOver() - .WhereRestrictionOn(x => x.StartTime) - .IsBetween(bFrom[0].StartTime) - .And(bTo[0].EndTime) - .List(); - - foreach (var tr in testResults) + foreach (var cmdText in commands) { - Text = string.Format("Batch # = {0}", tr.Batch.BatchNr); - - float factor1 = 0.01f; - - tr.PressUpMean *= factor1; - tr.PressUpStart *= factor1; - tr.PressUpEnd *= factor1; - tr.PressUpMin *= factor1; - tr.PressUpMax *= factor1; - - tr.PressDownMean *= factor1; - tr.PressDownStart *= factor1; - tr.PressDownEnd *= factor1; - tr.PressDownMin *= factor1; - tr.PressDownMax *= factor1; - - tr.AmbPressMean *= 0.001f; - - session.SaveOrUpdate(tr); + MySqlCommand cmd = dbConn.CreateCommand(); + cmd.CommandText = cmdText; + cmd.ExecuteNonQuery(); } - session.Flush(); - - MessageBox.Show("Upgrade completed", "", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); + upgradeDbButton.Enabled = false; } catch (Exception exc) { - MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + finally + { + dbConn.Close(); } } } diff --git a/TestBenchFramework/MainWnd.cs b/TestBenchFramework/MainWnd.cs index 19038963c..e485657ea 100644 --- a/TestBenchFramework/MainWnd.cs +++ b/TestBenchFramework/MainWnd.cs @@ -101,7 +101,7 @@ namespace TBF usersTSMenuItem.Visible = Users.GlobalData.CurrentUser.IsMemberOf(Users.Grp.GID.Administrators) || Users.GlobalData.CurrentUser.IsMemberOf(Users.Grp.GID.HeadOfLab); - upgradeTSMenuItem.Visible = Users.GlobalData.CurrentUser.IsMemberOf(Users.Grp.GID.Administrators) && Users.GlobalData.CurrentUser.UserName == "milan"; + upgradeTSMenuItem.Visible = Users.GlobalData.CurrentUser.IsMemberOf(Users.Grp.GID.Administrators) && Users.GlobalData.CurrentUser.IsPowerUser(); benchInitializationFailed = false; /// diff --git a/TestBenchFramework/Properties/AssemblyInfo.cs b/TestBenchFramework/Properties/AssemblyInfo.cs index 354c6c928..d226b9da0 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.681.1")] -[assembly: AssemblyFileVersion("2.17.681.1")] +[assembly: AssemblyVersion("2.17.684.1")] +[assembly: AssemblyFileVersion("2.17.684.1")] diff --git a/Users/Entities/User.cs b/Users/Entities/User.cs index 50aa207ad..0a084e93b 100644 --- a/Users/Entities/User.cs +++ b/Users/Entities/User.cs @@ -22,6 +22,7 @@ namespace Users.Entities public virtual IList Groups { get; set; } //User can be a member of a list of groups private bool powerUser; /// true for built-in users, power users have full access even with empty Groups list + public virtual bool IsPowerUser() { return powerUser; } private string legalizator; /// Not mapped to a database !!!, 2nd user entered in a logging dialog public virtual void SetLegalizator(string value) { legalizator = value; }