mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Add Schema Compare (#777)
* successfully sends list of differences to ADS * bumping dacfx nuget package version * add second schema compare try for case when first db to db comparison fails but works on second try * move schemacompare out of dacfx folders * capitalizing * more capitalizing * addressing comments
This commit is contained in:
@@ -21,6 +21,7 @@ using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Metadata;
|
||||
using Microsoft.SqlTools.ServiceLayer.Profiler;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCopmare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Scripting;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
@@ -118,6 +119,9 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
CmsService.Instance.InitializeService(serviceHost);
|
||||
serviceProvider.RegisterSingleService(CmsService.Instance);
|
||||
|
||||
SchemaCopmare.SchemaCompareService.Instance.InitializeService(serviceHost);
|
||||
serviceProvider.RegisterSingleService(SchemaCompareService.Instance);
|
||||
|
||||
InitializeHostedServices(serviceProvider, serviceHost);
|
||||
serviceHost.ServiceProvider = serviceProvider;
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
|
||||
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="$(SmoPackageVersion)" />
|
||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4240.1-preview" />
|
||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4316.1-preview" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0-preview3-26501-04" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
|
||||
{
|
||||
public enum SchemaCompareEndpointType
|
||||
{
|
||||
Database,
|
||||
Dacpac
|
||||
}
|
||||
|
||||
public class SchemaCompareEndpointInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the endpoint
|
||||
/// </summary>
|
||||
public SchemaCompareEndpointType EndpointType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets package filepath
|
||||
/// </summary>
|
||||
public string PackageFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets name for the database
|
||||
/// </summary>
|
||||
public string DatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Connection uri
|
||||
/// </summary>
|
||||
public string OwnerUri { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameters for a schema compare request.
|
||||
/// </summary>
|
||||
public class SchemaCompareParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the source endpoint info
|
||||
/// </summary>
|
||||
public SchemaCompareEndpointInfo SourceEndpointInfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the target endpoint info
|
||||
/// </summary>
|
||||
public SchemaCompareEndpointInfo TargetEndpointInfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Executation mode for the operation. Default is execution
|
||||
/// </summary>
|
||||
public TaskExecutionMode TaskExecutionMode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameters returned from a schema compare request.
|
||||
/// </summary>
|
||||
public class SchemaCompareResult : ResultStatus
|
||||
{
|
||||
public string OperationId { get; set; }
|
||||
|
||||
public bool AreEqual { get; set; }
|
||||
|
||||
public List<DiffEntry> Differences { get; set; }
|
||||
}
|
||||
|
||||
public class DiffEntry
|
||||
{
|
||||
public SchemaUpdateAction UpdateAction;
|
||||
public SchemaDifferenceType DifferenceType;
|
||||
public string Name;
|
||||
public string SourceValue;
|
||||
public string TargetValue;
|
||||
public DiffEntry Parent;
|
||||
public List<DiffEntry> Children;
|
||||
public string SourceScript;
|
||||
public string TargetScript;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the Schema Compare request type
|
||||
/// </summary>
|
||||
class SchemaCompareRequest
|
||||
{
|
||||
public static readonly RequestType<SchemaCompareParams, SchemaCompareResult> Type =
|
||||
RequestType<SchemaCompareParams, SchemaCompareResult>.Create("schemaCompare/compare");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
{
|
||||
/// <summary>
|
||||
/// Schema compare operation
|
||||
/// </summary>
|
||||
class SchemaCompareOperation : ITaskOperation
|
||||
{
|
||||
private CancellationTokenSource cancellation = new CancellationTokenSource();
|
||||
private bool disposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique id associated with this instance.
|
||||
/// </summary>
|
||||
public string OperationId { get; private set; }
|
||||
|
||||
public SqlTask SqlTask { get; set; }
|
||||
|
||||
public SchemaCompareParams Parameters { get; set; }
|
||||
|
||||
public string SourceConnectionString { get; set; }
|
||||
|
||||
public string TargetConnectionString { get; set; }
|
||||
|
||||
public SchemaComparisonResult ComparisonResult { get; set; }
|
||||
|
||||
public List<DiffEntry> Differences;
|
||||
|
||||
public SchemaCompareOperation(SchemaCompareParams parameters, ConnectionInfo sourceConnInfo, ConnectionInfo targetConnInfo)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
this.Parameters = parameters;
|
||||
this.SourceConnectionString = GetConnectionString(sourceConnInfo, parameters.SourceEndpointInfo.DatabaseName);
|
||||
this.TargetConnectionString = GetConnectionString(targetConnInfo, parameters.TargetEndpointInfo.DatabaseName);
|
||||
this.OperationId = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
protected CancellationToken CancellationToken { get { return this.cancellation.Token; } }
|
||||
|
||||
/// <summary>
|
||||
/// The error occurred during operation
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cancel operation
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
if (!this.cancellation.IsCancellationRequested)
|
||||
{
|
||||
Logger.Write(TraceEventType.Verbose, string.Format("Cancel invoked for OperationId {0}", this.OperationId));
|
||||
this.cancellation.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the operation.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
this.Cancel();
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(TaskExecutionMode mode)
|
||||
{
|
||||
if (this.CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw new OperationCanceledException(this.CancellationToken);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SchemaCompareEndpoint sourceEndpoint = CreateSchemaCompareEndpoint(this.Parameters.SourceEndpointInfo, this.SourceConnectionString);
|
||||
SchemaCompareEndpoint targetEndpoint = CreateSchemaCompareEndpoint(this.Parameters.TargetEndpointInfo, this.TargetConnectionString);
|
||||
|
||||
SchemaComparison comparison = new SchemaComparison(sourceEndpoint, targetEndpoint);
|
||||
this.ComparisonResult = comparison.Compare();
|
||||
|
||||
// try one more time if it didn't work the first time
|
||||
if(!this.ComparisonResult.IsValid)
|
||||
{
|
||||
this.ComparisonResult = comparison.Compare();
|
||||
}
|
||||
|
||||
this.Differences = new List<DiffEntry>();
|
||||
foreach (SchemaDifference difference in this.ComparisonResult.Differences)
|
||||
{
|
||||
DiffEntry diffEntry = CreateDiffEntry(difference, null);
|
||||
this.Differences.Add(diffEntry);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare operation {0} failed with exception {1}", this.OperationId, e));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
|
||||
{
|
||||
DiffEntry diffEntry = new DiffEntry();
|
||||
diffEntry.UpdateAction = difference.UpdateAction;
|
||||
diffEntry.DifferenceType = difference.DifferenceType;
|
||||
diffEntry.Name = difference.Name;
|
||||
|
||||
if (difference.SourceObject != null)
|
||||
{
|
||||
diffEntry.SourceValue = GetName(difference.SourceObject.Name.ToString());
|
||||
}
|
||||
if (difference.TargetObject != null)
|
||||
{
|
||||
diffEntry.TargetValue = GetName(difference.TargetObject.Name.ToString());
|
||||
}
|
||||
|
||||
if (difference.DifferenceType == SchemaDifferenceType.Object)
|
||||
{
|
||||
// set source and target scripts
|
||||
if (difference.SourceObject != null)
|
||||
{
|
||||
string sourceScript;
|
||||
difference.SourceObject.TryGetScript(out sourceScript);
|
||||
diffEntry.SourceScript = RemoveExcessWhitespace(sourceScript);
|
||||
}
|
||||
if (difference.TargetObject != null)
|
||||
{
|
||||
string targetScript;
|
||||
difference.TargetObject.TryGetScript(out targetScript);
|
||||
diffEntry.TargetScript = RemoveExcessWhitespace(targetScript);
|
||||
}
|
||||
}
|
||||
|
||||
diffEntry.Children = new List<DiffEntry>();
|
||||
|
||||
foreach (SchemaDifference child in difference.Children)
|
||||
{
|
||||
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry));
|
||||
}
|
||||
|
||||
return diffEntry;
|
||||
}
|
||||
|
||||
private SchemaCompareEndpoint CreateSchemaCompareEndpoint(SchemaCompareEndpointInfo endpointInfo, string connectionString)
|
||||
{
|
||||
switch (endpointInfo.EndpointType)
|
||||
{
|
||||
case SchemaCompareEndpointType.Dacpac:
|
||||
{
|
||||
return new SchemaCompareDacpacEndpoint(endpointInfo.PackageFilePath);
|
||||
}
|
||||
case SchemaCompareEndpointType.Database:
|
||||
{
|
||||
return new SchemaCompareDatabaseEndpoint(connectionString);
|
||||
}
|
||||
default:
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string GetConnectionString(ConnectionInfo connInfo, string databaseName)
|
||||
{
|
||||
if (connInfo == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
connInfo.ConnectionDetails.DatabaseName = databaseName;
|
||||
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
|
||||
}
|
||||
|
||||
private string RemoveExcessWhitespace(string script)
|
||||
{
|
||||
// replace all multiple spaces with single space
|
||||
return Regex.Replace(script, " {2,}", " ");
|
||||
}
|
||||
|
||||
private string GetName(string name)
|
||||
{
|
||||
// remove brackets from name
|
||||
return Regex.Replace(name, @"[\[\]]", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
|
||||
{
|
||||
/// <summary>
|
||||
/// Main class for SchemaCompare service
|
||||
/// </summary>
|
||||
class SchemaCompareService
|
||||
{
|
||||
private static ConnectionService connectionService = null;
|
||||
private static readonly Lazy<SchemaCompareService> instance = new Lazy<SchemaCompareService>(() => new SchemaCompareService());
|
||||
|
||||
/// <summary>
|
||||
/// Gets the singleton instance object
|
||||
/// </summary>
|
||||
public static SchemaCompareService Instance
|
||||
{
|
||||
get { return instance.Value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the service instance
|
||||
/// </summary>
|
||||
/// <param name="serviceHost"></param>
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
serviceHost.SetRequestHandler(SchemaCompareRequest.Type, this.HandleSchemaCompareRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles schema compare request
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareRequest(SchemaCompareParams parameters, RequestContext<SchemaCompareResult> requestContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
ConnectionInfo sourceConnInfo;
|
||||
ConnectionInfo targetConnInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.SourceEndpointInfo.OwnerUri,
|
||||
out sourceConnInfo);
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.TargetEndpointInfo.OwnerUri,
|
||||
out targetConnInfo);
|
||||
|
||||
Task schemaCompareTask = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
SchemaCompareOperation operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation.OperationId,
|
||||
Success = true,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
AreEqual = operation.ComparisonResult.IsEqual,
|
||||
Differences = operation.Differences
|
||||
});
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
}
|
||||
|
||||
internal static ConnectionService ConnectionServiceInstance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (connectionService == null)
|
||||
{
|
||||
connectionService = ConnectionService.Instance;
|
||||
}
|
||||
return connectionService;
|
||||
}
|
||||
set
|
||||
{
|
||||
connectionService = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,13 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
@@ -19,6 +18,28 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
{
|
||||
public class DacFxServiceTests
|
||||
{
|
||||
private const string SourceScript = @"CREATE TABLE [dbo].[table1]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[Date] DATE NOT NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL
|
||||
)";
|
||||
|
||||
private const string TargetScript = @"CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL,
|
||||
[col2] NCHAR(10) NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table3]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] INT NULL,
|
||||
)";
|
||||
|
||||
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
{
|
||||
@@ -263,17 +284,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DacFx
|
||||
private async Task<Mock<RequestContext<DacFxResult>>> SendAndValidateGenerateDeployPlanRequest()
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, null, "DacFxGenerateDeployPlanTest");
|
||||
sourceDb.RunQuery(@"CREATE TABLE [dbo].[table1]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[Date] DATE NOT NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL
|
||||
)");
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "DacFxGenerateDeployPlanTest");
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest");
|
||||
Directory.CreateDirectory(folderPath);
|
||||
@@ -293,18 +305,7 @@ CREATE TABLE [dbo].[table2]
|
||||
var generateDeployPlanRequestContext = new Mock<RequestContext<DacFxResult>>();
|
||||
generateDeployPlanRequestContext.Setup(x => x.SendResult(It.IsAny<DacFxResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, null, "DacFxGenerateDeployPlanTestTarget");
|
||||
targetDb.RunQuery(@"CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL,
|
||||
[col2] NCHAR(10) NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table3]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] INT NULL,
|
||||
)");
|
||||
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "DacFxGenerateDeployPlanTestTarget");
|
||||
|
||||
var generateDeployPlanParams = new GenerateDeployPlanParams
|
||||
{
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
|
||||
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="$(SmoPackageVersion)" />
|
||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4240.1-preview" />
|
||||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="150.4316.1-preview" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
|
||||
{
|
||||
public class SchemaCompareServiceTests
|
||||
{
|
||||
private const string SourceScript = @"CREATE TABLE [dbo].[table1]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[Date] DATE NOT NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL
|
||||
)";
|
||||
|
||||
private const string TargetScript = @"CREATE TABLE [dbo].[table2]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] NCHAR(10) NULL,
|
||||
[col2] NCHAR(10) NULL
|
||||
)
|
||||
CREATE TABLE [dbo].[table3]
|
||||
(
|
||||
[ID] INT NOT NULL PRIMARY KEY,
|
||||
[col1] INT NULL,
|
||||
)";
|
||||
|
||||
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
{
|
||||
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<Mock<RequestContext<SchemaCompareResult>>> SendAndValidateSchemaCompareRequestDacpacToDacpac()
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var schemaCompareRequestContext = new Mock<RequestContext<SchemaCompareResult>>();
|
||||
schemaCompareRequestContext.Setup(x => x.SendResult(It.IsAny<SchemaCompareResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
// create dacpacs from databases
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareSource");
|
||||
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "SchemaCompareTarget");
|
||||
string sourceDacpacFilePath = CreateDacpac(sourceDb);
|
||||
string targetDacpacFilePath = CreateDacpac(targetDb);
|
||||
|
||||
SchemaCompareEndpointInfo sourceInfo = new SchemaCompareEndpointInfo();
|
||||
SchemaCompareEndpointInfo targetInfo = new SchemaCompareEndpointInfo();
|
||||
|
||||
sourceInfo.EndpointType = SchemaCompareEndpointType.Dacpac;
|
||||
sourceInfo.PackageFilePath = sourceDacpacFilePath;
|
||||
targetInfo.EndpointType = SchemaCompareEndpointType.Dacpac;
|
||||
targetInfo.PackageFilePath = targetDacpacFilePath;
|
||||
|
||||
var schemaCompareParams = new SchemaCompareParams
|
||||
{
|
||||
SourceEndpointInfo = sourceInfo,
|
||||
TargetEndpointInfo = targetInfo
|
||||
};
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, null, null);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
// cleanup
|
||||
VerifyAndCleanup(sourceDacpacFilePath);
|
||||
VerifyAndCleanup(targetDacpacFilePath);
|
||||
sourceDb.Cleanup();
|
||||
targetDb.Cleanup();
|
||||
|
||||
return schemaCompareRequestContext;
|
||||
}
|
||||
|
||||
private async Task<Mock<RequestContext<SchemaCompareResult>>> SendAndValidateSchemaCompareRequestDatabaseToDatabase()
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var schemaCompareRequestContext = new Mock<RequestContext<SchemaCompareResult>>();
|
||||
schemaCompareRequestContext.Setup(x => x.SendResult(It.IsAny<SchemaCompareResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareSource");
|
||||
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "SchemaCompareTarget");
|
||||
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest");
|
||||
Directory.CreateDirectory(folderPath);
|
||||
|
||||
SchemaCompareEndpointInfo sourceInfo = new SchemaCompareEndpointInfo();
|
||||
SchemaCompareEndpointInfo targetInfo = new SchemaCompareEndpointInfo();
|
||||
|
||||
sourceInfo.EndpointType = SchemaCompareEndpointType.Database;
|
||||
sourceInfo.DatabaseName = sourceDb.DatabaseName;
|
||||
targetInfo.EndpointType = SchemaCompareEndpointType.Database;
|
||||
targetInfo.DatabaseName = targetDb.DatabaseName;
|
||||
|
||||
var schemaCompareParams = new SchemaCompareParams
|
||||
{
|
||||
SourceEndpointInfo = sourceInfo,
|
||||
TargetEndpointInfo = targetInfo
|
||||
};
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
SchemaCompareOperation schemaCompareOperation = new SchemaCompareOperation(schemaCompareParams, result.ConnectionInfo, result.ConnectionInfo);
|
||||
schemaCompareOperation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
Assert.True(schemaCompareOperation.ComparisonResult.IsValid);
|
||||
Assert.False(schemaCompareOperation.ComparisonResult.IsEqual);
|
||||
Assert.NotNull(schemaCompareOperation.ComparisonResult.Differences);
|
||||
|
||||
// cleanup
|
||||
sourceDb.Cleanup();
|
||||
targetDb.Cleanup();
|
||||
|
||||
return schemaCompareRequestContext;
|
||||
}
|
||||
|
||||
private async Task<Mock<RequestContext<SchemaCompareResult>>> SendAndValidateSchemaCompareRequestDatabaseToDacpac()
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var schemaCompareRequestContext = new Mock<RequestContext<SchemaCompareResult>>();
|
||||
schemaCompareRequestContext.Setup(x => x.SendResult(It.IsAny<SchemaCompareResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareSource");
|
||||
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "SchemaCompareTarget");
|
||||
string targetDacpacFilePath = 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
|
||||
};
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
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);
|
||||
|
||||
// cleanup
|
||||
VerifyAndCleanup(targetDacpacFilePath);
|
||||
sourceDb.Cleanup();
|
||||
targetDb.Cleanup();
|
||||
|
||||
return schemaCompareRequestContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare request comparing two dacpacs
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SchemaCompareDacpacToDacpac()
|
||||
{
|
||||
Assert.NotNull(SendAndValidateSchemaCompareRequestDacpacToDacpac());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare request comparing a two databases
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void SchemaCompareDatabaseToDatabase()
|
||||
{
|
||||
Assert.NotNull(await SendAndValidateSchemaCompareRequestDatabaseToDatabase());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the schema compare request comparing a database to a dacpac
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void SchemaCompareDatabaseToDacpac()
|
||||
{
|
||||
Assert.NotNull(await SendAndValidateSchemaCompareRequestDatabaseToDacpac());
|
||||
}
|
||||
|
||||
private void VerifyAndCleanup(string filePath)
|
||||
{
|
||||
// Verify it was created
|
||||
Assert.True(File.Exists(filePath));
|
||||
|
||||
// Remove the file
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
private string CreateDacpac(SqlTestDb testdb)
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DacFxTest");
|
||||
Directory.CreateDirectory(folderPath);
|
||||
|
||||
var extractParams = new ExtractParams
|
||||
{
|
||||
DatabaseName = testdb.DatabaseName,
|
||||
PackageFilePath = Path.Combine(folderPath, string.Format("{0}.dacpac", testdb.DatabaseName)),
|
||||
ApplicationName = "test",
|
||||
ApplicationVersion = new Version(1, 0)
|
||||
};
|
||||
|
||||
DacFxService service = new DacFxService();
|
||||
ExtractOperation operation = new ExtractOperation(extractParams, result.ConnectionInfo);
|
||||
service.PerformOperation(operation);
|
||||
|
||||
return extractParams.PackageFilePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user