Skip to content

Commit

Permalink
Improve handling around connection issues
Browse files Browse the repository at this point in the history
Provide a better experience in cases where there are issues connecting to the repository database.  #137
  • Loading branch information
DavidWiseman committed May 5, 2023
1 parent 8cffd29 commit e8eae80
Show file tree
Hide file tree
Showing 8 changed files with 1,858 additions and 1,817 deletions.
14 changes: 11 additions & 3 deletions DBADashGUI/Checks/Summary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ public void RefreshData()
toolStrip1.Invoke(() => { tsRefresh.Enabled = true; tsClearFilter.Enabled = false; });
if (task.Exception != null)
{
refresh1.SetFailed("Error:" + task.Exception.ToString());
refresh1.Invoke(() => { refresh1.SetFailed("Error:" + task.Exception.ToString()); });
return Task.CompletedTask;
}
DataTable dt = task.Result;
GroupSummaryByTest(ref dt);
UpdateSummary(ref dt);
try
{
GroupSummaryByTest(ref dt);
UpdateSummary(ref dt);
}
catch (Exception ex)
{
refresh1.Invoke(() => { refresh1.SetFailed("Error:" + ex.ToString()); });
}
return Task.CompletedTask;
}, cancellationTS.Token);
}
Expand Down
3,445 changes: 1,694 additions & 1,751 deletions DBADashGUI/Main.Designer.cs

Large diffs are not rendered by default.

110 changes: 91 additions & 19 deletions DBADashGUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private async void Main_Load(object sender, EventArgs e)
Properties.Settings.Default.SettingsUpgradeRequired = false;
Properties.Settings.Default.Save();
}

mnuTags.Visible = !commandLine.NoTagMenu;
lblVersion.Text = "Version: " + System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
tabs.TabPages.Clear();
tabs.TabPages.Add(tabDBADash);
Expand All @@ -142,37 +142,75 @@ private async void Main_Load(object sender, EventArgs e)

public async Task SetConnection(RepositoryConnection connection)
{
SetConnectionState(false);
if (connection == null) return;
Common.SetConnectionString(connection);
mnuTags.Visible = !commandLine.NoTagMenu;

if (!CheckRepositoryDBConnection())
try
{
throw new Exception("Error checking repository DB connection");
if (!CheckRepositoryDBConnection(connection.ConnectionString))
{
return;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

await CheckVersion();
try
{
await CheckVersion(connection.ConnectionString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

DBADashUser.GetUser();
GetCommandLineTags();
GetTreeLayout();
BuildTagMenu(commandLineTags);
AddInstanes();
AddTimeZoneMenus();
Common.SetConnectionString(connection);
try
{
DBADashUser.GetUser();
GetCommandLineTags();
GetTreeLayout();
BuildTagMenu(commandLineTags);
AddInstanes();
AddTimeZoneMenus();
SetConnectionState(true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}

private void SetConnectionState(bool isGood)
{
bttnSearch.Enabled = isGood;
optionsToolStripMenuItem.Enabled = isGood;
diffToolStripMenuItem.Enabled = isGood;
if (!isGood)
{
tv1.Nodes.Clear();
tabs.TabPages.Clear();
tabs.TabPages.Add(tabDBADash);
}
}

/// <summary>
/// Check connection to DBA Dash repository DB. User is prompted to retry or cancel on failure.
/// </summary>
/// <returns>True if connection succeeded</returns>
private static bool CheckRepositoryDBConnection()
private static bool CheckRepositoryDBConnection(string connectionString)
{
bool connectionCheckPassed = false;
while (!connectionCheckPassed)
{
try
{
using (var cn = new SqlConnection(Common.ConnectionString))
using (var cn = new SqlConnection(connectionString))
{
cn.Open();
}
Expand All @@ -192,9 +230,9 @@ private static bool CheckRepositoryDBConnection()
return connectionCheckPassed;
}

private static async Task CheckVersion()
private static async Task CheckVersion(string connectionString)
{
var dbVersion = DBValidations.GetDBVersion(Common.ConnectionString);
var dbVersion = DBValidations.GetDBVersion(connectionString);
var appVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
var compare =
(new Version(appVersion.Major, appVersion.Minor)).CompareTo(new Version(dbVersion.Major,
Expand Down Expand Up @@ -1422,6 +1460,7 @@ private void TsTimeFilter_Click(object sender, EventArgs e)
/// </summary>
private void TsHome_Click(object sender, EventArgs e)
{
if (tv1.Nodes.Count == 0) return;
tv1.SelectedNode = tv1.Nodes[0];
tabs.SelectedIndex = 0;
}
Expand Down Expand Up @@ -1570,7 +1609,7 @@ public bool PreFilterMessage(ref Message m)
private async void TsConnect_Click(object sender, EventArgs e)
{
var oldConnection = Common.RepositoryDBConnection;
using var frm = new DBConnection() { ConnectionString = Common.ConnectionString };
using var frm = new DBConnection() { ConnectionString = GetNewConnectionString() };
frm.ShowDialog();
if (frm.DialogResult != DialogResult.OK) return;
try
Expand Down Expand Up @@ -1706,14 +1745,47 @@ private async void TsAddConnection_Click(object sender, EventArgs e)
await AddConnection();
}

private string GetNewConnectionString()
{
string connectionString = Common.ConnectionString;
if (string.IsNullOrEmpty(connectionString))
{
connectionString = (new SqlConnectionStringBuilder()
{
InitialCatalog = "DBADashDB",
IntegratedSecurity = true,
Encrypt = true,
TrustServerCertificate = true
}).ConnectionString;
}
return connectionString;
}

private async Task AddConnection()
{
var oldConnection = Common.RepositoryDBConnection;
using var frm = new DBConnection() { ConnectionString = Common.ConnectionString };
using var frm = new DBConnection() { ConnectionString = GetNewConnectionString() };
frm.ShowDialog();
if (frm.DialogResult != DialogResult.OK) return;
var connection = repositories.FindByConnectionString(frm.ConnectionString);
if (connection != null)
{
MessageBox.Show($"The connection already exists: {connection.Name}", "Add Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

connection = new RepositoryConnection() { ConnectionString = frm.ConnectionString };

try
{
DBValidations.GetDBVersion(connection.ConnectionString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

var connection = new RepositoryConnection() { ConnectionString = frm.ConnectionString };
connection.Name = connection.GetDefaultName();
repositories.Add(connection);
LoadRepositoryConnections();
Expand Down
2 changes: 1 addition & 1 deletion DBADashGUI/Main.resx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAEZTeXN0ZW0uV2luZG93cy5Gb3JtcywgQ3VsdHVyZT1uZXV0cmFs
LCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAmU3lzdGVtLldpbmRvd3MuRm9ybXMu
SW1hZ2VMaXN0U3RyZWFtZXIBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAApBsAAAJNU0Z0AUkBTAIBARUB
AAFgAREBYAERARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMAAUADAAFgAwABAQEAAQgG
AAEQARIBEAESARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMAAUADAAFgAwABAQEAAQgG
AAEYGAABgAIAAYADAAKAAQABgAMAAYABAAGAAQACgAIAA8ABAAHAAdwBwAEAAfABygGmAQABMwUAATMB
AAEzAQABMwEAAjMCAAMWAQADHAEAAyIBAAMpAQADVQEAA00BAANCAQADOQEAAYABfAH/AQACUAH/AQAB
kwEAAdYBAAH/AewBzAEAAcYB1gHvAQAB1gLnAQABkAGpAa0CAAH/ATMDAAFmAwABmQMAAcwCAAEzAwAC
Expand Down
50 changes: 24 additions & 26 deletions DBADashGUI/Refresh.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions DBADashGUI/Refresh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,10 @@ public void SetFailed(string message)

public void SetMessage(string message, Color backColor, Color foreColor)
{
timer1.Enabled = false;
this.BackColor = backColor;
this.ForeColor = foreColor;
lblRefresh.Text = message;
timer1.Enabled = false;
}

private void Refresh_VisibilityChanged(object sender, EventArgs e)
{
Reset();
}
}
}
26 changes: 24 additions & 2 deletions DBADashGUI/RepositoryConnections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ public RepositoryConnection FindByName(string name)
return this.FirstOrDefault(c => c.Name.Equals(name));
}

public RepositoryConnection FindByConnectionString(string connectionString)
{
return this.FirstOrDefault(c => CompareConnections(c.ConnectionString, connectionString));
}

private bool CompareConnections(string connectionString1, string connectionString2)
{
var builder1 = new SqlConnectionStringBuilder(connectionString1);
var builder2 = new SqlConnectionStringBuilder(connectionString2);
return builder1.DataSource == builder2.DataSource
&& builder1.InitialCatalog == builder2.InitialCatalog
&& builder1.UserID == builder2.UserID
&& builder1.IntegratedSecurity == builder2.IntegratedSecurity;
}

public bool Contains(string name)
{
return FindByName(name) != null;
Expand Down Expand Up @@ -54,8 +69,15 @@ private static string GetStorageFilePath()
public void Save()
{
string filePath = GetStorageFilePath();
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(filePath, json);
if (Count == 0)
{
File.Delete(filePath);
}
else
{
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(filePath, json);
}
}

public static RepositoryConnectionList GetRepositoryConnectionList()
Expand Down
21 changes: 12 additions & 9 deletions DBADashSharedGUI/CommonShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ public static void ShowAbout(IWin32Window owner, bool StartGUIOnUpgrade)
[SupportedOSPlatform("windows")]
public static void ShowAbout(string connectionString, IWin32Window owner, bool StartGUIOnUpgrade)
{
Version DBVersion;
try
Version dbVersion= new Version();
if (!string.IsNullOrEmpty(connectionString))
{
DBVersion = DBADash.DBValidations.GetDBVersion(connectionString);
}
catch (Exception ex)
{
MessageBox.Show("Error getting repository version" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
DBVersion = new Version();
try
{
dbVersion = DBADash.DBValidations.GetDBVersion(connectionString);
}
catch (Exception ex)
{
MessageBox.Show(@"Error getting repository version: " + ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
using About frm = new()
{
DBVersion = DBVersion,
DBVersion = dbVersion,
StartGUIOnUpgrade = StartGUIOnUpgrade
};
frm.ShowDialog(owner);
Expand Down

0 comments on commit e8eae80

Please sign in to comment.