From 6f198e9e5aacc0e1a8eacb3ad07b556e0cbe3d52 Mon Sep 17 00:00:00 2001 From: Kim Santiago <31145923+kisantia@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:04:31 -0700 Subject: [PATCH] fix schema comparing empty project showing more differences than expected (#1727) --- .../SchemaCompare/SchemaCompareOperation.cs | 3 ++ ...Tools.ServiceLayer.IntegrationTests.csproj | 3 ++ .../SchemaCompareServiceTests.cs | 42 ++++++++++++++++++- .../SchemaCompare/SchemaCompareTestUtils.cs | 28 +++++++++++-- .../SqlProjects/emptyTemplate.sqlproj | 13 ++++++ 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SqlProjects/emptyTemplate.sqlproj diff --git a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs index 988fa239..b2cdc43e 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareOperation.cs @@ -113,6 +113,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare this.Differences = new List(); if (this.ComparisonResult.Differences != null) { + // filter out not included and not excludeable differences + (this.ComparisonResult.Differences as List).RemoveAll(d => !d.Included && !d.IsExcludable); + foreach (SchemaDifference difference in this.ComparisonResult.Differences) { DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null); diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj index db2ab85f..e0333102 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj @@ -53,4 +53,7 @@ + + + diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareServiceTests.cs index 24abea94..6a455004 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareServiceTests.cs @@ -281,6 +281,41 @@ WITH VALUES } } + /// + /// Verify the schema compare request comparing empty project to a database + /// + [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(); + } + } + /// /// Verify the schema compare request comparing a dacpac and a project /// @@ -1554,7 +1589,7 @@ WITH VALUES } } - private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation) + private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation, int? expectedDifferencesCount = null) { schemaCompareOperation.Execute(TaskExecutionMode.Execute); @@ -1563,6 +1598,11 @@ WITH VALUES Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences); 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 DiffEntry diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.First(), null); diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareTestUtils.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareTestUtils.cs index 97b52935..0f265ad8 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareTestUtils.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SchemaCompareTestUtils.cs @@ -22,6 +22,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare { internal static class SchemaCompareTestUtils { + private static string sqlProjectsFolder = Path.Combine("..", "..", "..", "SchemaCompare", "SqlProjects"); + internal static void VerifyAndCleanup(string path) { // verify it was created... @@ -61,12 +63,17 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare return extractParams.PackageFilePath; } + /// + /// Creates an SDK-style .sqlproj from the database + /// + /// Database to create the sql project from + /// Name of the project + /// Full path to the project folder internal static string CreateProject(SqlTestDb testdb, string projectName) { var result = GetLiveAutoCompleteTestObjects(); - string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchemaCompareTest", projectName); - Directory.CreateDirectory(folderPath); - File.Create(Path.Combine(folderPath, projectName + ".sqlproj")).Close(); + string sqlprojFilePath = CreateSqlProj(projectName); + string folderPath = Path.GetDirectoryName(sqlprojFilePath); var extractParams = new ExtractParams { @@ -84,6 +91,21 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare return folderPath; } + /// + /// Creates an empty SDK-style .sqlproj + /// + /// name for the .sqlproj + /// Full path to the .sqlproj + 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) { return Directory.GetFiles(projectPath, "*.sql", SearchOption.AllDirectories); diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SqlProjects/emptyTemplate.sqlproj b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SqlProjects/emptyTemplate.sqlproj new file mode 100644 index 00000000..e58535b7 --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/SchemaCompare/SqlProjects/emptyTemplate.sqlproj @@ -0,0 +1,13 @@ + + + + + TestProjectName + {BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575} + Microsoft.Data.Tools.Schema.Sql.Sql160DatabaseSchemaProvider + 1033, CI + + + + +