fall back to master when connected to secondary (#854)

* fall back to master when connected to secondary

* use var per feedback
This commit is contained in:
David Shiflet
2019-08-22 12:16:28 -07:00
committed by GitHub
parent b01bdeba32
commit 4f928133e1

View File

@@ -19,21 +19,30 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
{
Parent = serverNode;
NodeValue = databaseName;
Database db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
var db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
db.Refresh();
// If we got this far, the connection is valid. However, it's possible
// the user connected directly to the master of a readable secondary
// In that case, the name in the connection string won't be found in sys.databases
// We detect that here and fall back to master
if (db.State == SqlSmoState.Creating)
{
db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, "master");
db.Refresh();
}
CacheInfoFromModel(db);
}
/// <summary>
/// Initializes the context and ensures that
/// Initializes the context and sets its ValidFor property
/// </summary>
protected override void EnsureContextInitialized()
{
if (context == null)
{
base.EnsureContextInitialized();
Database db = SmoObject as Database;
if (context != null && db != null)
var db = SmoObject as Database;
if (db != null)
{
context.Database = db;
}
@@ -43,8 +52,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
protected override void PopulateChildren(bool refresh, string name, CancellationToken cancellationToken)
{
SmoQueryContext context = this.GetContextAs<SmoQueryContext>();
if (IsAccessible(context))
var smoQueryContext = this.GetContextAs<SmoQueryContext>();
if (IsAccessible(smoQueryContext))
{
base.PopulateChildren(refresh, name, cancellationToken);
}
@@ -53,7 +62,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
if (string.IsNullOrEmpty(ErrorMessage))
{
// Write error message if it wasn't already set during IsAccessible check
ErrorMessage = string.Format(CultureInfo.InvariantCulture, SR.DatabaseNotAccessible, context.Database.Name);
ErrorMessage = string.Format(CultureInfo.InvariantCulture, SR.DatabaseNotAccessible, this.NodeValue);
}
}
}
@@ -62,15 +71,11 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
{
try
{
if (context == null || context.Database == null)
{
return true;
}
return context.Database.IsAccessible;
return context?.Database == null || context.Database.IsAccessible;
}
catch (Exception ex)
{
string error = string.Format(CultureInfo.InvariantCulture, "Failed to get IsAccessible. error:{0} inner:{1} stacktrace:{2}",
var error = string.Format(CultureInfo.InvariantCulture, "Failed to get IsAccessible. error:{0} inner:{1} stacktrace:{2}",
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "", ex.StackTrace);
Logger.Write(TraceEventType.Error, error);
ErrorMessage = ex.Message;