mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-28 17:24:27 -05:00
Add schema compare generate script operation (#787)
* add generate script operation
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// 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.Hosting.Protocol.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters for a schema compare generate script request.
|
||||
/// </summary>
|
||||
public class SchemaCompareGenerateScriptParams
|
||||
{
|
||||
/// <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>
|
||||
/// Defines the Schema Compare generate script request type
|
||||
/// </summary>
|
||||
class SchemaCompareGenerateScriptRequest
|
||||
{
|
||||
public static readonly RequestType<SchemaCompareGenerateScriptParams, ResultStatus> Type =
|
||||
RequestType<SchemaCompareGenerateScriptParams, ResultStatus>.Create("schemaCompare/generateScript");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// 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.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
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 generate script operation
|
||||
/// </summary>
|
||||
class SchemaCompareGenerateScriptOperation : 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 SchemaCompareGenerateScriptParams 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 SchemaCompareGenerateScriptOperation(SchemaCompareGenerateScriptParams parameters, SchemaComparisonResult comparisonResult)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
Validate.IsNotNull("scriptFilePath", parameters.ScriptFilePath);
|
||||
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
|
||||
{
|
||||
SchemaCompareScriptGenerationResult result = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName);
|
||||
File.WriteAllText(this.Parameters.ScriptFilePath, result.Script);
|
||||
|
||||
if (!string.IsNullOrEmpty(result.MasterScript))
|
||||
{
|
||||
// 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
|
||||
string masterScriptPath = Path.Combine(Path.GetDirectoryName(this.Parameters.ScriptFilePath), string.Concat("master_", Path.GetFileName(this.Parameters.ScriptFilePath)));
|
||||
File.WriteAllText(masterScriptPath, result.MasterScript);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorMessage = e.Message;
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare generate script 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,16 +56,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cancel operation
|
||||
/// </summary>
|
||||
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
|
||||
public void Cancel()
|
||||
{
|
||||
if (!this.cancellation.IsCancellationRequested)
|
||||
{
|
||||
Logger.Write(TraceEventType.Verbose, string.Format("Cancel invoked for OperationId {0}", this.OperationId));
|
||||
this.cancellation.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,7 +103,8 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare operation {0} failed with exception {1}", this.OperationId, e));
|
||||
ErrorMessage = e.Message;
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare operation {0} failed with exception {1}", this.OperationId, e.Message));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,10 @@ using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
{
|
||||
@@ -21,7 +22,10 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
class SchemaCompareService
|
||||
{
|
||||
private static ConnectionService connectionService = null;
|
||||
private SqlTaskManager sqlTaskManagerInstance = null;
|
||||
private static readonly Lazy<SchemaCompareService> instance = new Lazy<SchemaCompareService>(() => new SchemaCompareService());
|
||||
private Lazy<ConcurrentDictionary<string, SchemaComparisonResult>> schemaCompareResults =
|
||||
new Lazy<ConcurrentDictionary<string, SchemaComparisonResult>>(() => new ConcurrentDictionary<string, SchemaComparisonResult>());
|
||||
|
||||
/// <summary>
|
||||
/// Gets the singleton instance object
|
||||
@@ -38,6 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
serviceHost.SetRequestHandler(SchemaCompareRequest.Type, this.HandleSchemaCompareRequest);
|
||||
serviceHost.SetRequestHandler(SchemaCompareGenerateScriptRequest.Type, this.HandleSchemaCompareGenerateScriptRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -59,11 +64,16 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
|
||||
Task schemaCompareTask = Task.Run(async () =>
|
||||
{
|
||||
SchemaCompareOperation operation = null;
|
||||
|
||||
try
|
||||
{
|
||||
SchemaCompareOperation operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
// add result to dictionary of results
|
||||
schemaCompareResults.Value[operation.OperationId] = operation.ComparisonResult;
|
||||
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation.OperationId,
|
||||
@@ -73,9 +83,14 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
Differences = operation.Differences
|
||||
});
|
||||
}
|
||||
catch(Exception e)
|
||||
catch
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -85,6 +100,59 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles request for schema compare generate deploy script
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareGenerateScriptRequest(SchemaCompareGenerateScriptParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
SchemaCompareGenerateScriptOperation operation = null;
|
||||
try
|
||||
{
|
||||
SchemaComparisonResult compareResult = schemaCompareResults.Value[parameters.OperationId];
|
||||
operation = new SchemaCompareGenerateScriptOperation(parameters, compareResult);
|
||||
SqlTask sqlTask = null;
|
||||
TaskMetadata metadata = new TaskMetadata();
|
||||
metadata.TaskOperation = operation;
|
||||
// want to show filepath in task history instead of server and database
|
||||
metadata.ServerName = parameters.ScriptFilePath;
|
||||
metadata.DatabaseName = string.Empty;
|
||||
metadata.Name = "Generate Script";
|
||||
|
||||
sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = operation.ErrorMessage
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = operation.ErrorMessage
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private SqlTaskManager SqlTaskManagerInstance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sqlTaskManagerInstance == null)
|
||||
{
|
||||
sqlTaskManagerInstance = SqlTaskManager.Instance;
|
||||
}
|
||||
return sqlTaskManagerInstance;
|
||||
}
|
||||
set
|
||||
{
|
||||
sqlTaskManagerInstance = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal static ConnectionService ConnectionServiceInstance
|
||||
{
|
||||
get
|
||||
|
||||
Reference in New Issue
Block a user