Save file structure in scmp file (#1878)

* Save file structure in scmp files

* Update DacFx version and add test

* Address comments

* Fix test

* try to fix tests

---------

Co-authored-by: Kim Santiago <kisantia@microsoft.com>
This commit is contained in:
Sakshi Sharma
2023-02-24 14:24:25 -08:00
committed by GitHub
parent f7fd478857
commit b44f0d561f
4 changed files with 63 additions and 3 deletions

View File

@@ -20,7 +20,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="161.8085.0-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx" Version="161.8406.0-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx.Projects" Version="161.8093.0-alpha" />
<PackageReference Update="Microsoft.Azure.Kusto.Data" Version="9.0.4" />
<PackageReference Update="Microsoft.Azure.Kusto.Language" Version="9.0.4" />

View File

@@ -5,6 +5,7 @@
#nullable disable
using System.Collections.Generic;
using Microsoft.SqlServer.Dac;
using Microsoft.SqlServer.Dac.Compare;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
@@ -70,6 +71,11 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
/// Connection details
/// </summary>
public ConnectionDetails ConnectionDetails { get; set; }
/// <summary>
/// Extract target of the project used when extracting a database to file system or updating the project from database
/// </summary>
public DacExtractTarget? ExtractTarget { get; set; }
}
/// <summary>

View File

@@ -176,7 +176,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
case SchemaCompareEndpointType.Project:
{
return new SchemaCompareProjectEndpoint(endpointInfo.ProjectFilePath, endpointInfo.TargetScripts, endpointInfo.DataSchemaProvider);
return endpointInfo?.ExtractTarget != null
? new SchemaCompareProjectEndpoint(endpointInfo.ProjectFilePath, endpointInfo.TargetScripts, endpointInfo.DataSchemaProvider, (DacExtractTarget)endpointInfo?.ExtractTarget)
: new SchemaCompareProjectEndpoint(endpointInfo.ProjectFilePath, endpointInfo.TargetScripts, endpointInfo.DataSchemaProvider);
}
case SchemaCompareEndpointType.Dacpac:
{

View File

@@ -1200,6 +1200,41 @@ WITH VALUES
await CreateAndOpenScmp(SchemaCompareEndpointType.Project, SchemaCompareEndpointType.Project);
}
/// <summary>
/// Verify folder structure gets stored in scmp file for project endpoint
/// </summary>
[Test]
public async Task VerifyExtractTargetInScmpFile()
{
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareOpenScmpSource");
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "SchemaCompareOpenScmpTarget");
try
{
SchemaCompareEndpoint sourceEndpoint = CreateSchemaCompareEndpoint(sourceDb, SchemaCompareEndpointType.Database);
SchemaCompareEndpoint targetEndpoint = CreateSchemaCompareEndpoint(targetDb, SchemaCompareEndpointType.Project, true);
// create a comparison and exclude the first difference
SchemaComparison compare = new SchemaComparison(sourceEndpoint, targetEndpoint);
SchemaComparisonResult result = compare.Compare();
Assert.That(result.Differences, Is.Not.Empty);
// save to scmp
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchemaCompareTest");
Directory.CreateDirectory(folderPath);
string filePath = Path.Combine(folderPath, string.Format("SchemaCompareOpenScmpTest{0}.scmp", DateTime.Now.ToFileTime()));
compare.SaveToFile(filePath);
SchemaCompareTestUtils.VerifyAndCleanup(Directory.GetParent((targetEndpoint as SchemaCompareProjectEndpoint).ProjectFilePath).FullName);
await VerifyContentAndCleanupAsync(filePath, "<FolderStructure>ObjectType</FolderStructure>");
}
finally
{
sourceDb.Cleanup();
targetDb.Cleanup();
}
}
/// <summary>
/// Verify the schema compare Service Calls ends to end
/// </summary>
@@ -1594,6 +1629,23 @@ WITH VALUES
}
}
private async Task VerifyContentAndCleanupAsync(string outputFilePath, string textToMatch)
{
// Verify it was created
Assert.True(File.Exists(outputFilePath), "The output file did not get generated.");
//Verify the contents contain the stringToMatch
string output = await File.ReadAllTextAsync(outputFilePath);
Assert.True(output.Contains(textToMatch), $"The output doesn't contain the string. Pattern expected {Environment.NewLine} {textToMatch} {Environment.NewLine} Actual file {Environment.NewLine} {output}");
// Remove the file
if (File.Exists(outputFilePath))
{
File.Delete(outputFilePath);
}
}
private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation, int? expectedDifferencesCount = null)
{
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
@@ -1794,7 +1846,7 @@ WITH VALUES
{
string projectPath = SchemaCompareTestUtils.CreateProject(db, isProjectTarget ? "TargetProject" : "SourceProject");
string[] scripts = SchemaCompareTestUtils.GetProjectScripts(projectPath);
return new SchemaCompareProjectEndpoint(Path.Combine(projectPath, isProjectTarget ? "TargetProject.sqlproj" : "SourceProject.sqlproj"), scripts, "150");
return new SchemaCompareProjectEndpoint(Path.Combine(projectPath, isProjectTarget ? "TargetProject.sqlproj" : "SourceProject.sqlproj"), scripts, "150", DacExtractTarget.ObjectType);
}
else
{