Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fall back to master when connected to secondary #854

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@ public DatabaseTreeNode(ServerNode serverNode, string databaseName): this()
{
Parent = serverNode;
NodeValue = databaseName;
Database db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
var db = new Database(serverNode.GetContextAs<SmoQueryContext>().Server, this.NodeValue);
shueybubbles marked this conversation as resolved.
Show resolved Hide resolved
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
shueybubbles marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Expand All @@ -43,8 +52,8 @@ protected override void EnsureContextInitialized()

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);
}
Expand All @@ -53,7 +62,7 @@ protected override void PopulateChildren(bool refresh, string name, Cancellation
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);
}
}
}
Expand All @@ -62,15 +71,11 @@ public bool IsAccessible(SmoQueryContext context)
{
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;
Expand Down