Add force change database logic (#519)

* add logic to close connections if changing fails

* added logic to close connections and reopen that fail to change (azure)

* expose connection map in connection info, change while to foreach

* removed unneeded code

* reworked logic to not depend on thrown errors

* added tests
This commit is contained in:
Anthony Dresser
2017-10-25 10:54:17 -07:00
committed by GitHub
parent f80fd8a458
commit 399b03cbd1
3 changed files with 194 additions and 13 deletions

View File

@@ -1201,7 +1201,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
ChangeDatabaseParams changeDatabaseParams,
RequestContext<bool> requestContext)
{
await requestContext.SendResult(ChangeConnectionDatabaseContext(changeDatabaseParams.OwnerUri, changeDatabaseParams.NewDatabase));
await requestContext.SendResult(ChangeConnectionDatabaseContext(changeDatabaseParams.OwnerUri, changeDatabaseParams.NewDatabase, true));
}
/// <summary>
@@ -1209,23 +1209,42 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
/// </summary>
/// <param name="ownerUri">URI of the owner of the connection</param>
/// <param name="newDatabaseName">Name of the database to change the connection to</param>
public bool ChangeConnectionDatabaseContext(string ownerUri, string newDatabaseName)
public bool ChangeConnectionDatabaseContext(string ownerUri, string newDatabaseName, bool force = false)
{
ConnectionInfo info;
if (TryFindConnection(ownerUri, out info))
{
try
{
foreach (DbConnection connection in info.AllConnections)
{
if (connection.State == ConnectionState.Open)
{
connection.ChangeDatabase(newDatabaseName);
}
}
info.ConnectionDetails.DatabaseName = newDatabaseName;
foreach (string key in info.AllConnectionTypes)
{
DbConnection conn;
info.TryGetConnection(key, out conn);
if (conn != null && conn.Database != newDatabaseName && conn.State == ConnectionState.Open)
{
if (info.IsCloud && force)
{
conn.Close();
conn.Dispose();
info.RemoveConnection(key);
string connectionString = BuildConnectionString(info.ConnectionDetails);
// create a sql connection instance
DbConnection connection = info.Factory.CreateSqlConnection(connectionString);
connection.Open();
info.AddConnection(key, connection);
}
else
{
conn.ChangeDatabase(newDatabaseName);
}
}
}
// Fire a connection changed event
ConnectionChangedParams parameters = new ConnectionChangedParams();
IConnectionSummary summary = info.ConnectionDetails;