Feature/schemacompare scmp save (#824)

* First cut of scmp Save related changes and some test refactoring

* Adding Exclude/Include objects in saving

* Add diff entry validation as part of test

* Adding PR comments - major change is change to nameparts in place of name hence preserving any "."/"[" in name and avoiding any string operations

* One more UT scenario addition for create excluded object
This commit is contained in:
udeeshagautam
2019-06-13 17:28:59 -07:00
committed by GitHub
parent b451670222
commit 432e054d55
11 changed files with 883 additions and 235 deletions

View File

@@ -88,12 +88,14 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public SchemaUpdateAction UpdateAction { get; set; }
public SchemaDifferenceType DifferenceType { get; set; }
public string Name { get; set; }
public string SourceValue { get; set; }
public string TargetValue { get; set; }
public string[] SourceValue { get; set; }
public string[] TargetValue { get; set; }
public DiffEntry Parent { get; set; }
public List<DiffEntry> Children { get; set; }
public string SourceScript { get; set; }
public string TargetScript { get; set; }
public string SourceObjectType { get; set; }
public string TargetObjectType { get; set; }
}
/// <summary>

View File

@@ -0,0 +1,35 @@
//
// 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.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
{
internal class SchemaCompareSaveScmpParams : SchemaCompareParams
{
/// <summary>
/// Gets or sets the File Path for scmp
/// </summary>
public string ScmpFilePath { get; set; }
/// <summary>
/// Excluded source objects
/// </summary>
public SchemaCompareObjectId[] ExcludedSourceObjects { get; set; }
/// <summary>
/// Excluded Target objects
/// </summary>
public SchemaCompareObjectId[] ExcludedTargetObjects { get; set; }
}
internal class SchemaCompareSaveScmpRequest
{
public static readonly RequestType<SchemaCompareSaveScmpParams, ResultStatus> Type =
RequestType<SchemaCompareSaveScmpParams, ResultStatus>.Create("schemaCompare/saveScmp");
}
}

View File

@@ -9,8 +9,6 @@ using Microsoft.SqlTools.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
@@ -94,7 +92,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
bool result = true;
// Create a diff entry from difference and check if it matches the diff entry passed
DiffEntry entryFromDifference = SchemaCompareOperation.CreateDiffEntry(difference, null);
DiffEntry entryFromDifference = SchemaCompareUtils.CreateDiffEntry(difference, null);
System.Reflection.PropertyInfo[] properties = diffEntry.GetType().GetProperties();
foreach (var prop in properties)

View File

@@ -2,7 +2,6 @@
// 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;
@@ -11,7 +10,6 @@ using Microsoft.SqlTools.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
@@ -45,8 +43,8 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
Validate.IsNotNull("parameters", parameters);
this.Parameters = parameters;
this.SourceConnectionString = GetConnectionString(sourceConnInfo, parameters.SourceEndpointInfo.DatabaseName);
this.TargetConnectionString = GetConnectionString(targetConnInfo, parameters.TargetEndpointInfo.DatabaseName);
this.SourceConnectionString = SchemaCompareUtils.GetConnectionString(sourceConnInfo, parameters.SourceEndpointInfo.DatabaseName);
this.TargetConnectionString = SchemaCompareUtils.GetConnectionString(targetConnInfo, parameters.TargetEndpointInfo.DatabaseName);
this.OperationId = Guid.NewGuid().ToString();
}
@@ -83,14 +81,14 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
try
{
SchemaCompareEndpoint sourceEndpoint = CreateSchemaCompareEndpoint(this.Parameters.SourceEndpointInfo, this.SourceConnectionString);
SchemaCompareEndpoint targetEndpoint = CreateSchemaCompareEndpoint(this.Parameters.TargetEndpointInfo, this.TargetConnectionString);
SchemaCompareEndpoint sourceEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.SourceEndpointInfo, this.SourceConnectionString);
SchemaCompareEndpoint targetEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.TargetEndpointInfo, this.TargetConnectionString);
SchemaComparison comparison = new SchemaComparison(sourceEndpoint, targetEndpoint);
if (this.Parameters.DeploymentOptions != null)
{
comparison.Options = this.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
}
this.ComparisonResult = comparison.Compare();
@@ -104,7 +102,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
this.Differences = new List<DiffEntry>();
foreach (SchemaDifference difference in this.ComparisonResult.Differences)
{
DiffEntry diffEntry = CreateDiffEntry(difference, null);
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null);
this.Differences.Add(diffEntry);
}
}
@@ -115,127 +113,5 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
throw;
}
}
private DacDeployOptions CreateSchemaCompareOptions(DeploymentOptions deploymentOptions)
{
System.Reflection.PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();
DacDeployOptions dacOptions = new DacDeployOptions();
foreach (var deployOptionsProp in deploymentOptionsProperties)
{
var prop = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
if (prop != null)
{
prop.SetValue(dacOptions, deployOptionsProp.GetValue(deploymentOptions));
}
}
return dacOptions;
}
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
{
if (difference == null)
{
return null;
}
DiffEntry diffEntry = new DiffEntry();
diffEntry.UpdateAction = difference.UpdateAction;
diffEntry.DifferenceType = difference.DifferenceType;
diffEntry.Name = difference.Name;
if (difference.SourceObject != null)
{
diffEntry.SourceValue = GetName(difference.SourceObject.Name.ToString());
}
if (difference.TargetObject != null)
{
diffEntry.TargetValue = GetName(difference.TargetObject.Name.ToString());
}
if (difference.DifferenceType == SchemaDifferenceType.Object)
{
// set source and target scripts
if (difference.SourceObject != null)
{
string sourceScript;
difference.SourceObject.TryGetScript(out sourceScript);
diffEntry.SourceScript = FormatScript(sourceScript);
}
if (difference.TargetObject != null)
{
string targetScript;
difference.TargetObject.TryGetScript(out targetScript);
diffEntry.TargetScript = FormatScript(targetScript);
}
}
diffEntry.Children = new List<DiffEntry>();
foreach (SchemaDifference child in difference.Children)
{
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry));
}
return diffEntry;
}
private SchemaCompareEndpoint CreateSchemaCompareEndpoint(SchemaCompareEndpointInfo endpointInfo, string connectionString)
{
switch (endpointInfo.EndpointType)
{
case SchemaCompareEndpointType.Dacpac:
{
return new SchemaCompareDacpacEndpoint(endpointInfo.PackageFilePath);
}
case SchemaCompareEndpointType.Database:
{
return new SchemaCompareDatabaseEndpoint(connectionString);
}
default:
{
return null;
}
}
}
private string GetConnectionString(ConnectionInfo connInfo, string databaseName)
{
if (connInfo == null)
{
return null;
}
connInfo.ConnectionDetails.DatabaseName = databaseName;
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
}
public static string RemoveExcessWhitespace(string script)
{
if (script != null)
{
// remove leading and trailing whitespace
script = script.Trim();
// replace all multiple spaces with single space
script = Regex.Replace(script, " {2,}", " ");
}
return script;
}
public static string FormatScript(string script)
{
script = RemoveExcessWhitespace(script);
if (!string.IsNullOrWhiteSpace(script) && !script.Equals("null"))
{
script += Environment.NewLine + "GO";
}
return script;
}
private static string GetName(string name)
{
// remove brackets from name
return Regex.Replace(name, @"[\[\]]", "");
}
}
}

View File

@@ -0,0 +1,120 @@
//
// 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.SqlServer.Dac.Model;
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.Threading;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
class SchemaCompareSaveScmpOperation : 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; }
protected CancellationToken CancellationToken { get { return this.cancellation.Token; } }
public string ErrorMessage { get; set; }
public SqlTask SqlTask { get; set; }
public SchemaCompareSaveScmpParams Parameters { get; set; }
public string SourceConnectionString { get; set; }
public string TargetConnectionString { get; set; }
public SchemaCompareSaveScmpOperation(SchemaCompareSaveScmpParams parameters, ConnectionInfo sourceConnInfo, ConnectionInfo targetConnInfo)
{
Validate.IsNotNull("parameters", parameters);
Validate.IsNotNull("parameters.ScmpFilePath", parameters.ScmpFilePath);
this.Parameters = parameters;
this.SourceConnectionString = SchemaCompareUtils.GetConnectionString(sourceConnInfo, parameters.SourceEndpointInfo.DatabaseName);
this.TargetConnectionString = SchemaCompareUtils.GetConnectionString(targetConnInfo, parameters.TargetEndpointInfo.DatabaseName);
this.OperationId = Guid.NewGuid().ToString();
}
public void Execute(TaskExecutionMode mode = TaskExecutionMode.Execute)
{
if (this.CancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(this.CancellationToken);
}
try
{
SchemaCompareEndpoint sourceEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.SourceEndpointInfo, this.SourceConnectionString);
SchemaCompareEndpoint targetEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.TargetEndpointInfo, this.TargetConnectionString);
SchemaComparison comparison = new SchemaComparison(sourceEndpoint, targetEndpoint);
if (Parameters.ExcludedSourceObjects != null)
{
foreach (var sourceObj in this.Parameters.ExcludedSourceObjects)
{
SchemaComparisonExcludedObjectId excludedObjId = SchemaCompareUtils.CreateExcludedObject(sourceObj);
if (excludedObjId != null)
{
comparison.ExcludedSourceObjects.Add(excludedObjId);
}
}
}
if (Parameters.ExcludedTargetObjects != null)
{
foreach (var targetObj in this.Parameters.ExcludedTargetObjects)
{
SchemaComparisonExcludedObjectId excludedObjId = SchemaCompareUtils.CreateExcludedObject(targetObj);
if (excludedObjId != null)
{
comparison.ExcludedTargetObjects.Add(excludedObjId);
}
}
}
if (this.Parameters.DeploymentOptions != null)
{
comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
}
comparison.SaveToFile(this.Parameters.ScmpFilePath, true);
}
catch (Exception e)
{
ErrorMessage = e.Message;
Logger.Write(TraceEventType.Error, string.Format("Schema compare save settings operation {0} failed with exception {1}", this.OperationId, e));
throw;
}
}
// The schema compare public api doesn't currently take a cancellation token for scmp save so the operation can't be cancelled
public void Cancel()
{
}
/// <summary>
/// Disposes the operation.
/// </summary>
public void Dispose()
{
if (!disposed)
{
this.Cancel();
disposed = true;
}
}
}
}

View File

@@ -12,6 +12,8 @@ using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.SqlServer.Dac.Compare;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
using System.Diagnostics;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
@@ -49,10 +51,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
serviceHost.SetRequestHandler(SchemaCompareIncludeExcludeNodeRequest.Type, this.HandleSchemaCompareIncludeExcludeNodeRequest);
serviceHost.SetRequestHandler(SchemaCompareGetDefaultOptionsRequest.Type, this.HandleSchemaCompareGetDefaultOptionsRequest);
serviceHost.SetRequestHandler(SchemaCompareOpenScmpRequest.Type, this.HandleSchemaCompareOpenScmpRequest);
serviceHost.SetRequestHandler(SchemaCompareSaveScmpRequest.Type, this.HandleSchemaCompareSaveScmpRequest);
}
/// <summary>
/// Handles schema compare request
/// </summary>
@@ -70,7 +71,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
parameters.TargetEndpointInfo.OwnerUri,
out targetConnInfo);
Task schemaCompareTask = Task.Run(async () =>
CurrentSchemaCompareTask = Task.Run(async () =>
{
SchemaCompareOperation operation = null;
@@ -93,6 +94,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
catch (Exception e)
{
Logger.Write(TraceEventType.Error, "Failed to compare schema. Error: " + e);
await requestContext.SendResult(new SchemaCompareResult()
{
OperationId = operation != null ? operation.OperationId : null,
@@ -137,11 +139,12 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
catch (Exception e)
{
Logger.Write(TraceEventType.Error, "Failed to generate schema compare script. Error: " + e);
await requestContext.SendResult(new ResultStatus()
{
Success = false,
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
});
});
}
}
@@ -173,6 +176,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
catch (Exception e)
{
Logger.Write(TraceEventType.Error, "Failed to publish schema compare changes. Error: " + e);
await requestContext.SendResult(new ResultStatus()
{
Success = false,
@@ -181,6 +185,10 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
}
/// <summary>
/// Handles request for exclude incude node in Schema compare result
/// </summary>
/// <returns></returns>
public async Task HandleSchemaCompareIncludeExcludeNodeRequest(SchemaCompareNodeParams parameters, RequestContext<ResultStatus> requestContext)
{
SchemaCompareIncludeExcludeNodeOperation operation = null;
@@ -199,6 +207,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
catch (Exception e)
{
Logger.Write(TraceEventType.Error, "Failed to select compare schema result node. Error: " + e);
await requestContext.SendResult(new ResultStatus()
{
Success = false,
@@ -207,6 +216,10 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
}
/// <summary>
/// Handles request to create default deployment options as per DacFx
/// </summary>
/// <returns></returns>
public async Task HandleSchemaCompareGetDefaultOptionsRequest(SchemaCompareGetOptionsParams parameters, RequestContext<SchemaCompareOptionsResult> requestContext)
{
try
@@ -267,6 +280,52 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
}
/// <summary>
/// Handles schema compare save SCMP request
/// </summary>
/// <returns></returns>
public async Task HandleSchemaCompareSaveScmpRequest(SchemaCompareSaveScmpParams parameters, RequestContext<ResultStatus> requestContext)
{
try
{
ConnectionInfo sourceConnInfo;
ConnectionInfo targetConnInfo;
ConnectionServiceInstance.TryFindConnection(parameters.SourceEndpointInfo.OwnerUri, out sourceConnInfo);
ConnectionServiceInstance.TryFindConnection(parameters.TargetEndpointInfo.OwnerUri, out targetConnInfo);
CurrentSchemaCompareTask = Task.Run(async () =>
{
SchemaCompareSaveScmpOperation operation = null;
try
{
operation = new SchemaCompareSaveScmpOperation(parameters, sourceConnInfo, targetConnInfo);
operation.Execute(parameters.TaskExecutionMode);
await requestContext.SendResult(new ResultStatus()
{
Success = true,
ErrorMessage = operation.ErrorMessage,
});
}
catch (Exception e)
{
Logger.Write(TraceEventType.Error, "Failed to save scmp file. Error: " + e);
await requestContext.SendResult(new SchemaCompareResult()
{
OperationId = operation != null ? operation.OperationId : null,
Success = false,
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
});
}
});
}
catch (Exception e)
{
await requestContext.SendError(e);
}
}
private SqlTaskManager SqlTaskManagerInstance
{
get

View File

@@ -0,0 +1,163 @@
//
// 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.SqlServer.Dac.Model;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
/// <summary>
/// Internal class for utilities shared between multiple schema compare operations
/// </summary>
internal static class SchemaCompareUtils
{
internal static DacDeployOptions CreateSchemaCompareOptions(DeploymentOptions deploymentOptions)
{
System.Reflection.PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();
DacDeployOptions dacOptions = new DacDeployOptions();
foreach (var deployOptionsProp in deploymentOptionsProperties)
{
var prop = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
if (prop != null)
{
prop.SetValue(dacOptions, deployOptionsProp.GetValue(deploymentOptions));
}
}
return dacOptions;
}
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
{
if (difference == null)
{
return null;
}
DiffEntry diffEntry = new DiffEntry();
diffEntry.UpdateAction = difference.UpdateAction;
diffEntry.DifferenceType = difference.DifferenceType;
diffEntry.Name = difference.Name;
if (difference.SourceObject != null)
{
diffEntry.SourceValue = difference.SourceObject.Name.Parts.ToArray();
var sourceType = new SchemaComparisonExcludedObjectId(difference.SourceObject.ObjectType, difference.SourceObject.Name);
diffEntry.SourceObjectType = sourceType.TypeName;
}
if (difference.TargetObject != null)
{
diffEntry.TargetValue = difference.TargetObject.Name.Parts.ToArray();
var targetType = new SchemaComparisonExcludedObjectId(difference.TargetObject.ObjectType, difference.TargetObject.Name);
diffEntry.TargetObjectType = targetType.TypeName;
}
if (difference.DifferenceType == SchemaDifferenceType.Object)
{
// set source and target scripts
if (difference.SourceObject != null)
{
string sourceScript;
difference.SourceObject.TryGetScript(out sourceScript);
diffEntry.SourceScript = FormatScript(sourceScript);
}
if (difference.TargetObject != null)
{
string targetScript;
difference.TargetObject.TryGetScript(out targetScript);
diffEntry.TargetScript = FormatScript(targetScript);
}
}
diffEntry.Children = new List<DiffEntry>();
foreach (SchemaDifference child in difference.Children)
{
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry));
}
return diffEntry;
}
internal static SchemaComparisonExcludedObjectId CreateExcludedObject(SchemaCompareObjectId sourceObj)
{
try
{
if (sourceObj == null || sourceObj.NameParts == null || string.IsNullOrEmpty(sourceObj.SqlObjectType))
{
return null;
}
ObjectIdentifier id = new ObjectIdentifier(sourceObj.NameParts);
SchemaComparisonExcludedObjectId excludedObjId = new SchemaComparisonExcludedObjectId(sourceObj.SqlObjectType, id);
return excludedObjId;
}
catch (ArgumentException)
{
return null;
}
}
internal static SchemaCompareEndpoint CreateSchemaCompareEndpoint(SchemaCompareEndpointInfo endpointInfo, string connectionString)
{
switch (endpointInfo.EndpointType)
{
case SchemaCompareEndpointType.Dacpac:
{
return new SchemaCompareDacpacEndpoint(endpointInfo.PackageFilePath);
}
case SchemaCompareEndpointType.Database:
{
return new SchemaCompareDatabaseEndpoint(connectionString);
}
default:
{
throw new NotSupportedException($"Endpoint Type {endpointInfo.EndpointType} is not supported");
}
}
}
internal static string GetConnectionString(ConnectionInfo connInfo, string databaseName)
{
if (connInfo == null)
{
return null;
}
connInfo.ConnectionDetails.DatabaseName = databaseName;
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
}
internal static string RemoveExcessWhitespace(string script)
{
if (script != null)
{
// remove leading and trailing whitespace
script = script.Trim();
// replace all multiple spaces with single space
script = Regex.Replace(script, " {2,}", " ");
}
return script;
}
internal static string FormatScript(string script)
{
script = RemoveExcessWhitespace(script);
if (!string.IsNullOrWhiteSpace(script) && !script.Equals("null"))
{
script += Environment.NewLine + "GO";
}
return script;
}
}
}