mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-17 11:01:39 -05:00
Feature/schemacompare Exclude-Include and Options enhancement (#799)
* Initial code for Including/Excluding individual changes (no tests added yet) * Adding Exclude include tests. Default options call and additional options tests. * Taking PR comments * Retry in test for reliability
This commit is contained in:
@@ -2925,6 +2925,30 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
}
|
||||
}
|
||||
|
||||
public static string IncludeNodeTaskName
|
||||
{
|
||||
get
|
||||
{
|
||||
return Keys.GetString(Keys.IncludeNodeTaskName);
|
||||
}
|
||||
}
|
||||
|
||||
public static string ExcludeNodeTaskName
|
||||
{
|
||||
get
|
||||
{
|
||||
return Keys.GetString(Keys.ExcludeNodeTaskName);
|
||||
}
|
||||
}
|
||||
|
||||
public static string SchemaCompareExcludeIncludeNodeNotFound
|
||||
{
|
||||
get
|
||||
{
|
||||
return Keys.GetString(Keys.SchemaCompareExcludeIncludeNodeNotFound);
|
||||
}
|
||||
}
|
||||
|
||||
public static string ConnectionServiceListDbErrorNotConnected(string uri)
|
||||
{
|
||||
return Keys.GetString(Keys.ConnectionServiceListDbErrorNotConnected, uri);
|
||||
@@ -4259,6 +4283,15 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
public const string PublishChangesTaskName = "PublishChangesTaskName";
|
||||
|
||||
|
||||
public const string IncludeNodeTaskName = "IncludeNodeTaskName";
|
||||
|
||||
|
||||
public const string ExcludeNodeTaskName = "ExcludeNodeTaskName";
|
||||
|
||||
|
||||
public const string SchemaCompareExcludeIncludeNodeNotFound = "SchemaCompareExcludeIncludeNodeNotFound";
|
||||
|
||||
|
||||
private Keys()
|
||||
{ }
|
||||
|
||||
|
||||
@@ -1715,4 +1715,16 @@
|
||||
<value>Apply schema compare changes</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="IncludeNodeTaskName" xml:space="preserve">
|
||||
<value>Include schema compare node</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="ExcludeNodeTaskName" xml:space="preserve">
|
||||
<value>Exclude schema compare node</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="ExcludeNodeTaskName" xml:space="preserve">
|
||||
<value>Failed to find the specified change in the model</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
</root>
|
||||
|
||||
@@ -212,7 +212,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
|
||||
foreach (var deployOptionsProp in deploymentOptionsProperties)
|
||||
{
|
||||
var prop = options.GetType().GetProperty(deployOptionsProp.Name);
|
||||
if (prop != null)
|
||||
|
||||
// Note that we set excluded object types here since dacfx has this value as null;
|
||||
if (prop != null && deployOptionsProp.Name != "ExcludeObjectTypes")
|
||||
{
|
||||
deployOptionsProp.SetValue(this, prop.GetValue(options));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// 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 include specific node request
|
||||
/// </summary>
|
||||
public class SchemaCompareNodeParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Operation id of the schema compare operation
|
||||
/// </summary>
|
||||
public string OperationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Difference to Include or exclude
|
||||
/// </summary>
|
||||
public DiffEntry DiffEntry { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicator for include or exclude request
|
||||
/// </summary>
|
||||
public bool IncludeRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution mode for the operation. Default is execution
|
||||
/// </summary>
|
||||
public TaskExecutionMode TaskExecutionMode { get; set; }
|
||||
}
|
||||
|
||||
class SchemaCompareIncludeExcludeNodeRequest
|
||||
{
|
||||
public static readonly RequestType<SchemaCompareNodeParams, ResultStatus> Type =
|
||||
RequestType<SchemaCompareNodeParams, ResultStatus>.Create("schemaCompare/includeExcludeNode");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// 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
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines paramaters for Get default options call
|
||||
/// No parameters required so far
|
||||
/// </summary>
|
||||
public class SchemaCompareGetOptionsParams
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the result of get default options call
|
||||
/// </summary>
|
||||
public class SchemaCompareOptionsResult : ResultStatus
|
||||
{
|
||||
public DeploymentOptions DefaultDeploymentOptions { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the Schema Compare request type
|
||||
/// </summary>
|
||||
class SchemaCompareGetDefaultOptionsRequest
|
||||
{
|
||||
public static readonly RequestType<SchemaCompareGetOptionsParams, SchemaCompareOptionsResult> Type =
|
||||
RequestType<SchemaCompareGetOptionsParams, SchemaCompareOptionsResult>.Create("schemaCompare/getDefaultOptions");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -79,15 +79,15 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
|
||||
|
||||
public class DiffEntry
|
||||
{
|
||||
public SchemaUpdateAction UpdateAction;
|
||||
public SchemaDifferenceType DifferenceType;
|
||||
public string Name;
|
||||
public string SourceValue;
|
||||
public string TargetValue;
|
||||
public DiffEntry Parent;
|
||||
public List<DiffEntry> Children;
|
||||
public string SourceScript;
|
||||
public string TargetScript;
|
||||
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 DiffEntry Parent { get; set; }
|
||||
public List<DiffEntry> Children { get; set; }
|
||||
public string SourceScript { get; set; }
|
||||
public string TargetScript { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -78,5 +78,17 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
public void Cancel()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the operation.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
this.Cancel();
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to represent an in-progress schema compare include/exclude Node operation
|
||||
/// </summary>
|
||||
class SchemaCompareIncludeExcludeNodeOperation : 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 SchemaCompareNodeParams 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 bool Success { get; set; }
|
||||
|
||||
public SchemaCompareIncludeExcludeNodeOperation(SchemaCompareNodeParams parameters, SchemaComparisonResult comparisonResult)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
Validate.IsNotNull("comparisonResult", comparisonResult);
|
||||
this.ComparisonResult = comparisonResult;
|
||||
}
|
||||
|
||||
public void Execute(TaskExecutionMode mode)
|
||||
{
|
||||
this.CancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
SchemaDifference node = this.FindDifference(this.ComparisonResult.Differences, this.Parameters.DiffEntry);
|
||||
if (node == null)
|
||||
{
|
||||
throw new InvalidOperationException(SR.SchemaCompareExcludeIncludeNodeNotFound);
|
||||
}
|
||||
|
||||
this.Success = this.Parameters.IncludeRequest ? this.ComparisonResult.Include(node) : this.ComparisonResult.Exclude(node);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private SchemaDifference FindDifference(IEnumerable<SchemaDifference> differences, DiffEntry diffEntry)
|
||||
{
|
||||
foreach (var difference in differences)
|
||||
{
|
||||
if (IsEqual(difference, diffEntry))
|
||||
{
|
||||
return difference;
|
||||
}
|
||||
else
|
||||
{
|
||||
var childDiff = FindDifference(difference.Children, diffEntry);
|
||||
if (childDiff != null)
|
||||
{
|
||||
return childDiff;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool IsEqual(SchemaDifference difference, DiffEntry diffEntry)
|
||||
{
|
||||
bool result = true;
|
||||
// Create a diff entry from difference and check if it matches the diff entry passed
|
||||
DiffEntry entryFromDifference = SchemaCompareOperation.CreateDiffEntry(difference, null);
|
||||
|
||||
System.Reflection.PropertyInfo[] properties = diffEntry.GetType().GetProperties();
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
result = result &&
|
||||
((prop.GetValue(diffEntry) == null &&
|
||||
prop.GetValue(entryFromDifference) == null) ||
|
||||
prop.GetValue(diffEntry).SafeToString().Equals(prop.GetValue(entryFromDifference).SafeToString()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
|
||||
public void Cancel()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the operation.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
this.Cancel();
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
|
||||
public List<DiffEntry> Differences;
|
||||
|
||||
public DacDeployOptions DefaultOptions;
|
||||
|
||||
public SchemaCompareOperation(SchemaCompareParams parameters, ConnectionInfo sourceConnInfo, ConnectionInfo targetConnInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
@@ -134,8 +132,13 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
return dacOptions;
|
||||
}
|
||||
|
||||
private DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
|
||||
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;
|
||||
@@ -207,13 +210,13 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
|
||||
}
|
||||
|
||||
private string RemoveExcessWhitespace(string script)
|
||||
private static string RemoveExcessWhitespace(string script)
|
||||
{
|
||||
// replace all multiple spaces with single space
|
||||
return Regex.Replace(script, " {2,}", " ");
|
||||
}
|
||||
|
||||
private string GetName(string name)
|
||||
private static string GetName(string name)
|
||||
{
|
||||
// remove brackets from name
|
||||
return Regex.Replace(name, @"[\[\]]", "");
|
||||
|
||||
@@ -19,7 +19,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
class SchemaComparePublishChangesOperation : ITaskOperation
|
||||
{
|
||||
private CancellationTokenSource cancellation = new CancellationTokenSource();
|
||||
private bool disposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique id associated with this instance.
|
||||
|
||||
@@ -44,6 +44,8 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
serviceHost.SetRequestHandler(SchemaCompareRequest.Type, this.HandleSchemaCompareRequest);
|
||||
serviceHost.SetRequestHandler(SchemaCompareGenerateScriptRequest.Type, this.HandleSchemaCompareGenerateScriptRequest);
|
||||
serviceHost.SetRequestHandler(SchemaComparePublishChangesRequest.Type, this.HandleSchemaComparePublishChangesRequest);
|
||||
serviceHost.SetRequestHandler(SchemaCompareIncludeExcludeNodeRequest.Type, this.HandleSchemaCompareIncludeExcludeNodeRequest);
|
||||
serviceHost.SetRequestHandler(SchemaCompareGetDefaultOptionsRequest.Type, this.HandleSchemaCompareGetDefaultOptionsRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -174,6 +176,61 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleSchemaCompareIncludeExcludeNodeRequest(SchemaCompareNodeParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
SchemaCompareIncludeExcludeNodeOperation operation = null;
|
||||
try
|
||||
{
|
||||
SchemaComparisonResult compareResult = schemaCompareResults.Value[parameters.OperationId];
|
||||
operation = new SchemaCompareIncludeExcludeNodeOperation(parameters, compareResult);
|
||||
SqlTask sqlTask = null;
|
||||
TaskMetadata metadata = new TaskMetadata();
|
||||
metadata.TaskOperation = operation;
|
||||
metadata.Name = parameters.IncludeRequest ? SR.IncludeNodeTaskName : SR.ExcludeNodeTaskName;
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleSchemaCompareGetDefaultOptionsRequest(SchemaCompareGetOptionsParams parameters, RequestContext<SchemaCompareOptionsResult> requestContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
// this does not need to be an async operation since this only creates and resturn the default opbject
|
||||
DeploymentOptions options = new DeploymentOptions();
|
||||
|
||||
await requestContext.SendResult(new SchemaCompareOptionsResult()
|
||||
{
|
||||
DefaultDeploymentOptions = options,
|
||||
Success = true,
|
||||
ErrorMessage = null
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new SchemaCompareOptionsResult()
|
||||
{
|
||||
DefaultDeploymentOptions = null,
|
||||
Success = false,
|
||||
ErrorMessage = e.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private SqlTaskManager SqlTaskManagerInstance
|
||||
{
|
||||
get
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
@@ -16,6 +13,7 @@ using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCopmare;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
|
||||
{
|
||||
@@ -111,7 +109,6 @@ END
|
||||
|
||||
private async Task<Mock<RequestContext<SchemaCompareResult>>> SendAndValidateSchemaCompareRequestDacpacToDacpacWithOptions(string sourceScript, string targetScript, DeploymentOptions nodiffOption, DeploymentOptions shouldDiffOption)
|
||||
{
|
||||
|
||||
var result = SchemaCompareTestUtils.GetLiveAutoCompleteTestObjects();
|
||||
var schemaCompareRequestContext = new Mock<RequestContext<SchemaCompareResult>>();
|
||||
schemaCompareRequestContext.Setup(x => x.SendResult(It.IsAny<SchemaCompareResult>())).Returns(Task.FromResult(new object()));
|
||||
@@ -373,10 +370,10 @@ END
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare script generation comparing dacpac and db with and excluding table valued function
|
||||
/// Verify the schema compare default creation test
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ValidateSchemaCompareOptionsDefault()
|
||||
public void ValidateSchemaCompareOptionsDefaultAgainstDacFx()
|
||||
{
|
||||
DeploymentOptions deployOptions = new DeploymentOptions();
|
||||
DacDeployOptions dacOptions = new DacDeployOptions();
|
||||
@@ -384,6 +381,10 @@ END
|
||||
System.Reflection.PropertyInfo[] deploymentOptionsProperties = deployOptions.GetType().GetProperties();
|
||||
System.Reflection.PropertyInfo[] ddProperties = dacOptions.GetType().GetProperties();
|
||||
|
||||
// Note that DatabaseSpecification and sql cmd variables list is not present in Sqltools service - its not settable and is not used by ADS options.
|
||||
// TODO : update this test if the above options are added later
|
||||
Assert.True(deploymentOptionsProperties.Length == ddProperties.Length - 2 , $"Number of properties is not same Deployment options : {deploymentOptionsProperties.Length} DacFx options : {ddProperties.Length}");
|
||||
|
||||
foreach (var deployOptionsProp in deploymentOptionsProperties)
|
||||
{
|
||||
var dacProp = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
|
||||
@@ -392,9 +393,48 @@ END
|
||||
var deployOptionsValue = deployOptionsProp.GetValue(deployOptions);
|
||||
var dacValue = dacProp.GetValue(dacOptions);
|
||||
|
||||
if (deployOptionsProp.Name != "ExcludeObjectTypes") // do not compare for ExcludeObjectTypes because it will be different
|
||||
{
|
||||
Assert.True((deployOptionsValue == null && dacValue == null) || deployOptionsValue.Equals(dacValue), $"DacFx DacDeploy property not equal to Tools Service DeploymentOptions for { deployOptionsProp.Name}, SchemaCompareOptions value: {deployOptionsValue} and DacDeployOptions value: {dacValue} ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare default creation test
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ValidateSchemaCompareGetDefaultOptionsCallFromService()
|
||||
{
|
||||
DeploymentOptions deployOptions = new DeploymentOptions();
|
||||
var schemaCompareRequestContext = new Mock<RequestContext<SchemaCompareOptionsResult>>();
|
||||
schemaCompareRequestContext.Setup(x => x.SendResult(It.IsAny<SchemaCompareOptionsResult>())).Returns(Task.FromResult(new object()));
|
||||
schemaCompareRequestContext.Setup((RequestContext<SchemaCompareOptionsResult> x) => x.SendResult(It.Is<SchemaCompareOptionsResult>((options) => this.OptionsEqualsDefault(options) == true))).Returns(Task.FromResult(new object()));
|
||||
|
||||
SchemaCompareGetOptionsParams p = new SchemaCompareGetOptionsParams();
|
||||
await SchemaCompareService.Instance.HandleSchemaCompareGetDefaultOptionsRequest(p, schemaCompareRequestContext.Object);
|
||||
}
|
||||
|
||||
private bool OptionsEqualsDefault(SchemaCompareOptionsResult options)
|
||||
{
|
||||
DeploymentOptions defaultOpt = new DeploymentOptions();
|
||||
DeploymentOptions actualOpt = options.DefaultDeploymentOptions;
|
||||
|
||||
System.Reflection.PropertyInfo[] deploymentOptionsProperties = defaultOpt.GetType().GetProperties();
|
||||
foreach(var v in deploymentOptionsProperties)
|
||||
{
|
||||
var defaultP = v.GetValue(defaultOpt);
|
||||
var actualP = v.GetValue(actualOpt);
|
||||
if (v.Name == "ExcludeObjectTypes")
|
||||
{
|
||||
Assert.True((defaultP as ObjectType[]).Length == (actualP as ObjectType[]).Length, $"Number of excluded objects is different; expected: {(defaultP as ObjectType[]).Length} actual: {(actualP as ObjectType[]).Length}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.True((defaultP == null && actualP == null) || defaultP.Equals(actualP), $"Actual Property from Service is not equal to default property for { v.Name}, Actual value: {actualP} and Default value: {defaultP}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
@@ -13,6 +10,7 @@ using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
@@ -73,11 +71,7 @@ CREATE TABLE [dbo].[table3]
|
||||
};
|
||||
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, null, null);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
ValidateSchemaCompareWithExcludeIncludeResults(schemaCompareOperation);
|
||||
|
||||
// cleanup
|
||||
SchemaCompareTestUtils.VerifyAndCleanup(sourceDacpacFilePath);
|
||||
@@ -120,11 +114,7 @@ CREATE TABLE [dbo].[table3]
|
||||
};
|
||||
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, result.ConnectionInfo);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
ValidateSchemaCompareWithExcludeIncludeResults(schemaCompareOperation);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -163,11 +153,7 @@ CREATE TABLE [dbo].[table3]
|
||||
};
|
||||
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, null);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
ValidateSchemaCompareWithExcludeIncludeResults(schemaCompareOperation);
|
||||
|
||||
// cleanup
|
||||
SchemaCompareTestUtils.VerifyAndCleanup(targetDacpacFilePath);
|
||||
@@ -208,13 +194,8 @@ CREATE TABLE [dbo].[table3]
|
||||
};
|
||||
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, result.ConnectionInfo);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
// generate script
|
||||
// generate script params
|
||||
var generateScriptParams = new SchemaCompareGenerateScriptParams
|
||||
{
|
||||
TargetDatabaseName = targetDb.DatabaseName,
|
||||
@@ -222,8 +203,7 @@ CREATE TABLE [dbo].[table3]
|
||||
ScriptFilePath = Path.Combine(folderPath, string.Concat(sourceDb.DatabaseName, "_", "Update.publish.sql"))
|
||||
};
|
||||
|
||||
SchemaCompareGenerateScriptOperation generateScriptOperation = new SchemaCompareGenerateScriptOperation(generateScriptParams, schemaCompareOperation.ComparisonResult);
|
||||
generateScriptOperation.Execute(TaskExecutionMode.Execute);
|
||||
ValidateSchemaCompareScriptGenerationWithExcludeIncludeResults(schemaCompareOperation, generateScriptParams);
|
||||
|
||||
// cleanup
|
||||
SchemaCompareTestUtils.VerifyAndCleanup(generateScriptParams.ScriptFilePath);
|
||||
@@ -266,11 +246,6 @@ CREATE TABLE [dbo].[table3]
|
||||
};
|
||||
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, result.ConnectionInfo);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
// generate script
|
||||
var generateScriptParams = new SchemaCompareGenerateScriptParams
|
||||
@@ -280,8 +255,7 @@ CREATE TABLE [dbo].[table3]
|
||||
ScriptFilePath = Path.Combine(folderPath, string.Concat(sourceDb.DatabaseName, "_", "Update.publish.sql"))
|
||||
};
|
||||
|
||||
SchemaCompareGenerateScriptOperation generateScriptOperation = new SchemaCompareGenerateScriptOperation(generateScriptParams, schemaCompareOperation.ComparisonResult);
|
||||
generateScriptOperation.Execute(TaskExecutionMode.Execute);
|
||||
ValidateSchemaCompareScriptGenerationWithExcludeIncludeResults(schemaCompareOperation, generateScriptParams);
|
||||
|
||||
// cleanup
|
||||
SchemaCompareTestUtils.VerifyAndCleanup(generateScriptParams.ScriptFilePath);
|
||||
@@ -430,6 +404,107 @@ CREATE TABLE [dbo].[table3]
|
||||
return schemaCompareRequestContext;
|
||||
}
|
||||
|
||||
private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation)
|
||||
{
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
// create Diff Entry from Difference
|
||||
|
||||
DiffEntry diff = SchemaCompareOperation.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null);
|
||||
|
||||
int initial = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
SchemaCompareNodeParams schemaCompareExcludeNodeParams = new SchemaCompareNodeParams()
|
||||
{
|
||||
OperationId = schemaCompareOperation.OperationId,
|
||||
DiffEntry = diff,
|
||||
IncludeRequest = false,
|
||||
TaskExecutionMode = TaskExecutionMode.Execute
|
||||
};
|
||||
SchemaCompareIncludeExcludeNodeOperation nodeExcludeOperation = new SchemaCompareIncludeExcludeNodeOperation(schemaCompareExcludeNodeParams, schemaCompareOperation.ComparisonResult);
|
||||
nodeExcludeOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
int afterExclude = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
|
||||
Assert.True(initial == afterExclude, $"Changes should be same again after excluding/including, before {initial}, now {afterExclude}");
|
||||
|
||||
SchemaCompareNodeParams schemaCompareincludeNodeParams = new SchemaCompareNodeParams()
|
||||
{
|
||||
OperationId = schemaCompareOperation.OperationId,
|
||||
DiffEntry = diff,
|
||||
IncludeRequest = true,
|
||||
TaskExecutionMode = TaskExecutionMode.Execute
|
||||
};
|
||||
|
||||
SchemaCompareIncludeExcludeNodeOperation nodeIncludeOperation = new SchemaCompareIncludeExcludeNodeOperation(schemaCompareincludeNodeParams, schemaCompareOperation.ComparisonResult);
|
||||
nodeIncludeOperation.Execute(TaskExecutionMode.Execute);
|
||||
int afterInclude = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
|
||||
|
||||
Assert.True(initial == afterInclude, $"Changes should be same again after excluding/including, before:{initial}, now {afterInclude}");
|
||||
}
|
||||
|
||||
private void ValidateSchemaCompareScriptGenerationWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation, SchemaCompareGenerateScriptParams generateScriptParams)
|
||||
{
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
SchemaCompareGenerateScriptOperation generateScriptOperation = new SchemaCompareGenerateScriptOperation(generateScriptParams, schemaCompareOperation.ComparisonResult);
|
||||
generateScriptOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
string initialScript = File.ReadAllText(generateScriptParams.ScriptFilePath);
|
||||
|
||||
// create Diff Entry from on Difference
|
||||
DiffEntry diff = SchemaCompareOperation.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null);
|
||||
|
||||
int initial = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
SchemaCompareNodeParams schemaCompareExcludeNodeParams = new SchemaCompareNodeParams()
|
||||
{
|
||||
OperationId = schemaCompareOperation.OperationId,
|
||||
DiffEntry = diff,
|
||||
IncludeRequest = false,
|
||||
TaskExecutionMode = TaskExecutionMode.Execute
|
||||
};
|
||||
SchemaCompareIncludeExcludeNodeOperation nodeExcludeOperation = new SchemaCompareIncludeExcludeNodeOperation(schemaCompareExcludeNodeParams, schemaCompareOperation.ComparisonResult);
|
||||
nodeExcludeOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
int afterExclude = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
|
||||
Assert.True(initial == afterExclude, $"Changes should be same again after excluding/including, before {initial}, now {afterExclude}");
|
||||
|
||||
generateScriptOperation = new SchemaCompareGenerateScriptOperation(generateScriptParams, schemaCompareOperation.ComparisonResult);
|
||||
generateScriptOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
string afterExcludeScript = File.ReadAllText(generateScriptParams.ScriptFilePath);
|
||||
Assert.True(initialScript.Length > afterExcludeScript.Length, $"Script should be affected (less statements) exclude operation, before {initialScript}, now {afterExcludeScript}");
|
||||
|
||||
SchemaCompareNodeParams schemaCompareincludeNodeParams = new SchemaCompareNodeParams()
|
||||
{
|
||||
OperationId = schemaCompareOperation.OperationId,
|
||||
DiffEntry = diff,
|
||||
IncludeRequest = true,
|
||||
TaskExecutionMode = TaskExecutionMode.Execute
|
||||
};
|
||||
|
||||
SchemaCompareIncludeExcludeNodeOperation nodeIncludeOperation = new SchemaCompareIncludeExcludeNodeOperation(schemaCompareincludeNodeParams, schemaCompareOperation.ComparisonResult);
|
||||
nodeIncludeOperation.Execute(TaskExecutionMode.Execute);
|
||||
int afterInclude = schemaCompareOperation.ComparisonResult.Differences.Count();
|
||||
|
||||
Assert.True(initial == afterInclude, $"Changes should be same again after excluding/including:{initial}, now {afterInclude}");
|
||||
|
||||
generateScriptOperation = new SchemaCompareGenerateScriptOperation(generateScriptParams, schemaCompareOperation.ComparisonResult);
|
||||
generateScriptOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
string afterIncludeScript = File.ReadAllText(generateScriptParams.ScriptFilePath);
|
||||
Assert.True(initialScript.Length == afterIncludeScript.Length, $"Changes should be same as inital since we included what we excluded, before {initialScript}, now {afterIncludeScript}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare request comparing two dacpacs
|
||||
/// </summary>
|
||||
|
||||
@@ -10,6 +10,7 @@ using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.IO;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
|
||||
{
|
||||
@@ -50,7 +51,21 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
|
||||
|
||||
internal static LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
{
|
||||
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||
// Adding retry for reliability - otherwise it caused test to fail in lab
|
||||
TestConnectionResult result = null;
|
||||
int retry = 3;
|
||||
|
||||
while (retry > 0)
|
||||
{
|
||||
result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||
if (result != null && result.ConnectionInfo != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
retry--;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user