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:
udeeshagautam
2019-04-25 22:07:00 -07:00
committed by GitHub
parent 85f34b65f1
commit f8cd355e61
14 changed files with 545 additions and 89 deletions

View File

@@ -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);
@@ -4257,6 +4281,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()

View File

@@ -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>

View File

@@ -41,7 +41,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool IgnoreLockHintsOnIndexes { get; set; }
public bool IgnoreKeywordCasing { get; set; }
public bool IgnoreIndexPadding { get; set; }
public bool IgnoreIndexOptions { get; set; }
@@ -51,21 +51,21 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool IgnoreIdentitySeed { get; set; }
public bool IgnoreUserSettingsObjects { get; set; }
public bool IgnoreFullTextCatalogFilePath { get; set; }
public bool IgnoreWhitespace { get; set; }
public bool IgnoreWithNocheckOnForeignKeys { get; set; }
public bool VerifyCollationCompatibility { get; set; }
public bool UnmodifiableObjectWarnings { get; set; }
public bool TreatVerificationErrorsAsWarnings { get; set; }
public bool ScriptRefreshModule { get; set; }
public bool ScriptNewConstraintValidation { get; set; }
public bool ScriptFileSize { get; set; }
@@ -81,7 +81,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool RunDeploymentPlanExecutors { get; set; }
public bool RegisterDataTierApplication { get; set; }
public bool PopulateFilesOnFileGroups { get; set; }
public bool NoAlterStatementsToChangeClrTypes { get; set; }
@@ -93,17 +93,17 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool AllowUnsafeRowLevelSecurityDataMovement { get; set; }
public bool IgnoreWithNocheckOnCheckConstraints { get; set; }
public bool IgnoreFillFactor { get; set; }
public bool IgnoreFileSize { get; set; }
public bool IgnoreFilegroupPlacement { get; set; }
public bool DoNotAlterReplicatedObjects { get; set; }
public bool DoNotAlterChangeDataCaptureObjects { get; set; }
public bool DisableAndReenableDdlTriggers { get; set; }
public bool DeployDatabaseInSingleUserMode { get; set; }
@@ -113,11 +113,11 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool CompareUsingTargetCollation { get; set; }
public bool CommentOutSetVarDeclarations { get; set; }
public int CommandTimeout { get; set; } = 120;
public bool BlockWhenDriftDetected { get; set; }
public bool BlockOnPossibleDataLoss { get; set; }
public bool BackupDatabaseBeforeChanges { get; set; }
@@ -129,15 +129,15 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public string AdditionalDeploymentContributorArguments { get; set; }
public string AdditionalDeploymentContributors { get; set; }
public bool DropConstraintsNotInSource { get; set; }
public bool DropDmlTriggersNotInSource { get; set; }
public bool DropExtendedPropertiesNotInSource { get; set; }
public bool DropIndexesNotInSource { get; set; }
public bool IgnoreFileAndLogFilePath { get; set; }
public bool IgnoreExtendedProperties { get; set; }
@@ -151,9 +151,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool IgnoreDdlTriggerState { get; set; }
public bool IgnoreDdlTriggerOrder { get; set; }
public bool IgnoreCryptographicProviderFilePath { get; set; }
public bool VerifyDeployment { get; set; }
public bool IgnoreComments { get; set; }
@@ -161,17 +161,17 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
public bool IgnoreColumnCollation { get; set; }
public bool IgnoreAuthorizer { get; set; }
public bool IgnoreAnsiNulls { get; set; }
public bool GenerateSmartDefaults { get; set; }
public bool DropStatisticsNotInSource { get; set; }
public bool DropRoleMembersNotInSource { get; set; }
public bool DropPermissionsNotInSource { get; set; }
public bool DropObjectsNotInSource { get; set; }
public bool IgnoreColumnOrder { get; set; }
@@ -208,11 +208,13 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
{
DacDeployOptions options = new DacDeployOptions();
System.Reflection.PropertyInfo[] deploymentOptionsProperties = this.GetType().GetProperties();
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));
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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>

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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, @"[\[\]]", "");

View File

@@ -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.

View File

@@ -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