Add DacFx Generate Deploy Script operation (#759)

* Adding DacFx Generate Deploy Script operation
This commit is contained in:
kisantia
2019-01-25 11:34:28 -08:00
committed by GitHub
parent adc13cff82
commit 1917100bfb
9 changed files with 211 additions and 30 deletions

View File

@@ -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");
}
}

View File

@@ -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)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
}
}
}

View File

@@ -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;