mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
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:
@@ -19,21 +19,30 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
|||||||
{
|
{
|
||||||
Parent = serverNode;
|
Parent = serverNode;
|
||||||
NodeValue = databaseName;
|
NodeValue = databaseName;
|
||||||
Database db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
|
var db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
|
||||||
db.Refresh();
|
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);
|
CacheInfoFromModel(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the context and ensures that
|
/// Initializes the context and sets its ValidFor property
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected override void EnsureContextInitialized()
|
protected override void EnsureContextInitialized()
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
base.EnsureContextInitialized();
|
base.EnsureContextInitialized();
|
||||||
Database db = SmoObject as Database;
|
var db = SmoObject as Database;
|
||||||
if (context != null && db != null)
|
if (db != null)
|
||||||
{
|
{
|
||||||
context.Database = db;
|
context.Database = db;
|
||||||
}
|
}
|
||||||
@@ -43,8 +52,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
|||||||
|
|
||||||
protected override void PopulateChildren(bool refresh, string name, CancellationToken cancellationToken)
|
protected override void PopulateChildren(bool refresh, string name, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
SmoQueryContext context = this.GetContextAs<SmoQueryContext>();
|
var smoQueryContext = this.GetContextAs<SmoQueryContext>();
|
||||||
if (IsAccessible(context))
|
if (IsAccessible(smoQueryContext))
|
||||||
{
|
{
|
||||||
base.PopulateChildren(refresh, name, cancellationToken);
|
base.PopulateChildren(refresh, name, cancellationToken);
|
||||||
}
|
}
|
||||||
@@ -53,7 +62,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
|||||||
if (string.IsNullOrEmpty(ErrorMessage))
|
if (string.IsNullOrEmpty(ErrorMessage))
|
||||||
{
|
{
|
||||||
// Write error message if it wasn't already set during IsAccessible check
|
// 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
|
try
|
||||||
{
|
{
|
||||||
if (context == null || context.Database == null)
|
return context?.Database == null || context.Database.IsAccessible;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return context.Database.IsAccessible;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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);
|
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "", ex.StackTrace);
|
||||||
Logger.Write(TraceEventType.Error, error);
|
Logger.Write(TraceEventType.Error, error);
|
||||||
ErrorMessage = ex.Message;
|
ErrorMessage = ex.Message;
|
||||||
|
|||||||
Reference in New Issue
Block a user