mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-08 01:28:29 -05:00
Send deploymentOptions to DacFx to save in the publish profile xml file (#1997)
* Send deploymentOptions to DacFx to save in the publish profile xml file * Update Packages.prop with latest DacFx nuget version. * Update Dacfx version after merge from main. * Address comments * Update method name
This commit is contained in:
@@ -329,7 +329,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
DacProfile profile = new DacProfile();
|
||||
profile.TargetDatabaseName = parameters.DatabaseName;
|
||||
profile.TargetConnectionString = parameters.ConnectionString;
|
||||
//TODO: Set deploy options to pass on to DacFx
|
||||
profile.DeployOptions = DacFxUtils.CreateDeploymentOptions(parameters.DeploymentOptions);
|
||||
|
||||
if (parameters.SqlCommandVariableValues != null)
|
||||
{
|
||||
@@ -338,7 +338,6 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
profile.DeployOptions.SqlCommandVariableValues[key] = parameters.SqlCommandVariableValues[key];
|
||||
}
|
||||
}
|
||||
//TODO: Add return from Save with success/fail status
|
||||
profile.Save(parameters.ProfilePath);
|
||||
}
|
||||
}, requestContext);
|
||||
|
||||
89
src/Microsoft.SqlTools.ServiceLayer/DacFx/DacFxUtils.cs
Normal file
89
src/Microsoft.SqlTools.ServiceLayer/DacFx/DacFxUtils.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
{
|
||||
internal static class DacFxUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts DeploymentOptions used in STS and ADS to DacDeployOptions which can be passed to the DacFx apis
|
||||
/// </summary>
|
||||
/// <param name="deploymentOptions"></param>
|
||||
/// <returns>DacDeployOptions</returns
|
||||
internal static DacDeployOptions CreateDeploymentOptions(DeploymentOptions deploymentOptions)
|
||||
{
|
||||
try
|
||||
{
|
||||
PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();
|
||||
|
||||
DacDeployOptions dacOptions = new DacDeployOptions();
|
||||
Type propType = dacOptions.GetType();
|
||||
Dictionary<string, DeploymentOptionProperty<bool>> booleanOptionsDictionary = new Dictionary<string, DeploymentOptionProperty<bool>>();
|
||||
|
||||
foreach (PropertyInfo deployOptionsProp in deploymentOptionsProperties)
|
||||
{
|
||||
var prop = propType.GetProperty(deployOptionsProp.Name);
|
||||
// Set the excludeObjectTypes values to the DacDeployOptions
|
||||
if (prop != null && deployOptionsProp.Name == nameof(deploymentOptions.ExcludeObjectTypes))
|
||||
{
|
||||
List<ObjectType> finalExcludeObjects = new List<ObjectType> { };
|
||||
var val = deployOptionsProp.GetValue(deploymentOptions);
|
||||
string[] excludeObjectTypeOptionsArray = (string[])val.GetType().GetProperty("Value").GetValue(val);
|
||||
|
||||
if (excludeObjectTypeOptionsArray != null)
|
||||
{
|
||||
foreach (string objectTypeValue in excludeObjectTypeOptionsArray)
|
||||
{
|
||||
ObjectType objectTypeName = new ObjectType();
|
||||
|
||||
if (objectTypeValue != null && Enum.TryParse(objectTypeValue, ignoreCase: true, out objectTypeName))
|
||||
{
|
||||
finalExcludeObjects.Add(objectTypeName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format($"{objectTypeValue} is not part of ObjectTypes enum"));
|
||||
}
|
||||
}
|
||||
// set final values to excludeObjectType property
|
||||
prop.SetValue(dacOptions, finalExcludeObjects.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
// BooleanOptionsDictionary has all the deployment options and is being processed separately in the second iteration by collecting here
|
||||
if (deployOptionsProp.Name == nameof(deploymentOptions.BooleanOptionsDictionary))
|
||||
{
|
||||
booleanOptionsDictionary = deploymentOptions.BooleanOptionsDictionary as Dictionary<string, DeploymentOptionProperty<bool>>;
|
||||
}
|
||||
}
|
||||
|
||||
// Iterating through the updated boolean options coming from the booleanOptionsDictionary and assigning them to DacDeployOptions
|
||||
foreach (KeyValuePair<string, DeploymentOptionProperty<bool>> deployOptionsProp in booleanOptionsDictionary)
|
||||
{
|
||||
var prop = propType.GetProperty(deployOptionsProp.Key);
|
||||
if (prop != null)
|
||||
{
|
||||
var selectedVal = deployOptionsProp.Value.Value;
|
||||
prop.SetValue(dacOptions, selectedVal);
|
||||
}
|
||||
}
|
||||
return dacOptions;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare create options model failed: {0}", e.Message));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
@@ -28,7 +27,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
public override void Execute()
|
||||
{
|
||||
DacPackage dacpac = DacPackage.Load(this.Parameters.PackageFilePath);
|
||||
DacDeployOptions options = this.Parameters.DeploymentOptions != null ? SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions) : this.GetDefaultDeployOptions();
|
||||
DacDeployOptions options = this.Parameters.DeploymentOptions != null ? DacFxUtils.CreateDeploymentOptions(this.Parameters.DeploymentOptions) : this.GetDefaultDeployOptions();
|
||||
|
||||
if (this.Parameters.SqlCommandVariableValues != null)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,6 @@ using System.Threading;
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
@@ -36,7 +35,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
|
||||
PublishOptions publishOptions = new PublishOptions();
|
||||
publishOptions.GenerateDeploymentReport = this.Parameters.GenerateDeploymentReport;
|
||||
publishOptions.CancelToken = this.CancellationToken;
|
||||
publishOptions.DeployOptions = this.Parameters.DeploymentOptions != null ? SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions) : this.GetDefaultDeployOptions();
|
||||
publishOptions.DeployOptions = this.Parameters.DeploymentOptions != null ? DacFxUtils.CreateDeploymentOptions(this.Parameters.DeploymentOptions) : this.GetDefaultDeployOptions();
|
||||
|
||||
if (this.Parameters.SqlCommandVariableValues != null)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#nullable disable
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -92,7 +93,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
|
||||
if (this.Parameters.DeploymentOptions != null)
|
||||
{
|
||||
comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
|
||||
comparison.Options = DacFxUtils.CreateDeploymentOptions(this.Parameters.DeploymentOptions);
|
||||
}
|
||||
|
||||
// for testing
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#nullable disable
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
@@ -87,7 +88,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
|
||||
if (this.Parameters.DeploymentOptions != null)
|
||||
{
|
||||
comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
|
||||
comparison.Options = DacFxUtils.CreateDeploymentOptions(this.Parameters.DeploymentOptions);
|
||||
}
|
||||
|
||||
comparison.SaveToFile(this.Parameters.ScmpFilePath, true);
|
||||
|
||||
@@ -6,19 +6,15 @@
|
||||
#nullable disable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.SqlServer.Dac;
|
||||
using Microsoft.SqlServer.Dac.Compare;
|
||||
using Microsoft.SqlServer.Dac.Model;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using static Microsoft.SqlTools.Utility.SqlConstants;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
{
|
||||
@@ -28,77 +24,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
/// </summary>
|
||||
internal static class SchemaCompareUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts DeploymentOptions used in STS and ADS to DacDeployOptions which can be passed to the DacFx apis
|
||||
/// </summary>
|
||||
/// <param name="deploymentOptions"></param>
|
||||
/// <returns>DacDeployOptions</returns
|
||||
internal static DacDeployOptions CreateSchemaCompareOptions(DeploymentOptions deploymentOptions)
|
||||
{
|
||||
try
|
||||
{
|
||||
PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();
|
||||
|
||||
DacDeployOptions dacOptions = new DacDeployOptions();
|
||||
Type propType = dacOptions.GetType();
|
||||
Dictionary<string, DeploymentOptionProperty<bool>> booleanOptionsDictionary = new Dictionary<string, DeploymentOptionProperty<bool>>();
|
||||
|
||||
foreach (PropertyInfo deployOptionsProp in deploymentOptionsProperties)
|
||||
{
|
||||
var prop = propType.GetProperty(deployOptionsProp.Name);
|
||||
// Set the excludeObjectTypes values to the DacDeployOptions
|
||||
if (prop != null && deployOptionsProp.Name == nameof(deploymentOptions.ExcludeObjectTypes))
|
||||
{
|
||||
List<ObjectType> finalExcludeObjects = new List<ObjectType> { };
|
||||
var val = deployOptionsProp.GetValue(deploymentOptions);
|
||||
string[] excludeObjectTypeOptionsArray = (string[])val.GetType().GetProperty("Value").GetValue(val);
|
||||
|
||||
if (excludeObjectTypeOptionsArray != null)
|
||||
{
|
||||
foreach(string objectTypeValue in excludeObjectTypeOptionsArray)
|
||||
{
|
||||
ObjectType objectTypeName = new ObjectType();
|
||||
|
||||
if (objectTypeValue != null && Enum.TryParse(objectTypeValue, ignoreCase: true, out objectTypeName))
|
||||
{
|
||||
finalExcludeObjects.Add(objectTypeName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format($"{objectTypeValue} is not part of ObjectTypes enum"));
|
||||
}
|
||||
}
|
||||
// set final values to excludeObjectType property
|
||||
prop.SetValue(dacOptions, finalExcludeObjects.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
// BooleanOptionsDictionary has all the deployment options and is being processed separately in the second iteration by collecting here
|
||||
if (deployOptionsProp.Name == nameof(deploymentOptions.BooleanOptionsDictionary))
|
||||
{
|
||||
booleanOptionsDictionary = deploymentOptions.BooleanOptionsDictionary as Dictionary<string, DeploymentOptionProperty<bool>>;
|
||||
}
|
||||
}
|
||||
|
||||
// Iterating through the updated boolean options coming from the booleanOptionsDictionary and assigning them to DacDeployOptions
|
||||
foreach (KeyValuePair<string, DeploymentOptionProperty<bool>> deployOptionsProp in booleanOptionsDictionary)
|
||||
{
|
||||
var prop = propType.GetProperty(deployOptionsProp.Key);
|
||||
if (prop != null)
|
||||
{
|
||||
var selectedVal = deployOptionsProp.Value.Value;
|
||||
prop.SetValue(dacOptions, selectedVal);
|
||||
}
|
||||
}
|
||||
return dacOptions;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, string.Format("Schema compare create options model failed: {0}", e.Message));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
internal static DiffEntry CreateDiffEntry(SchemaDifference difference, DiffEntry parent)
|
||||
{
|
||||
if (difference == null)
|
||||
|
||||
Reference in New Issue
Block a user