UpgradeSelectionDlg : v.2.22 -> v.2.24 and v.2.24 -> v.2.25

This commit is contained in:
Milan Hanajik
2020-01-21 17:40:23 +01:00
parent 7c070fc635
commit 670de424c2
6 changed files with 383 additions and 38 deletions
+287 -20
View File
@@ -1,4 +1,7 @@
using System;
///
/// Copyright (c) 2016-2020 Sensus slovensko a.s.
///
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
@@ -38,13 +41,13 @@ namespace TBF.UI.Settings
void ActivityStart()
{
form = new Shared.ModelessActivityForm()
{
Message = "Upgrading database",
FontFamily = "Arial",
FontSize = 24,
FontStyle = FontStyle.Regular,
BackgroundColor = Color.Green,
};
{
Message = "Upgrading database",
FontFamily = "Arial",
FontSize = 24,
FontStyle = FontStyle.Regular,
BackgroundColor = Color.Green,
};
formThread = new System.Threading.Thread(() => form.ShowDialog());
formThread.Start();
}
@@ -99,6 +102,63 @@ namespace TBF.UI.Settings
return success;
}
/// <summary>
/// Common part of database upgrade code
/// </summary>
/// <param name="dbConn">Database connection</param>
/// <param name="commands">Array of SQL commands to be executed</param>
/// <returns>true = successful</returns>
bool ChangeErrorFlags(string errorFlagsCmpntName)
{
bool success = false;
ISession session = null;
try
{
session = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config);
var testParams = session.QueryOver<Config.Entities.ComponentTest>()
.Where(x => (x.CmpntName == errorFlagsCmpntName))
.List();
foreach (var tp in testParams)
{
string temp;
if (tp.Parameters.Contains("<Mode>Info</Mode>"))
{
temp = tp.Parameters.Replace("true", "1");
}
else if (tp.Parameters.Contains("<Mode>On</Mode>"))
{
temp = tp.Parameters.Replace("true", "2");
}
else
{
temp = tp.Parameters.Replace("true", "0");
}
tp.Parameters = temp.Replace("false", "0");
session.SaveOrUpdate(tp);
}
session.Flush();
success = true;
}
catch (Exception exc)
{
ActivityEnd();
MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message),
"Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (session != null) session.Close();
}
return success;
}
///---------------------------------------------------------------------
private void upgradeDb_NFC_button_Click(object sender, EventArgs e)
{
@@ -131,7 +191,7 @@ namespace TBF.UI.Settings
MessageBox.Show("No database selected");
return;
}
string[] configDbCommands = new string[]
{
/// 2.4 -> 2.5
@@ -182,7 +242,7 @@ namespace TBF.UI.Settings
MessageBox.Show("No database selected");
return;
}
string[] configDbCommands = new string[]
{
/// 2.9 -> 2.10
@@ -310,7 +370,7 @@ namespace TBF.UI.Settings
}
}
}
///---------------------------------------------------------------------
private void upgradeDb_212_215_Button_Click(object sender, EventArgs e)
{
@@ -486,8 +546,8 @@ namespace TBF.UI.Settings
}
}
///---------------------------------------------------------------------
private void upgradeDb_215_216_Button_Click(object sender, EventArgs e)
///---------------------------------------------------------------------
private void upgradeDb_215_216_Button_Click(object sender, EventArgs e)
{
if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked)
{
@@ -510,8 +570,8 @@ namespace TBF.UI.Settings
}
}
///---------------------------------------------------------------------
private void upgradeDb_216_217_Button_Click(object sender, EventArgs e)
///---------------------------------------------------------------------
private void upgradeDb_216_217_Button_Click(object sender, EventArgs e)
{
if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked)
{
@@ -596,7 +656,75 @@ namespace TBF.UI.Settings
string[] configDbCommands = new string[]
{
"ALTER TABLE `user` ADD `Password2` VARCHAR(255) NULL DEFAULT NULL AFTER `Password`, ADD `Password3` VARCHAR(255) NULL DEFAULT NULL AFTER `Password2`;",
/// Table profile
"CREATE TABLE IF NOT EXISTS `profile` (" +
"`Id` int(11) NOT NULL," +
" `ItemNr` int(11) DEFAULT NULL," +
" `Name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" `Description` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" `CreationUser` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" `CreationTime` datetime DEFAULT NULL," +
" `LastChgUser` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" `LastChgTime` datetime DEFAULT NULL," +
" `ProfileType` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" `ErrorLimitType` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," +
" PRIMARY KEY (`Id`));",
/// Table ptest
"CREATE TABLE IF NOT EXISTS `ptest` (" +
"`Id` int(11) NOT NULL," +
" `ItemNr` int(11) DEFAULT NULL," +
" `Name` varchar(255) DEFAULT NULL," +
" `Part` int(11) NOT NULL DEFAULT '0'," +
" `CoefQfrom` double NOT NULL DEFAULT '0'," +
" `CoefQto` double NOT NULL DEFAULT '0'," +
" `ErrLimLo` double NOT NULL DEFAULT '0'," +
" `ErrLimHi` double NOT NULL DEFAULT '0'," +
" `TempLimLo` double NOT NULL DEFAULT '0'," +
" `TempLimHi` double NOT NULL DEFAULT '0'," +
" `PressLimLo` double NOT NULL DEFAULT '0'," +
" `PressLimHi` double NOT NULL DEFAULT '0'," +
" `Publish` tinyint(4) NOT NULL DEFAULT '1'," +
" `Evaluate` boolean NOT NULL DEFAULT TRUE," +
" `E1` tinyint(4) NOT NULL DEFAULT '0'," +
" `E2` tinyint(4) NOT NULL DEFAULT '0'," +
" `E3` tinyint(4) NOT NULL DEFAULT '0'," +
" `E4` tinyint(4) NOT NULL DEFAULT '0'," +
" `E5` tinyint(4) NOT NULL DEFAULT '0'," +
" `E6` tinyint(4) NOT NULL DEFAULT '0'," +
" `E7` tinyint(4) NOT NULL DEFAULT '0'," +
" `E8` tinyint(4) NOT NULL DEFAULT '0'," +
" `E9` tinyint(4) NOT NULL DEFAULT '0'," +
" `E10` tinyint(4) NOT NULL DEFAULT '0'," +
" `E11` tinyint(4) NOT NULL DEFAULT '0'," +
" `E12` tinyint(4) NOT NULL DEFAULT '0'," +
" `E13` tinyint(4) NOT NULL DEFAULT '0'," +
" `E14` tinyint(4) NOT NULL DEFAULT '0'," +
" `E15` tinyint(4) NOT NULL DEFAULT '0'," +
" `E16` tinyint(4) NOT NULL DEFAULT '0'," +
" `E21` tinyint(4) NOT NULL DEFAULT '0'," +
" `Delta_Q_pct` double NOT NULL DEFAULT '0'," +
" `Delta_T` double NOT NULL DEFAULT '0'," +
" `Delta_Tup_Tdn` double NOT NULL DEFAULT '0'," +
" `TestTime_min` double NOT NULL DEFAULT '0'," +
" `TestTime_max` double NOT NULL DEFAULT '0'," +
" `AmbTemp_min` double NOT NULL DEFAULT '0'," +
" `AmbTemp_max` double NOT NULL DEFAULT '0'," +
" `Coef_E9` double NOT NULL DEFAULT '0'," +
" `Coef_E10` double NOT NULL DEFAULT '0'," +
" `Coef_E11` double NOT NULL DEFAULT '0'," +
" `Coef_E12` double NOT NULL DEFAULT '0'," +
" `Offset_E9` double NOT NULL DEFAULT '0'," +
" `Offset_E12` double NOT NULL DEFAULT '0'," +
" `Profile_id` int(11) NOT NULL DEFAULT '0'," +
" PRIMARY KEY (`Id`));",
/// Table test
"ALTER TABLE `test` ADD `Qtg` FLOAT NULL DEFAULT NULL AFTER `Evaluate`;",
"ALTER TABLE `test` CHANGE `RelTransAfter` `Profile` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;",
"UPDATE `test` SET Profile = 'UserDefined';",
/// Table uncertainty
"CREATE TABLE IF NOT EXISTS `uncertainty` (" +
" `Id` int(11) NOT NULL AUTO_INCREMENT," +
" `Measurement` float NOT NULL DEFAULT '0'," +
@@ -608,19 +736,54 @@ namespace TBF.UI.Settings
" `Component_id` int(11) DEFAULT NULL," +
" PRIMARY KEY (`Id`)," +
" KEY `Component_id` (`Component_id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;",
"UPDATE `test` SET `RelTransAfter`='UserDefined';",
");",
/// Table user
"ALTER TABLE `user` ADD `Password2` VARCHAR(255) NULL DEFAULT NULL AFTER `Password`, ADD `Password3` VARCHAR(255) NULL DEFAULT NULL AFTER `Password2`, ADD `Password4` VARCHAR(255) NULL DEFAULT NULL AFTER `Password3`;",
/// Tables watermeter and wmtype
"DROP TABLE `watermeter`",
"DROP TABLE `wmtype`",
//"ALTER TABLE `procedure` DROP `ProtocolTitle`",
};
string[] resultsDbCommands = new string[]
{
"ALTER TABLE `testdata` ADD `Qtg` DOUBLE NULL DEFAULT NULL AFTER `Name`;",
"ALTER TABLE `testrslt` ADD `Uncertnt` FLOAT NOT NULL DEFAULT '0' AFTER `FlowMax`, ADD `UncertntScale` FLOAT NOT NULL DEFAULT '0' AFTER `Uncertnt`, ADD `UncertntDensity` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntScale`, ADD `UncertntTemp` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntDensity`, ADD `UncertntPressure` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntTemp`;",
"ALTER TABLE `testrslt` CHANGE `ErrorFlags` `ErrorFlags` BIGINT(20) NOT NULL DEFAULT '0', CHANGE `ErrorFlagsMd` `InfoFlags` BIGINT(20) NOT NULL DEFAULT '0';",
"ALTER TABLE `watermeter` CHANGE `ErrorFlags` `ErrorFlags` BIGINT(20) NOT NULL DEFAULT '0', ADD `InfoFlags` BIGINT NOT NULL DEFAULT '0' AFTER `ErrorFlags`;",
#if IPERL
"ALTER TABLE `watermeter` DROP `Q2CorrectionDone`, DROP `Q2Correction`;",
#endif
};
ActivityStart();
bool success = true;
if ((!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) &&
(!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(DB.ConnectionString), resultsDbCommands)))
if (upgradeConfigDBCheckBox.Checked)
{
success = success && ExecuteCommands(new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands);
if (!string.IsNullOrEmpty(errorFlagsComponentsTextBox.Text))
{
string[] errorFlagsCmpnts = errorFlagsComponentsTextBox.Text.Split(new char[] { '~' });
foreach (var errFCmpnt in errorFlagsCmpnts)
{
success = success && ChangeErrorFlags(errFCmpnt);
}
}
}
if (upgradeResultsDBCheckBox.Checked)
{
success = success && ExecuteCommands(new MySqlConnection(DB.ConnectionString), resultsDbCommands);
}
if (success)
{
ActivityEnd();
MessageBox.Show("DB succesfully upgraded." + Environment.NewLine +
@@ -632,6 +795,110 @@ namespace TBF.UI.Settings
}
}
private void upgradeDb_224_225_Button_Click(object sender, EventArgs e)
{
MySqlConnection dbConn = null;
MySqlCommand cmd;
ActivityStart();
bool success = false;
try
{
dbConn = new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString);
dbConn.Open();
cmd = dbConn.CreateCommand();
cmd.CommandText = "CREATE TABLE IF NOT EXISTS `usersgroups` ( `Group_id` int(11) NOT NULL, `User_id` int(11) NOT NULL );";
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = dbConn.CreateCommand();
cmd.CommandText = "ALTER TABLE `usersgroups` ADD KEY `User_id` (`User_id`), ADD KEY `Group_id` (`Group_id`)," +
" ADD CONSTRAINT `FKEC3AF23393C4061C` FOREIGN KEY (`Group_id`) REFERENCES `group` (`Id`)," +
" ADD CONSTRAINT `FKEC3AF23373767C99` FOREIGN KEY (`User_id`) REFERENCES `user` (`Id`);";
cmd.ExecuteNonQuery();
cmd.Dispose();
int[] groupIdsToKeep = new int[(int)Users.Entities.GID.NrOfGroups];
IList<GroupTableRow> groupTable = new List<GroupTableRow>();
cmd = dbConn.CreateCommand();
cmd.CommandText = "SELECT `Id`, `Gid`, `User_id` FROM `group`;";
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
int groupId = dr.GetInt32(0);
Users.Entities.GID gid = (Users.Entities.GID)dr.GetInt32(1);
int userId = dr.GetInt32(2);
groupTable.Add(new GroupTableRow(groupId, gid, userId));
if ((gid >= 0) && (gid < Users.Entities.GID.NrOfGroups) && (groupIdsToKeep[(int)gid] == 0))
{
groupIdsToKeep[(int)gid] = groupId;
}
}
dr.Close();
foreach (var groupTableRow in groupTable)
{
cmd = dbConn.CreateCommand();
cmd.CommandText = string.Format("INSERT INTO `usersgroups` (`Group_id`, `User_id`) VALUES ({0}, {1});",
groupIdsToKeep[(int)groupTableRow.Gid],
groupTableRow.UserId);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
//foreach (var groupTableRow in groupTable)
//{
// bool deleteThisRow = true;
// for (int i = 0; i < groupIdsToKeep.Length; i++)
// {
// if (groupTableRow.GroupId == groupIdsToKeep[i])
// {
// deleteThisRow = false;
// break;
// }
// }
// if (deleteThisRow)
// {
// cmd = dbConn.CreateCommand();
// cmd.CommandText = string.Format("DELETE FROM `group` WHERE `Id` = {0}", groupTableRow.GroupId);
// cmd.ExecuteNonQuery();
// cmd.Dispose();
// }
//}
cmd = dbConn.CreateCommand();
cmd.CommandText = string.Format("ALTER TABLE `group` ADD `AccessFlags` BIGINT NOT NULL DEFAULT '0' AFTER `Name`;");
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
{
if (dbConn != null) dbConn.Close();
}
if (success)
{
ActivityEnd();
MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
upgradeDb_224_225_Button.Enabled = false;
}
}
private void pressureRepresentationButton_Click(object sender, EventArgs e)
{
try