mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-16 17:23:38 -05:00
Add schema compare publish changes operation (#795)
* add schema compare publish changes operation
This commit is contained in:
@@ -13,27 +13,12 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
|
||||
/// <summary>
|
||||
/// Parameters for a schema compare generate script request.
|
||||
/// </summary>
|
||||
public class SchemaCompareGenerateScriptParams
|
||||
public class SchemaCompareGenerateScriptParams : SchemaComparePublishChangesParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Operation id of the schema compare operation
|
||||
/// </summary>
|
||||
public string OperationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of target database
|
||||
/// </summary>
|
||||
public string TargetDatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the filepath where to save the generated script
|
||||
/// </summary>
|
||||
public string ScriptFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution mode for the operation. Default is execution
|
||||
/// </summary>
|
||||
public TaskExecutionMode TaskExecutionMode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// 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.SchemaCompare.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters for a schema compare publish changes request.
|
||||
/// </summary>
|
||||
public class SchemaComparePublishChangesParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Operation id of the schema compare operation
|
||||
/// </summary>
|
||||
public string OperationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of target server
|
||||
/// </summary>
|
||||
public string TargetServerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of target database
|
||||
/// </summary>
|
||||
public string TargetDatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution mode for the operation. Default is execution
|
||||
/// </summary>
|
||||
public TaskExecutionMode TaskExecutionMode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the Schema Compare publish changes request type
|
||||
/// </summary>
|
||||
class SchemaComparePublishChangesRequest
|
||||
{
|
||||
public static readonly RequestType<SchemaComparePublishChangesParams, ResultStatus> Type =
|
||||
RequestType<SchemaComparePublishChangesParams, ResultStatus>.Create("schemaCompare/publish");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// 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.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to represent an in-progress schema compare publish changes operation
|
||||
/// </summary>
|
||||
class SchemaComparePublishChangesOperation : ITaskOperation
|
||||
{
|
||||
private CancellationTokenSource cancellation = new CancellationTokenSource();
|
||||
private bool disposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique id associated with this instance.
|
||||
/// </summary>
|
||||
public string OperationId { get; private set; }
|
||||
|
||||
public SchemaComparePublishChangesParams Parameters { get; }
|
||||
|
||||
protected CancellationToken CancellationToken { get { return this.cancellation.Token; } }
|
||||
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public SqlTask SqlTask { get; set; }
|
||||
|
||||
public SchemaComparisonResult ComparisonResult { get; set; }
|
||||
|
||||
public SchemaComparePublishResult PublishResult { get; set; }
|
||||
|
||||
public SchemaComparePublishChangesOperation(SchemaComparePublishChangesParams parameters, SchemaComparisonResult comparisonResult)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
Validate.IsNotNull("comparisonResult", comparisonResult);
|
||||
this.ComparisonResult = comparisonResult;
|
||||
}
|
||||
|
||||
public void Execute(TaskExecutionMode mode)
|
||||
{
|
||||
if (this.CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw new OperationCanceledException(this.CancellationToken);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this.PublishResult = this.ComparisonResult.PublishChangesToTarget();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorMessage = e.Message;
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare publish changes operation {0} failed with exception {1}", this.OperationId, e.Message));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
|
||||
public void Cancel()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
{
|
||||
serviceHost.SetRequestHandler(SchemaCompareRequest.Type, this.HandleSchemaCompareRequest);
|
||||
serviceHost.SetRequestHandler(SchemaCompareGenerateScriptRequest.Type, this.HandleSchemaCompareGenerateScriptRequest);
|
||||
serviceHost.SetRequestHandler(SchemaComparePublishChangesRequest.Type, this.HandleSchemaComparePublishChangesRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -83,13 +84,13 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
Differences = operation.Differences
|
||||
});
|
||||
}
|
||||
catch
|
||||
catch(Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -117,7 +118,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
// want to show filepath in task history instead of server and database
|
||||
metadata.ServerName = parameters.ScriptFilePath;
|
||||
metadata.DatabaseName = string.Empty;
|
||||
metadata.Name = "Generate Script";
|
||||
metadata.Name = SR.GenerateScriptTaskName;
|
||||
|
||||
sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);
|
||||
|
||||
@@ -127,14 +128,50 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
ErrorMessage = operation.ErrorMessage
|
||||
});
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles request for schema compare publish changes script
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaComparePublishChangesRequest(SchemaComparePublishChangesParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
SchemaComparePublishChangesOperation operation = null;
|
||||
try
|
||||
{
|
||||
SchemaComparisonResult compareResult = schemaCompareResults.Value[parameters.OperationId];
|
||||
operation = new SchemaComparePublishChangesOperation(parameters, compareResult);
|
||||
SqlTask sqlTask = null;
|
||||
TaskMetadata metadata = new TaskMetadata();
|
||||
metadata.TaskOperation = operation;
|
||||
metadata.ServerName = parameters.TargetServerName;
|
||||
metadata.DatabaseName = parameters.TargetDatabaseName;
|
||||
metadata.Name = SR.PublishChangesTaskName;
|
||||
|
||||
sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = operation.ErrorMessage
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private SqlTaskManager SqlTaskManagerInstance
|
||||
|
||||
Reference in New Issue
Block a user