mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-31 01:25:42 -05:00
Add DacFx Generate Deploy Script operation (#759)
* Adding DacFx Generate Deploy Script operation
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters for a DacFx generate deploy script request.
|
||||
/// </summary>
|
||||
public class GenerateDeployScriptParams : DacFxParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the filepath where to save the generated script
|
||||
/// </summary>
|
||||
public string ScriptFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether a Deployment Report should be generated during deploy.
|
||||
/// </summary>
|
||||
public bool GenerateDeploymentReport { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the filepath where to save the deployment report
|
||||
/// </summary>
|
||||
public string DeploymentReportFilePath { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the DacFx generate deploy script request type
|
||||
/// </summary>
|
||||
class GenerateDeployScriptRequest
|
||||
{
|
||||
public static readonly RequestType<GenerateDeployScriptParams, DacFxResult> Type =
|
||||
RequestType<GenerateDeployScriptParams, DacFxResult>.Create("dacfx/generateDeploymentScript");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System;
|
||||
@@ -27,14 +28,14 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
|
||||
public SqlTask SqlTask { get; set; }
|
||||
|
||||
protected SqlConnection SqlConnection { get; private set; }
|
||||
protected string ConnectionString { get; private set; }
|
||||
|
||||
protected DacServices DacServices { get; private set; }
|
||||
|
||||
protected DacFxOperation(SqlConnection sqlConnection)
|
||||
protected DacFxOperation(ConnectionInfo connInfo)
|
||||
{
|
||||
Validate.IsNotNull("sqlConnection", sqlConnection);
|
||||
this.SqlConnection = sqlConnection;
|
||||
Validate.IsNotNull("connectionInfo", connInfo);
|
||||
this.ConnectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
|
||||
this.OperationId = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
|
||||
try
|
||||
{
|
||||
this.DacServices = new DacServices(this.SqlConnection.ConnectionString);
|
||||
this.DacServices = new DacServices(this.ConnectionString);
|
||||
Execute();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -43,6 +43,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
serviceHost.SetRequestHandler(ImportRequest.Type, this.HandleImportRequest);
|
||||
serviceHost.SetRequestHandler(ExtractRequest.Type, this.HandleExtractRequest);
|
||||
serviceHost.SetRequestHandler(DeployRequest.Type, this.HandleDeployRequest);
|
||||
serviceHost.SetRequestHandler(GenerateDeployScriptRequest.Type, this.HandleGenerateDeployScriptRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -64,8 +65,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
out connInfo);
|
||||
if (connInfo != null)
|
||||
{
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, "Export");
|
||||
ExportOperation operation = new ExportOperation(parameters, sqlConn);
|
||||
ExportOperation operation = new ExportOperation(parameters, connInfo);
|
||||
await ExecuteOperation(operation, parameters, "Export bacpac", requestContext);
|
||||
}
|
||||
}
|
||||
@@ -89,8 +89,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
out connInfo);
|
||||
if (connInfo != null)
|
||||
{
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, "Import");
|
||||
ImportOperation operation = new ImportOperation(parameters, sqlConn);
|
||||
ImportOperation operation = new ImportOperation(parameters, connInfo);
|
||||
await ExecuteOperation(operation, parameters, "Import bacpac", requestContext);
|
||||
}
|
||||
}
|
||||
@@ -114,8 +113,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
out connInfo);
|
||||
if (connInfo != null)
|
||||
{
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, "Extract");
|
||||
ExtractOperation operation = new ExtractOperation(parameters, sqlConn);
|
||||
ExtractOperation operation = new ExtractOperation(parameters, connInfo);
|
||||
await ExecuteOperation(operation, parameters, "Extract dacpac", requestContext);
|
||||
}
|
||||
}
|
||||
@@ -139,8 +137,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
out connInfo);
|
||||
if (connInfo != null)
|
||||
{
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, "Deploy");
|
||||
DeployOperation operation = new DeployOperation(parameters, sqlConn);
|
||||
DeployOperation operation = new DeployOperation(parameters, connInfo);
|
||||
await ExecuteOperation(operation, parameters, "Deploy dacpac", requestContext);
|
||||
}
|
||||
}
|
||||
@@ -150,6 +147,44 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles request to generate deploy script
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleGenerateDeployScriptRequest(GenerateDeployScriptParams parameters, RequestContext<DacFxResult> requestContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.OwnerUri,
|
||||
out connInfo);
|
||||
if (connInfo != null)
|
||||
{
|
||||
GenerateDeployScriptOperation operation = new GenerateDeployScriptOperation(parameters, connInfo);
|
||||
SqlTask sqlTask = null;
|
||||
TaskMetadata metadata = TaskMetadata.Create(parameters, "Generate script", operation, ConnectionServiceInstance);
|
||||
|
||||
// want to show filepath in task history instead of server and database
|
||||
metadata.ServerName = parameters.ScriptFilePath;
|
||||
metadata.DatabaseName = "";
|
||||
|
||||
sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);
|
||||
|
||||
await requestContext.SendResult(new DacFxResult()
|
||||
{
|
||||
OperationId = operation.OperationId,
|
||||
Success = true,
|
||||
ErrorMessage = ""
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExecuteOperation(DacFxOperation operation, DacFxParams parameters, string taskName, RequestContext<DacFxResult> requestContext)
|
||||
{
|
||||
SqlTask sqlTask = null;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -19,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
public DeployParams Parameters { get; }
|
||||
|
||||
public DeployOperation(DeployParams parameters, SqlConnection sqlConnection) : base(sqlConnection)
|
||||
public DeployOperation(DeployParams parameters, ConnectionInfo connInfo) : base(connInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -19,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
public ExportParams Parameters { get; }
|
||||
|
||||
public ExportOperation(ExportParams parameters, SqlConnection sqlConnection): base(sqlConnection)
|
||||
public ExportOperation(ExportParams parameters, ConnectionInfo connInfo) : base(connInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -19,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
public ExtractParams Parameters { get; }
|
||||
|
||||
public ExtractOperation(ExtractParams parameters, SqlConnection sqlConnection): base(sqlConnection)
|
||||
public ExtractOperation(ExtractParams parameters, ConnectionInfo connInfo) : base(connInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to represent an in-progress generate deploy script operation
|
||||
/// </summary>
|
||||
class GenerateDeployScriptOperation : DacFxOperation
|
||||
{
|
||||
public GenerateDeployScriptParams Parameters { get; }
|
||||
|
||||
public GenerateDeployScriptOperation(GenerateDeployScriptParams parameters, ConnectionInfo connInfo) : base(connInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
}
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
DacPackage dacpac = DacPackage.Load(this.Parameters.PackageFilePath);
|
||||
PublishOptions publishOptions = new PublishOptions();
|
||||
publishOptions.GenerateDeploymentReport = this.Parameters.GenerateDeploymentReport;
|
||||
publishOptions.CancelToken = this.CancellationToken;
|
||||
publishOptions.DatabaseScriptPath = this.Parameters.ScriptFilePath;
|
||||
// master script is only used if the target is Azure SQL db and the script contains all operations that must be done against the master database
|
||||
publishOptions.MasterDbScriptPath = Path.Combine(Path.GetDirectoryName(this.Parameters.ScriptFilePath), string.Concat("master_", Path.GetFileName(this.Parameters.ScriptFilePath)));
|
||||
|
||||
PublishResult result = this.DacServices.Script(dacpac, this.Parameters.DatabaseName, publishOptions);
|
||||
|
||||
if(this.Parameters.GenerateDeploymentReport && !string.IsNullOrEmpty(this.Parameters.DeploymentReportFilePath))
|
||||
{
|
||||
File.WriteAllText(this.Parameters.DeploymentReportFilePath, result.DeploymentReport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -19,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
public ImportParams Parameters { get; }
|
||||
|
||||
public ImportOperation(ImportParams parameters, SqlConnection sqlConnection): base(sqlConnection)
|
||||
public ImportOperation(ImportParams parameters, ConnectionInfo connInfo) : base(connInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
|
||||
@@ -42,9 +42,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.bacpac", testdb.DatabaseName))
|
||||
};
|
||||
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(result.ConnectionInfo, "Export");
|
||||
DacFxService service = new DacFxService();
|
||||
ExportOperation operation = new ExportOperation(exportParams, sqlConn);
|
||||
ExportOperation operation = new ExportOperation(exportParams, result.ConnectionInfo);
|
||||
service.PerformOperation(operation);
|
||||
|
||||
// cleanup
|
||||
@@ -71,9 +70,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.bacpac", sourceDb.DatabaseName))
|
||||
};
|
||||
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(result.ConnectionInfo, "Import");
|
||||
DacFxService service = new DacFxService();
|
||||
ExportOperation exportOperation = new ExportOperation(exportParams, sqlConn);
|
||||
ExportOperation exportOperation = new ExportOperation(exportParams, result.ConnectionInfo);
|
||||
service.PerformOperation(exportOperation);
|
||||
|
||||
// import the created bacpac
|
||||
@@ -86,7 +84,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
DatabaseName = string.Concat(sourceDb.DatabaseName, "-imported")
|
||||
};
|
||||
|
||||
ImportOperation importOperation = new ImportOperation(importParams, sqlConn);
|
||||
ImportOperation importOperation = new ImportOperation(importParams, result.ConnectionInfo);
|
||||
service.PerformOperation(importOperation);
|
||||
SqlTestDb targetDb = SqlTestDb.CreateFromExisting(importParams.DatabaseName);
|
||||
|
||||
@@ -116,9 +114,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
ApplicationVersion = new Version(1, 0)
|
||||
};
|
||||
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(result.ConnectionInfo, "Extract");
|
||||
DacFxService service = new DacFxService();
|
||||
ExtractOperation operation = new ExtractOperation(extractParams, sqlConn);
|
||||
ExtractOperation operation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||
service.PerformOperation(operation);
|
||||
|
||||
// cleanup
|
||||
@@ -147,9 +144,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
ApplicationVersion = new Version(1, 0)
|
||||
};
|
||||
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(result.ConnectionInfo, "Deploy");
|
||||
DacFxService service = new DacFxService();
|
||||
ExtractOperation extractOperation = new ExtractOperation(extractParams, sqlConn);
|
||||
ExtractOperation extractOperation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||
service.PerformOperation(extractOperation);
|
||||
|
||||
// deploy the created dacpac
|
||||
@@ -164,8 +160,9 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
UpgradeExisting = false
|
||||
};
|
||||
|
||||
DeployOperation deployOperation = new DeployOperation(deployParams, sqlConn);
|
||||
service.PerformOperation(deployOperation); SqlTestDb targetDb = SqlTestDb.CreateFromExisting(deployParams.DatabaseName);
|
||||
DeployOperation deployOperation = new DeployOperation(deployParams, result.ConnectionInfo);
|
||||
service.PerformOperation(deployOperation);
|
||||
SqlTestDb targetDb = SqlTestDb.CreateFromExisting(deployParams.DatabaseName);
|
||||
|
||||
// cleanup
|
||||
VerifyAndCleanup(extractParams.PackageFilePath);
|
||||
@@ -191,9 +188,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.bacpac", testdb.DatabaseName))
|
||||
};
|
||||
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(result.ConnectionInfo, "Export");
|
||||
DacFxService service = new DacFxService();
|
||||
ExportOperation operation = new ExportOperation(exportParams, sqlConn);
|
||||
ExportOperation operation = new ExportOperation(exportParams, result.ConnectionInfo);
|
||||
|
||||
// set cancellation token to cancel
|
||||
operation.Cancel();
|
||||
@@ -216,6 +212,54 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
return requestContext;
|
||||
}
|
||||
|
||||
private async Task<Mock<RequestContext<DacFxResult>>> SendAndValidateGenerateDeployScriptRequest()
|
||||
{
|
||||
// first extract a dacpac
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var extractRequestContext = new Mock<RequestContext<DacFxResult>>();
|
||||
extractRequestContext.Setup(x => x.SendResult(It.IsAny<DacFxResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, null, "DacFxGenerateScriptTest");
|
||||
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest");
|
||||
Directory.CreateDirectory(folderPath);
|
||||
|
||||
var extractParams = new ExtractParams
|
||||
{
|
||||
DatabaseName = sourceDb.DatabaseName,
|
||||
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.dacpac", sourceDb.DatabaseName)),
|
||||
ApplicationName = "test",
|
||||
ApplicationVersion = new Version(1, 0)
|
||||
};
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
ExtractOperation extractOperation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||
service.PerformOperation(extractOperation);
|
||||
|
||||
// generate script
|
||||
var generateScriptRequestContext = new Mock<RequestContext<DacFxResult>>();
|
||||
generateScriptRequestContext.Setup(x => x.SendResult(It.IsAny<DacFxResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
|
||||
var generateScriptParams = new GenerateDeployScriptParams
|
||||
{
|
||||
PackageFilePath = extractParams.PackageFilePath,
|
||||
DatabaseName = string.Concat(sourceDb.DatabaseName, "-deployed"),
|
||||
ScriptFilePath = Path.Combine(folderPath, string.Concat(sourceDb.DatabaseName, "_", "UpgradeDACScript.sql"))
|
||||
};
|
||||
|
||||
GenerateDeployScriptOperation generateScriptOperation = new GenerateDeployScriptOperation(generateScriptParams, result.ConnectionInfo);
|
||||
service.PerformOperation(generateScriptOperation);
|
||||
SqlTestDb targetDb = SqlTestDb.CreateFromExisting(generateScriptParams.DatabaseName);
|
||||
|
||||
// cleanup
|
||||
VerifyAndCleanup(generateScriptParams.ScriptFilePath);
|
||||
VerifyAndCleanup(extractParams.PackageFilePath);
|
||||
sourceDb.Cleanup();
|
||||
targetDb.Cleanup();
|
||||
|
||||
return generateScriptRequestContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the export bacpac request
|
||||
/// </summary>
|
||||
@@ -261,6 +305,15 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
Assert.NotNull(await SendAndValidateDeployRequest());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the gnerate deploy script request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void GenerateDeployScript()
|
||||
{
|
||||
Assert.NotNull(await SendAndValidateGenerateDeployScriptRequest());
|
||||
}
|
||||
|
||||
private void VerifyAndCleanup(string filePath)
|
||||
{
|
||||
// Verify it was created
|
||||
|
||||
Reference in New Issue
Block a user