Fix for schema compare dropping constraints (#2120)

* fix for scripts that are displayed in schema compare drop constraints bug

* update DacFx version

* add more comments
This commit is contained in:
Kim Santiago
2023-06-26 14:01:28 -10:00
committed by GitHub
parent 9034b397ac
commit 95a094438e
5 changed files with 34 additions and 18 deletions

View File

@@ -22,7 +22,7 @@
<PackageReference Update="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version="1.1.1" />
<PackageReference Update="Microsoft.SqlServer.Management.SmoMetadataProvider" Version="170.12.0" />
<PackageReference Update="Microsoft.SqlServer.DacFx" Version="162.0.34-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx" Version="162.1.59-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx.Projects" Version="162.1.37-alpha" />
<PackageReference Update="Microsoft.Azure.Kusto.Data" Version="9.0.4" />
<PackageReference Update="Microsoft.Azure.Kusto.Language" Version="9.0.4" />

View File

@@ -71,21 +71,21 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
if(this.Parameters.IncludeRequest)
{
IEnumerable<SchemaDifference> affectedDependencies = this.ComparisonResult.GetIncludeDependencies(node);
this.AffectedDependencies = affectedDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null)).ToList();
this.AffectedDependencies = affectedDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null, schemaComparisonResult: this.ComparisonResult)).ToList();
}
else
{ // if exclude was successful, the possible affected dependencies are given by GetIncludedDependencies()
if(this.Success)
{
IEnumerable<SchemaDifference> affectedDependencies = this.ComparisonResult.GetIncludeDependencies(node);
this.AffectedDependencies = affectedDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null)).ToList();
this.AffectedDependencies = affectedDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null, schemaComparisonResult: this.ComparisonResult)).ToList();
}
// if not successful, send back the exclude dependencies that caused it to fail
else
{
IEnumerable<SchemaDifference> blockingDependencies = this.ComparisonResult.GetExcludeDependencies(node);
blockingDependencies = blockingDependencies.Where(difference => difference.Included == node.Included);
this.BlockingDependencies = blockingDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null)).ToList();
this.BlockingDependencies = blockingDependencies.Select(difference => SchemaCompareUtils.CreateDiffEntry(difference: difference, parent: null, schemaComparisonResult: this.ComparisonResult)).ToList();
}
}
@@ -122,7 +122,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 = SchemaCompareUtils.CreateDiffEntry(difference, null);
DiffEntry entryFromDifference = SchemaCompareUtils.CreateDiffEntry(difference, null, schemaComparisonResult: this.ComparisonResult);
System.Reflection.PropertyInfo[] properties = diffEntry.GetType().GetProperties();
foreach (var prop in properties)

View File

@@ -121,7 +121,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
foreach (SchemaDifference difference in this.ComparisonResult.Differences)
{
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null);
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null, this.ComparisonResult);
this.Differences.Add(diffEntry);
}
}

View File

@@ -24,7 +24,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
/// </summary>
internal static partial class SchemaCompareUtils
{
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent, SchemaComparisonResult schemaComparisonResult)
{
if (difference == null)
{
@@ -56,23 +56,39 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
// set source and target scripts
if (difference.SourceObject != null)
{
string sourceScript;
difference.SourceObject.TryGetScript(out sourceScript);
string sourceScript = schemaComparisonResult.GetDiffEntrySourceScript(difference);
// Child scripts that do not use alter need to be added if they are being changed, ex: "EXECUTE sp_addextendedproperty...".
// Don't add scripts that start with alter because those are handled by a top level element's create
// ex: if a column changes, then the parent table's script will have the column updated, but GetDiffEntrySourceScript() on the child
// will return an alter table statement for updating that column when getting the child script. The child's alter script is unecessary
// for displaying the script in schema compare because the comparison displays the create scripts
if (!sourceScript.ToLowerInvariant().StartsWith("alter"))
{
diffEntry.SourceScript = FormatScript(sourceScript);
}
}
if (difference.TargetObject != null)
{
string targetScript;
difference.TargetObject.TryGetScript(out targetScript);
string targetScript = schemaComparisonResult.GetDiffEntryTargetScript(difference);
// Child scripts that do not use alter need to be added if they are being changed, ex: "EXECUTE sp_addextendedproperty...".
// Don't add scripts that start with alter because those are handled by a top level element's create
// ex: if a column changes, then the parent table's script will have the column updated, but GetDiffEntrySourceScript() on the child
// will return an alter table script for updating that column when getting the child script. The child's alter script is unecessary
// for displaying the script in schema compare because the comparison displays the create scripts
if (!targetScript.ToLowerInvariant().StartsWith("alter"))
{
diffEntry.TargetScript = FormatScript(targetScript);
}
}
}
diffEntry.Children = new List<DiffEntry>();
foreach (SchemaDifference child in difference.Children)
{
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry));
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry, schemaComparisonResult));
}
return diffEntry;

View File

@@ -1568,7 +1568,7 @@ WITH VALUES
Assert.IsNull(schemaCompareOperation.ErrorMessage);
// try to exclude
DiffEntry t2Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First(), null);
DiffEntry t2Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First(), null, schemaCompareOperation.ComparisonResult);
SchemaCompareNodeParams t2ExcludeParams = new SchemaCompareNodeParams()
{
OperationId = schemaCompareOperation.OperationId,
@@ -1585,7 +1585,7 @@ WITH VALUES
Assert.True(t2ExcludeOperation.BlockingDependencies[0].SourceValue[1] == "v1", "Dependency should be View v1");
// exclude view v1, then t2 should also get excluded by this
DiffEntry v1Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "v1").First(), null);
DiffEntry v1Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "v1").First(), null, schemaCompareOperation.ComparisonResult);
SchemaCompareNodeParams v1ExcludeParams = new SchemaCompareNodeParams()
{
OperationId = schemaCompareOperation.OperationId,
@@ -1712,7 +1712,7 @@ WITH VALUES
}
// create Diff Entry from Difference
DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null);
DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null, schemaCompareOperation.ComparisonResult);
int initial = schemaCompareOperation.ComparisonResult.Differences.Count();
SchemaCompareNodeParams schemaCompareExcludeNodeParams = new SchemaCompareNodeParams()
@@ -1761,7 +1761,7 @@ WITH VALUES
string initialScript = generateScriptOperation.ScriptGenerationResult.Script;
// create Diff Entry from on Difference
DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null);
DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null, schemaCompareOperation.ComparisonResult);
//Validate Diff Entry creation for object type
ValidateDiffEntryCreation(diff, schemaCompareOperation.ComparisonResult.Differences.First());