fix schema comparing empty project showing more differences than expected (#1727)

This commit is contained in:
Kim Santiago
2022-10-21 14:04:31 -07:00
committed by GitHub
parent 99dbbd5036
commit 6f198e9e5a
5 changed files with 85 additions and 4 deletions

View File

@@ -113,6 +113,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
this.Differences = new List<DiffEntry>(); this.Differences = new List<DiffEntry>();
if (this.ComparisonResult.Differences != null) if (this.ComparisonResult.Differences != null)
{ {
// filter out not included and not excludeable differences
(this.ComparisonResult.Differences as List<SchemaDifference>).RemoveAll(d => !d.Included && !d.IsExcludable);
foreach (SchemaDifference difference in this.ComparisonResult.Differences) foreach (SchemaDifference difference in this.ComparisonResult.Differences)
{ {
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null); DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null);

View File

@@ -53,4 +53,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="DacFx\Dacpacs\" /> <Folder Include="DacFx\Dacpacs\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="SchemaCompare\SqlProjects\emptyTemplate.sqlproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -281,6 +281,41 @@ WITH VALUES
} }
} }
/// <summary>
/// Verify the schema compare request comparing empty project to a database
/// </summary>
[Test]
public async Task SchemaCompareEmptyProjectToDatabase()
{
TestConnectionResult result = SchemaCompareTestUtils.GetLiveAutoCompleteTestObjects();
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareSource");
try
{
string targetProjectPath = SchemaCompareTestUtils.CreateSqlProj("TargetProject");
string[] targetScripts = new string[0];
SchemaCompareEndpointInfo sourceInfo = CreateTestEndpoint(SchemaCompareEndpointType.Database, sourceDb.DatabaseName);
SchemaCompareEndpointInfo targetInfo = CreateTestEndpoint(SchemaCompareEndpointType.Project, targetProjectPath, targetScripts);
var schemaCompareParams = new SchemaCompareParams
{
SourceEndpointInfo = sourceInfo,
TargetEndpointInfo = targetInfo
};
SchemaCompareOperation schemaCompareOperation = new(schemaCompareParams, result.ConnectionInfo, null);
ValidateSchemaCompareWithExcludeIncludeResults(schemaCompareOperation, expectedDifferencesCount: 2);
// cleanup
SchemaCompareTestUtils.VerifyAndCleanup(targetProjectPath);
}
finally
{
sourceDb.Cleanup();
}
}
/// <summary> /// <summary>
/// Verify the schema compare request comparing a dacpac and a project /// Verify the schema compare request comparing a dacpac and a project
/// </summary> /// </summary>
@@ -1554,7 +1589,7 @@ WITH VALUES
} }
} }
private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation) private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation, int? expectedDifferencesCount = null)
{ {
schemaCompareOperation.Execute(TaskExecutionMode.Execute); schemaCompareOperation.Execute(TaskExecutionMode.Execute);
@@ -1563,6 +1598,11 @@ WITH VALUES
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences); Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
Assert.IsNull(schemaCompareOperation.ErrorMessage); Assert.IsNull(schemaCompareOperation.ErrorMessage);
if (expectedDifferencesCount != null)
{
Assert.That(expectedDifferencesCount, Is.EqualTo(schemaCompareOperation.ComparisonResult.Differences.Count()), "The actual number of differences did not match the expected number");
}
// create Diff Entry from Difference // create Diff Entry from Difference
DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null); DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null);

View File

@@ -22,6 +22,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
{ {
internal static class SchemaCompareTestUtils internal static class SchemaCompareTestUtils
{ {
private static string sqlProjectsFolder = Path.Combine("..", "..", "..", "SchemaCompare", "SqlProjects");
internal static void VerifyAndCleanup(string path) internal static void VerifyAndCleanup(string path)
{ {
// verify it was created... // verify it was created...
@@ -61,12 +63,17 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
return extractParams.PackageFilePath; return extractParams.PackageFilePath;
} }
/// <summary>
/// Creates an SDK-style .sqlproj from the database
/// </summary>
/// <param name="testdb">Database to create the sql project from</param>
/// <param name="projectName">Name of the project</param>
/// <returns>Full path to the project folder</returns>
internal static string CreateProject(SqlTestDb testdb, string projectName) internal static string CreateProject(SqlTestDb testdb, string projectName)
{ {
var result = GetLiveAutoCompleteTestObjects(); var result = GetLiveAutoCompleteTestObjects();
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchemaCompareTest", projectName); string sqlprojFilePath = CreateSqlProj(projectName);
Directory.CreateDirectory(folderPath); string folderPath = Path.GetDirectoryName(sqlprojFilePath);
File.Create(Path.Combine(folderPath, projectName + ".sqlproj")).Close();
var extractParams = new ExtractParams var extractParams = new ExtractParams
{ {
@@ -84,6 +91,21 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
return folderPath; return folderPath;
} }
/// <summary>
/// Creates an empty SDK-style .sqlproj
/// </summary>
/// <param name="projectName">name for the .sqlproj</param>
/// <returns>Full path to the .sqlproj</returns>
internal static string CreateSqlProj(string projectName)
{
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchemaCompareTest", projectName);
Directory.CreateDirectory(folderPath);
string sqlprojFilePath = Path.Combine(folderPath, projectName + ".sqlproj");
File.Copy(Path.Combine(sqlProjectsFolder, "emptyTemplate.sqlproj"), sqlprojFilePath);
return sqlprojFilePath;
}
internal static string[] GetProjectScripts(string projectPath) internal static string[] GetProjectScripts(string projectPath)
{ {
return Directory.GetFiles(projectPath, "*.sql", SearchOption.AllDirectories); return Directory.GetFiles(projectPath, "*.sql", SearchOption.AllDirectories);

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build">
<Sdk Name="Microsoft.Build.Sql" Version="0.1.3-preview" />
<PropertyGroup>
<Name>TestProjectName</Name>
<ProjectGuid>{BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql160DatabaseSchemaProvider</DSP>
<ModelCollation>1033, CI</ModelCollation>
</PropertyGroup>
<Target Name="BeforeBuild">
<Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" />
</Target>
</Project>