Schema compare include/exclude changes (#881)

* send back success of include/exclude request

* update dacfx nuget package and use new api to get affected dependencies of include/exclude request

* addressing comments

* rename test

* Addressing comments
This commit is contained in:
Kim Santiago
2019-10-24 17:11:30 -07:00
committed by GitHub
parent a0e56c5747
commit e60fc1a16c
9 changed files with 170 additions and 10 deletions

View File

@@ -33,7 +33,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4534.2-preview" />
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4576.1-preview" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />

View File

@@ -45,6 +45,19 @@ CREATE TABLE [dbo].[table3]
[col1] INT NULL,
)";
private const string SourceIncludeExcludeScript = @"CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT)
GO
CREATE TABLE t2(c1 INT PRIMARY KEY, c2 INT)
GO
cREATE VIEW v1 as SELECT c1 FROM t2";
private const string TargetIncludeExcludeScript = @"CREATE TABLE t1 (c1 INT PRIMARY KEY)
GO
CREATE TABLE t3 (c3 INT PRIMARY KEY, c2 INT)
GO
CREATE VIEW v2 as SELECT t1.c1, t3.c3 FROM t1, t3
GO";
private const string CreateKey = @"CREATE COLUMN MASTER KEY [CMK_Auto1]
WITH (
KEY_STORE_PROVIDER_NAME = N'MSSQL_CERTIFICATE_STORE',
@@ -801,7 +814,102 @@ WITH VALUES
sourceDb.Cleanup();
targetDb.Cleanup();
}
}
/// <summary>
/// Verify the schema compare request with failing exclude request because of dependencies and that include will include dependencies
/// </summary>
[Fact]
public async void SchemaCompareIncludeExcludeWithDependencies()
{
var result = SchemaCompareTestUtils.GetLiveAutoCompleteTestObjects();
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceIncludeExcludeScript, "SchemaCompareSource");
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetIncludeExcludeScript, "SchemaCompareTarget");
try
{
string targetDacpacFilePath = SchemaCompareTestUtils.CreateDacpac(targetDb);
SchemaCompareEndpointInfo sourceInfo = new SchemaCompareEndpointInfo();
SchemaCompareEndpointInfo targetInfo = new SchemaCompareEndpointInfo();
sourceInfo.EndpointType = SchemaCompareEndpointType.Database;
sourceInfo.DatabaseName = sourceDb.DatabaseName;
targetInfo.EndpointType = SchemaCompareEndpointType.Dacpac;
targetInfo.PackageFilePath = targetDacpacFilePath;
var schemaCompareParams = new SchemaCompareParams
{
SourceEndpointInfo = sourceInfo,
TargetEndpointInfo = targetInfo
};
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, null);
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
// try to exclude
DiffEntry t2Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First(), null);
SchemaCompareNodeParams t2ExcludeParams = new SchemaCompareNodeParams()
{
OperationId = schemaCompareOperation.OperationId,
DiffEntry = t2Diff,
IncludeRequest = false,
TaskExecutionMode = TaskExecutionMode.Execute
};
SchemaCompareIncludeExcludeNodeOperation t2ExcludeOperation = new SchemaCompareIncludeExcludeNodeOperation(t2ExcludeParams, schemaCompareOperation.ComparisonResult);
t2ExcludeOperation.Execute(TaskExecutionMode.Execute);
Assert.False(t2ExcludeOperation.Success, "Excluding Table t2 should fail because view v1 depends on it");
Assert.True(t2ExcludeOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First().Included, "Difference Table t2 should still be included because the exclude request failed");
// exclude view first, then excluding t2 should work
DiffEntry v1Diff = SchemaCompareUtils.CreateDiffEntry(schemaCompareOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "v1").First(), null);
SchemaCompareNodeParams v1ExcludeParams = new SchemaCompareNodeParams()
{
OperationId = schemaCompareOperation.OperationId,
DiffEntry = v1Diff,
IncludeRequest = false,
TaskExecutionMode = TaskExecutionMode.Execute
};
SchemaCompareIncludeExcludeNodeOperation v1ExcludeOperation = new SchemaCompareIncludeExcludeNodeOperation(v1ExcludeParams, schemaCompareOperation.ComparisonResult);
v1ExcludeOperation.Execute(TaskExecutionMode.Execute);
Assert.True(v1ExcludeOperation.Success, "Excluding View v1 should succeed");
Assert.False(t2ExcludeOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "v1").First().Included, "Difference View v1 should be excluded");
// try to exclude t2 again and it should succeed this time
t2ExcludeOperation.Execute(TaskExecutionMode.Execute);
Assert.True(t2ExcludeOperation.Success, "Excluding Table t2 should succeed");
Assert.False(t2ExcludeOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First().Included, "Difference Table t2 should still be excluded");
// including v1 should also include t2
SchemaCompareNodeParams v1IncludeParams = new SchemaCompareNodeParams()
{
OperationId = schemaCompareOperation.OperationId,
DiffEntry = v1Diff,
IncludeRequest = true,
TaskExecutionMode = TaskExecutionMode.Execute
};
SchemaCompareIncludeExcludeNodeOperation v1IncludeOperation = new SchemaCompareIncludeExcludeNodeOperation(v1IncludeParams, t2ExcludeOperation.ComparisonResult);
v1IncludeOperation.Execute(TaskExecutionMode.Execute);
Assert.True(v1IncludeOperation.Success, "Including v1 should succeed");
Assert.True(v1IncludeOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "v1").First().Included, "Difference View v1 should be included");
Assert.True(v1IncludeOperation.ComparisonResult.Differences.Where(x => x.SourceObject != null && x.SourceObject.Name.Parts[1] == "t2").First().Included, "Difference Table t2 should still be included");
Assert.True(v1IncludeOperation.ChangedDifferences != null && v1IncludeOperation.ChangedDifferences.Count == 1, "There should be one difference");
Assert.True(v1IncludeOperation.ChangedDifferences.First().SourceValue[1] == "t2", "The affected difference of including v1 should be t2");
// cleanup
SchemaCompareTestUtils.VerifyAndCleanup(targetDacpacFilePath);
}
finally
{
sourceDb.Cleanup();
targetDb.Cleanup();
}
}
private void ValidateSchemaCompareWithExcludeIncludeResults(SchemaCompareOperation schemaCompareOperation)