Skip to content

Commit

Permalink
Fix/restore connection issue (#416)
Browse files Browse the repository at this point in the history
* fixed the connection issue used for restore

* fixed a test
  • Loading branch information
llali authored Jul 17, 2017
1 parent 836847c commit d5b2bcd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ internal async Task HandleRestorePlanRequest(
RestoreParams restoreParams,
RequestContext<RestorePlanResponse> requestContext)
{
RestorePlanResponse response = new RestorePlanResponse();

try
{
RestorePlanResponse response = new RestorePlanResponse();
ConnectionInfo connInfo;
bool supported = IsBackupRestoreOperationSupported(restoreParams, out connInfo);

Expand All @@ -143,7 +144,9 @@ internal async Task HandleRestorePlanRequest(
}
catch (Exception ex)
{
await requestContext.SendError(ex.ToString());
response.CanRestore = false;
response.ErrorMessage = ex.Message;
await requestContext.SendResult(response);
}
}

Expand All @@ -154,9 +157,10 @@ internal async Task HandleRestoreRequest(
RestoreParams restoreParams,
RequestContext<RestoreResponse> requestContext)
{
RestoreResponse response = new RestoreResponse();

try
{
RestoreResponse response = new RestoreResponse();
ConnectionInfo connInfo;
bool supported = IsBackupRestoreOperationSupported(restoreParams, out connInfo);

Expand Down Expand Up @@ -199,7 +203,9 @@ internal async Task HandleRestoreRequest(
}
catch (Exception ex)
{
await requestContext.SendError(ex.ToString());
response.Result = false;
response.ErrorMessage = ex.Message;
await requestContext.SendResult(response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

using System;
using System.Linq;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts;
using Microsoft.SqlTools.ServiceLayer.TaskServices;
using Microsoft.SqlTools.Utility;

namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
{
Expand Down Expand Up @@ -219,7 +223,24 @@ public RestoreDatabaseTaskDataObject CreateRestoreDatabaseTaskDataObject(Restore

if (connInfo != null)
{
Server server = new Server(new ServerConnection(connInfo.ConnectionDetails.ServerName));
SqlConnection connection;
DbConnection dbConnection = connInfo.AllConnections.First();
ReliableSqlConnection reliableSqlConnection = dbConnection as ReliableSqlConnection;
SqlConnection sqlConnection = dbConnection as SqlConnection;
if (reliableSqlConnection != null)
{
connection = reliableSqlConnection.GetUnderlyingConnection();
}
else if (sqlConnection != null)
{
connection = sqlConnection;
}
else
{
Logger.Write(LogLevel.Warning, "Cannot find any sql connection for restore operation");
return null;
}
Server server = new Server(new ServerConnection(connection));

RestoreDatabaseTaskDataObject restoreDataObject = new RestoreDatabaseTaskDataObject(server, restoreParams.DatabaseName);
restoreDataObject.RestoreParams = restoreParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ private async Task<RestorePlanResponse> VerifyRestore(string backupFileName, boo
if (canRestore)
{
Assert.True(response.DbFiles.Any());
Assert.Equal(response.DatabaseName, "BackupTestDb");
if (string.IsNullOrEmpty(targetDatabase))
{
targetDatabase = response.DatabaseName;
}
Assert.Equal(response.DatabaseName, targetDatabase);

if(execute)
{
await DropDatabase(targetDatabase);
Expand Down

0 comments on commit d5b2bcd

Please sign in to comment.