mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-27 01:25:42 -05:00
Add schema compare generate script operation (#787)
* add generate script operation
This commit is contained in:
@@ -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