Enabling Database scoped configurations tab to DB Properties (#2147)

* sending dsc values to ADS

* modifying dsc method with unsupportable property IsValuedefault

* getting the options and added a bool flag to maintian checkbox for secondary to save

* sending data to ads

* Ready for PR with minimal changes of loading UI as expected, TODO:saving logic

* Excluding maxdop and resumable options from primary value conversion for 1/0's

* Adding Id to the info, as we cannot depend on names, as names can be altered in future

* saving successfully, todo-diff servers, script (secondary - primary compare and dont update),test, send null for unsupported

* adding nullable dsc for unsupported servers

* fixing script generation for some properties that are not touched. the generated script is unharmed but unnecessary here

* adding test conditions for database scoped configurations

* adding switch case method to get the values

* Removing Loc string for the TSQL options

* removing unnecessary using statement

* Adding test case and fixing createDatabase issue

* Update src/Microsoft.SqlTools.ServiceLayer/Admin/Database/DatabasePrototype130.cs

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>

* comment update

---------

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>
This commit is contained in:
Sai Avishkar Sreerama
2023-08-15 11:58:35 -05:00
committed by GitHub
parent 7c0da6b6b1
commit e4db70fb39
15 changed files with 281 additions and 71 deletions

View File

@@ -23,7 +23,7 @@ using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
{
/// <summary>
/// <summary>
/// Database object type handler
/// </summary>
public class DatabaseHandler : ObjectTypeHandler<DatabaseInfo, DatabaseViewContext>
@@ -44,8 +44,12 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
internal static readonly string[] AzureEditionNames;
internal static readonly string[] AzureBackupLevels;
internal static readonly string[] DscOnOffOptions;
internal static readonly string[] DscElevateOptions;
internal static readonly string[] DscEnableDisableOptions;
internal static readonly AzureEditionDetails[] AzureMaxSizes;
internal static readonly AzureEditionDetails[] AzureServiceLevels;
internal DatabaseScopedConfigurationCollection? databaseScopedConfigurationsCollection = null;
static DatabaseHandler()
{
@@ -75,6 +79,22 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
displayRestrictAccessOptions.Add(DatabaseUserAccess.Single, SR.prototype_db_prop_restrictAccess_value_single);
displayRestrictAccessOptions.Add(DatabaseUserAccess.Restricted, SR.prototype_db_prop_restrictAccess_value_restricted);
DscOnOffOptions = new[]{
CommonConstants.DatabaseScopedConfigurations_Value_On,
CommonConstants.DatabaseScopedConfigurations_Value_Off
};
DscElevateOptions = new[]{
CommonConstants.DatabaseScopedConfigurations_Value_Off,
CommonConstants.DatabaseScopedConfigurations_Value_When_supported,
CommonConstants.DatabaseScopedConfigurations_Value_Fail_Unsupported
};
DscEnableDisableOptions = new[]{
CommonConstants.DatabaseScopedConfigurations_Value_Enabled,
CommonConstants.DatabaseScopedConfigurations_Value_Disabled
};
// Set up maps from displayName to enum type so we can retrieve the equivalent enum types later when getting a Save/Script request.
// We can't use a simple Enum.Parse for that since the displayNames get localized.
foreach (CompatibilityLevel key in displayCompatLevels.Keys)
@@ -160,7 +180,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
AutoShrink = smoDatabase.AutoShrink,
AutoUpdateStatistics = smoDatabase.AutoUpdateStatisticsEnabled,
AutoUpdateStatisticsAsynchronously = smoDatabase.AutoUpdateStatisticsAsync,
EncryptionEnabled = smoDatabase.EncryptionEnabled
EncryptionEnabled = smoDatabase.EncryptionEnabled,
DatabaseScopedConfigurations = smoDatabase.IsSupportedObject<DatabaseScopedConfiguration>() ? GetDSCMetaData(smoDatabase.DatabaseScopedConfigurations) : null
};
if (!isAzureDB)
@@ -187,7 +208,11 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
((DatabaseInfo)databaseViewInfo.ObjectInfo).IsLedgerDatabase = smoDatabase.IsLedger;
}
}
databaseScopedConfigurationsCollection = smoDatabase.IsSupportedObject<DatabaseScopedConfiguration>() ? smoDatabase.DatabaseScopedConfigurations : null;
}
databaseViewInfo.DscOnOffOptions = DscOnOffOptions;
databaseViewInfo.DscElevateOptions = DscElevateOptions;
databaseViewInfo.DscEnableDisableOptions = DscEnableDisableOptions;
}
// azure sql db doesn't have a sysadmin fixed role
@@ -569,6 +594,36 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
db110.DatabaseContainmentType = containmentTypeEnums[database.ContainmentType];
}
}
if (prototype is DatabasePrototype130 db130)
{
if (!viewParams.IsNewObject && databaseScopedConfigurationsCollection != null && database.DatabaseScopedConfigurations != null)
{
foreach (DatabaseScopedConfigurationsInfo dsc in database.DatabaseScopedConfigurations)
{
foreach (DatabaseScopedConfiguration smoDscCollection in databaseScopedConfigurationsCollection)
{
if (smoDscCollection.Name == dsc.Name)
{
smoDscCollection.Value = dsc.ValueForPrimary == CommonConstants.DatabaseScopedConfigurations_Value_Enabled
? "1" : dsc.ValueForPrimary == CommonConstants.DatabaseScopedConfigurations_Value_Disabled
? "0" : dsc.ValueForPrimary;
// When sending the DSC seconday value to ADS, we convert the secondaryValue of 'PRIMARY' to match with primaryValue
// We need to set it back to 'PRIMARY' so that SMO would not generate any unnecessary scripts for unchanged properties
if (!(smoDscCollection.ValueForSecondary == CommonConstants.DatabaseScopedConfigurations_Value_Primary &&
dsc.ValueForPrimary.Equals(dsc.ValueForSecondary)))
{
smoDscCollection.ValueForSecondary = dsc.ValueForSecondary == CommonConstants.DatabaseScopedConfigurations_Value_Enabled
? "1" : dsc.ValueForSecondary == CommonConstants.DatabaseScopedConfigurations_Value_Disabled
? "0" : dsc.ValueForSecondary;
}
break;
}
}
}
db130.DatabaseScopedConfiguration = databaseScopedConfigurationsCollection;
}
}
// AutoCreateStatisticsIncremental can only be set when AutoCreateStatistics is enabled
prototype.AutoCreateStatisticsIncremental = database.AutoCreateIncrementalStatistics;
@@ -985,5 +1040,51 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
}
return sizes.ToArray();
}
/// <summary>
/// Prepares database scoped configurations list
/// </summary>
/// <param name="smoDSCMetaData"></param>
/// <returns>database scoped configurations metadata array</returns>
private static DatabaseScopedConfigurationsInfo[] GetDSCMetaData(DatabaseScopedConfigurationCollection smoDSCMetaData)
{
var dscMetaData = new List<DatabaseScopedConfigurationsInfo>();
foreach (DatabaseScopedConfiguration dsc in smoDSCMetaData)
{
string primaryValue = GetDscValue(dsc.Id, dsc.Value);
dscMetaData.Add(new DatabaseScopedConfigurationsInfo()
{
Id = dsc.Id,
Name = dsc.Name,
ValueForPrimary = primaryValue,
ValueForSecondary = dsc.ValueForSecondary == CommonConstants.DatabaseScopedConfigurations_Value_Primary ? primaryValue : GetDscValue(dsc.Id, dsc.ValueForSecondary)
});
}
return dscMetaData.ToArray();
}
/// <summary>
/// Gets primary and secondary value of the database scoped configuration property
/// </summary>
/// <param name="dsc"></param>
/// <returns>Value of the primary/secondary</returns>
private static string GetDscValue(int id, string value)
{
// MAXDOP(Id = 1) and PAUSED_RESUMABLE_INDEX_ABORT_DURATION_MINUTES(Id = 25) are integer numbers but coming as string value type and they need to send as is.
if (id == 1 || id == 25)
{
return value;
}
switch (value)
{
case "1":
return CommonConstants.DatabaseScopedConfigurations_Value_Enabled;
case "0":
return CommonConstants.DatabaseScopedConfigurations_Value_Disabled;
default:
return value;
}
}
}
}

View File

@@ -39,5 +39,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
public bool? DatabaseReadOnly { get; set; }
public bool EncryptionEnabled { get; set; }
public string? RestrictAccess { get; set; }
public DatabaseScopedConfigurationsInfo[]? DatabaseScopedConfigurations { get; set; }
}
public class DatabaseScopedConfigurationsInfo
{
public int Id { get; set; }
public string Name { get; set; }
public string ValueForPrimary { get; set; }
public string ValueForSecondary { get; set; }
}
}

View File

@@ -25,6 +25,9 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
public AzureEditionDetails[] AzureMaxSizes { get; set; }
public string[] PageVerifyOptions { get; set; }
public string[] RestrictAccessOptions { get; set; }
public string[] DscOnOffOptions { get; set; }
public string[] DscElevateOptions { get; set; }
public string[] DscEnableDisableOptions { get; set; }
}
public class AzureEditionDetails