mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-27 01:25:42 -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user