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:
Sakshi Sharma
2023-04-10 17:31:39 -07:00
committed by GitHub
parent 7bf370a4a5
commit 9aa84f517c
10 changed files with 122 additions and 85 deletions

View File

@@ -22,7 +22,7 @@
<PackageReference Update="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version="1.1.1" />
<PackageReference Update="Microsoft.SqlServer.Management.SmoMetadataProvider" Version="170.12.0" />
<PackageReference Update="Microsoft.SqlServer.DacFx" Version="161.8406.0-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx" Version="162.0.15-preview" />
<PackageReference Update="Microsoft.SqlServer.DacFx.Projects" Version="161.8442.0-alpha" />
<PackageReference Update="Microsoft.Azure.Kusto.Data" Version="9.0.4" />
<PackageReference Update="Microsoft.Azure.Kusto.Language" Version="9.0.4" />

View File

@@ -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);

View 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;
}
}
}
}

View File

@@ -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)
{

View File

@@ -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)
{

View File

@@ -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

View File

@@ -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);

View File

@@ -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)

View File

@@ -882,6 +882,17 @@ Streaming query statement contains a reference to missing output stream 'Missing
string profileFilePath = Path.Combine(folderPath, fileName);
string expectedFile = Path.Combine(publishProfileFolder, fileName);
DeploymentOptions deploymentOptions = DeploymentOptions.GetDefaultPublishOptions();
// a few extra options get added in the publish.xml file, because of the defaults that are set in STS for maintaining compatibility with ADS and SSDT
// these defaults are defined in ..\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\DacFx\Contracts\DeploymentOptions.cs
deploymentOptions.ExcludeObjectTypes = new DeploymentOptionProperty<string[]>(new[] { nameof(ObjectType.Views) });
deploymentOptions.BooleanOptionsDictionary[nameof(DacDeployOptions.CreateNewDatabase)].Value = true;
deploymentOptions.BooleanOptionsDictionary[nameof(DacDeployOptions.DropConstraintsNotInSource)].Value = false;
deploymentOptions.BooleanOptionsDictionary[nameof(DacDeployOptions.ScriptFileSize)].Value = true;
deploymentOptions.BooleanOptionsDictionary[nameof(DacDeployOptions.VerifyDeployment)].Value = false;
deploymentOptions.BooleanOptionsDictionary[nameof(DacDeployOptions.AllowIncompatiblePlatform)].Value = true;
var savePublishProfileParams = new SavePublishProfileParams
{
ProfilePath = profileFilePath,
@@ -890,7 +901,8 @@ Streaming query statement contains a reference to missing output stream 'Missing
SqlCommandVariableValues = new Dictionary<string, string>()
{
{ "testvar", "testval" }
}
},
DeploymentOptions = deploymentOptions
};
MockRequest<ResultStatus> requestMock = new();

View File

@@ -3,6 +3,18 @@
<PropertyGroup>
<TargetDatabaseName>testDb</TargetDatabaseName>
<TargetConnectionString>testConnString</TargetConnectionString>
<AllowIncompatiblePlatform>True</AllowIncompatiblePlatform>
<CreateNewDatabase>True</CreateNewDatabase>
<DropConstraintsNotInSource>False</DropConstraintsNotInSource>
<DropPermissionsNotInSource>True</DropPermissionsNotInSource>
<DropObjectsNotInSource>True</DropObjectsNotInSource>
<DropRoleMembersNotInSource>True</DropRoleMembersNotInSource>
<IgnoreKeywordCasing>False</IgnoreKeywordCasing>
<IgnoreSemicolonBetweenStatements>False</IgnoreSemicolonBetweenStatements>
<ScriptFileSize>True</ScriptFileSize>
<VerifyDeployment>False</VerifyDeployment>
<AllowDropBlockingAssemblies>True</AllowDropBlockingAssemblies>
<ExcludeViews>True</ExcludeViews>
<ProfileVersionNumber>1</ProfileVersionNumber>
</PropertyGroup>
<ItemGroup>