Feature/schemacompare scmp save (#824)

* First cut of scmp Save related changes and some test refactoring

* Adding Exclude/Include objects in saving

* Add diff entry validation as part of test

* Adding PR comments - major change is change to nameparts in place of name hence preserving any "."/"[" in name and avoiding any string operations

* One more UT scenario addition for create excluded object
This commit is contained in:
udeeshagautam
2019-06-13 17:28:59 -07:00
committed by GitHub
parent b451670222
commit 432e054d55
11 changed files with 883 additions and 235 deletions

View File

@@ -0,0 +1,163 @@
//
// 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;
using Microsoft.SqlServer.Dac.Compare;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
/// <summary>
/// Internal class for utilities shared between multiple schema compare operations
/// </summary>
internal static class SchemaCompareUtils
{
internal static DacDeployOptions CreateSchemaCompareOptions(DeploymentOptions deploymentOptions)
{
System.Reflection.PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();
DacDeployOptions dacOptions = new DacDeployOptions();
foreach (var deployOptionsProp in deploymentOptionsProperties)
{
var prop = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
if (prop != null)
{
prop.SetValue(dacOptions, deployOptionsProp.GetValue(deploymentOptions));
}
}
return dacOptions;
}
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
{
if (difference == null)
{
return null;
}
DiffEntry diffEntry = new DiffEntry();
diffEntry.UpdateAction = difference.UpdateAction;
diffEntry.DifferenceType = difference.DifferenceType;
diffEntry.Name = difference.Name;
if (difference.SourceObject != null)
{
diffEntry.SourceValue = difference.SourceObject.Name.Parts.ToArray();
var sourceType = new SchemaComparisonExcludedObjectId(difference.SourceObject.ObjectType, difference.SourceObject.Name);
diffEntry.SourceObjectType = sourceType.TypeName;
}
if (difference.TargetObject != null)
{
diffEntry.TargetValue = difference.TargetObject.Name.Parts.ToArray();
var targetType = new SchemaComparisonExcludedObjectId(difference.TargetObject.ObjectType, difference.TargetObject.Name);
diffEntry.TargetObjectType = targetType.TypeName;
}
if (difference.DifferenceType == SchemaDifferenceType.Object)
{
// set source and target scripts
if (difference.SourceObject != null)
{
string sourceScript;
difference.SourceObject.TryGetScript(out sourceScript);
diffEntry.SourceScript = FormatScript(sourceScript);
}
if (difference.TargetObject != null)
{
string targetScript;
difference.TargetObject.TryGetScript(out targetScript);
diffEntry.TargetScript = FormatScript(targetScript);
}
}
diffEntry.Children = new List<DiffEntry>();
foreach (SchemaDifference child in difference.Children)
{
diffEntry.Children.Add(CreateDiffEntry(child, diffEntry));
}
return diffEntry;
}
internal static SchemaComparisonExcludedObjectId CreateExcludedObject(SchemaCompareObjectId sourceObj)
{
try
{
if (sourceObj == null || sourceObj.NameParts == null || string.IsNullOrEmpty(sourceObj.SqlObjectType))
{
return null;
}
ObjectIdentifier id = new ObjectIdentifier(sourceObj.NameParts);
SchemaComparisonExcludedObjectId excludedObjId = new SchemaComparisonExcludedObjectId(sourceObj.SqlObjectType, id);
return excludedObjId;
}
catch (ArgumentException)
{
return null;
}
}
internal static 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:
{
throw new NotSupportedException($"Endpoint Type {endpointInfo.EndpointType} is not supported");
}
}
}
internal static string GetConnectionString(ConnectionInfo connInfo, string databaseName)
{
if (connInfo == null)
{
return null;
}
connInfo.ConnectionDetails.DatabaseName = databaseName;
return ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
}
internal static string RemoveExcessWhitespace(string script)
{
if (script != null)
{
// remove leading and trailing whitespace
script = script.Trim();
// replace all multiple spaces with single space
script = Regex.Replace(script, " {2,}", " ");
}
return script;
}
internal static string FormatScript(string script)
{
script = RemoveExcessWhitespace(script);
if (!string.IsNullOrWhiteSpace(script) && !script.Equals("null"))
{
script += Environment.NewLine + "GO";
}
return script;
}
}
}