Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Sql/Sql.Test/ScenarioTests/ServerCrudTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ function Test-CreateServerWithDefaultSoftDeleteRetentionEnabled
}
finally
{
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -SoftDeleteRetentionDays 0
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -EnableSoftDelete $False
Remove-ResourceGroupForTest $rg
}
}
Expand Down Expand Up @@ -731,7 +731,7 @@ function Test-CreateServerWithCustomSoftDeleteRetentionEnabled
}
finally
{
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -SoftDeleteRetentionDays 0
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -EnableSoftDelete $False
Remove-ResourceGroupForTest $rg
}
}
Expand Down Expand Up @@ -768,7 +768,7 @@ function Test-UpdateServerWithSoftDeleteRetentionEnabled
}
finally
{
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -SoftDeleteRetentionDays 0
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -EnableSoftDelete $False
Remove-ResourceGroupForTest $rg
}
}
Expand Down Expand Up @@ -798,7 +798,7 @@ function Test-RestoreDeletedServer
}
finally
{
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -SoftDeleteRetentionDays 0
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -EnableSoftDelete $False
Remove-ResourceGroupForTest $rg
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Improved parameter validation for the EnableSoftDelete and SoftDeleteRetentionDays parameters.
* Updated `New-AzSqlServer` to support soft-delete retention
- Added `EnableSoftDelete` parameter to `New-AzSqlServer` to enable creation of a server with soft-delete retention
- Added `SoftDeleteRetentionDays` parameter to `New-AzSqlServer` to set the soft-delete retention period (in days)
Expand Down
19 changes: 14 additions & 5 deletions src/Sql/Sql/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/Sql/Sql/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@
<value>SQL Administrator Credentials are only optional when Azure Active Directory Only Authentication is enabled via -EnableActiveDirectoryOnlyAuthentication.</value>
</data>
<data name="MissingEnableSoftDelete" xml:space="preserve">
<value>Invalid configuration: SoftDeleteRetentionDays can only be set to a value greater than 0 if EnableSoftDelete is set to true.</value>
<value>SoftDeleteRetentionDays parameter can only be specified if EnableSoftDelete is explicitly specified.</value>
</data>
<data name="RemoveAzureSqlServerOutboundFirewallRuleDescription" xml:space="preserve">
<value>Permanently removing allowed FQDN '{0}' from the list of Outbound Firewall Rules (Allowed FQDNs) for Azure Sql Database Server '{1}'.</value>
Expand Down Expand Up @@ -770,9 +770,12 @@
<value>Changing the service tier to Hyperscale also converts the geo-secondary replica to Hyperscale. For more information, see https://go.microsoft.com/fwlink/?linkid=2314103</value>
</data>
<data name="DeletedServerNotFound" xml:space="preserve">
<value>No deleted server named '{0}' found in resource group '{1}' that can be restored.</value>
<value>No deleted server named '{0}' found in location '{1}' that can be restored.</value>
</data>
<data name="InvalidSoftDeleteRetentionDays" xml:space="preserve">
<value>Invalid configuration: SoftDeleteRetentionDays can only be set to 0 when EnableSoftDelete is false.</value>
<data name="InvalidSoftDeleteRetentionDaysForDisablingSoftDelete" xml:space="preserve">
<value>SoftDeleteRetentionDays can only be set to 0 when EnableSoftDelete is false.</value>
</data>
<data name="InvalidSoftDeleteRetentionDaysRange" xml:space="preserve">
<value>SoftDeleteRetentionDays must be between 1 and 35 when EnableSoftDelete is true.</value>
</data>
</root>
43 changes: 28 additions & 15 deletions src/Sql/Sql/Server/Cmdlet/NewAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public class NewAzureSqlServer : AzureSqlServerCmdletBase
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specify whether to enable soft-delete retention for the server. When enabled, a dropped server can be restored within the retention window (defaults to 7 days if not specified). To set a custom retention period use -SoftDeleteRetentionDays.")]
public bool EnableSoftDelete { get; set; }
[PSArgumentCompleter("true", "false")]
public bool? EnableSoftDelete { get; set; }

/// <summary>
/// Soft-delete retention days for the server
Expand All @@ -188,18 +189,27 @@ public override void ExecuteCmdlet()
throw new PSArgumentException(Properties.Resources.MissingSQLAdministratorCredentials, "SqlAdministratorCredentials");
}

// if the user specified a retention days value, then they must also enable soft delete
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDelete)
if (SoftDeleteRetentionDays.HasValue)
{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
}

// if the user specified 0 retention days, then they must not enable soft delete
if (this.EnableSoftDelete && this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays == 0)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDays, "SoftDeleteRetentionDays");
if (EnableSoftDelete == true)
{
if (SoftDeleteRetentionDays.Value < 1 || SoftDeleteRetentionDays.Value > 35)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysRange, "SoftDeleteRetentionDays");
}
}
else if (EnableSoftDelete == false)
{
if (SoftDeleteRetentionDays.Value != 0)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysForDisablingSoftDelete, "SoftDeleteRetentionDays");
}
}
else
{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
}
}

base.ExecuteCmdlet();
}

Expand Down Expand Up @@ -244,16 +254,19 @@ public override void ExecuteCmdlet()
}

int? softDeleteRetentionDays;
if (this.EnableSoftDelete)
if (this.EnableSoftDelete == true)
{
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
}
else if (this.EnableSoftDelete == false)
Comment on lines +257 to +262
Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in SetAzureSqlServer.cs - use direct boolean comparison instead of explicit comparison to true/false for nullable boolean values.

Suggested change
if (this.EnableSoftDelete == true)
{
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
}
else if (this.EnableSoftDelete == false)
if (this.EnableSoftDelete)
{
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
}
else if (!this.EnableSoftDelete)

Copilot uses AI. Check for mistakes.

Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in SetAzureSqlServer.cs - use direct boolean comparison instead of explicit comparison to true/false for nullable boolean values.

Suggested change
else if (this.EnableSoftDelete == false)
else if (this.EnableSoftDelete is false)

Copilot uses AI. Check for mistakes.

{
// If disabling, explicitly set retention to 0.
softDeleteRetentionDays = 0;
}
else
{
// If not enabling, only explicitly set retention to 0 when the caller provided 0.
// Otherwise, leave as null so the service preserves the existing retention setting.
softDeleteRetentionDays = (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays.Value == 0) ? 0 : (int?)null;
softDeleteRetentionDays = (int?)null;
}


Expand Down
6 changes: 3 additions & 3 deletions src/Sql/Sql/Server/Cmdlet/RestoreAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class RestoreAzureSqlServer : AzureSqlServerCmdletBase
{
throw new PSArgumentException(
string.Format(Properties.Resources.DeletedServerNotFound,
this.ServerName, this.ResourceGroupName),
this.ServerName, this.Location),
"ServerName");
}

Expand All @@ -99,10 +99,10 @@ public class RestoreAzureSqlServer : AzureSqlServerCmdletBase
{
throw new PSArgumentException(
string.Format(Properties.Resources.DeletedServerNotFound,
this.ServerName, this.ResourceGroupName),
this.ServerName, this.Location),
"ServerName");
}

// Unexpected exception encountered
throw;
}
Expand Down
44 changes: 29 additions & 15 deletions src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
[Parameter(Mandatory = false,
HelpMessage = "Specify whether to enable soft-delete retention for the server. When enabled, a dropped server can be restored within the retention window (defaults to 7 days if not specified). To set a custom retention period use -SoftDeleteRetentionDays.")]
[PSArgumentCompleter("true", "false")]
public bool EnableSoftDelete { get; set; }
public bool? EnableSoftDelete { get; set; }

/// <summary>
/// Value for soft-delete retention days for the server.
Expand All @@ -152,18 +152,28 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
/// </summary>
public override void ExecuteCmdlet()
{
// if the user specified a retention days value, then they must also enable soft delete
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDelete)
if (SoftDeleteRetentionDays.HasValue)
{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
if (EnableSoftDelete == true)
{
if (SoftDeleteRetentionDays.Value < 1 || SoftDeleteRetentionDays.Value > 35)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysRange, "SoftDeleteRetentionDays");
}
}
else if (EnableSoftDelete == false)
{
if (SoftDeleteRetentionDays.Value != 0)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysForDisablingSoftDelete, "SoftDeleteRetentionDays");
}
}
else
{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
}
}

// if the user specified 0 retention days, then they must not enable soft delete
if (this.EnableSoftDelete && this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays == 0)
{
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDays, "SoftDeleteRetentionDays");
}


base.ExecuteCmdlet();
}
/// <summary>
Expand Down Expand Up @@ -204,16 +214,20 @@ public override void ExecuteCmdlet()
updateData[0].PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId ?? model.FirstOrDefault().PrimaryUserAssignedIdentityId;
updateData[0].KeyId = this.KeyId ?? updateData[0].KeyId;
updateData[0].FederatedClientId = this.FederatedClientId ?? updateData[0].FederatedClientId;
if (this.EnableSoftDelete)
if (this.EnableSoftDelete == true)
Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use direct boolean comparison instead of explicit comparison to true. This can be simplified to if (this.EnableSoftDelete == true) or better yet if (EnableSoftDelete.GetValueOrDefault()).

Suggested change
if (this.EnableSoftDelete == true)
if (this.EnableSoftDelete.GetValueOrDefault())

Copilot uses AI. Check for mistakes.

{
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
updateData[0].SoftDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
}
else if (this.EnableSoftDelete == false)
Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use direct boolean comparison instead of explicit comparison to false. This can be simplified to if (EnableSoftDelete.GetValueOrDefault(true) == false) or use a more explicit nullable check pattern.

Suggested change
else if (this.EnableSoftDelete == false)
else if (this.EnableSoftDelete.GetValueOrDefault(true) == false)

Copilot uses AI. Check for mistakes.

{
// if disabling soft-delete retention, set retention to 0 days.
updateData[0].SoftDeleteRetentionDays = 0;
}
else
{
// If not enabling, only explicitly set retention to 0 when the caller provided 0.
// Otherwise, leave as null so the service preserves the existing retention setting.
updateData[0].SoftDeleteRetentionDays = (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays.Value == 0) ? 0 : (int?)null;
// If EnableSoftDelete is not specified, retain existing retention value.
updateData[0].SoftDeleteRetentionDays = (int?)null;
}

return updateData;
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/help/Set-AzSqlServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ FederatedClientId :
SoftDeleteRetentionDays : 7
```

This command enables soft-delete retention on the Azure SQL Server named server01 (default 7 days). To specify the retention period in days, add the -SoftDeleteRetentionDays parameter.
This command enables soft-delete retention on the Azure SQL Server named server01, with a default retention period of 7 days. To customize the retention duration, use the SoftDeleteRetentionDays parameter.

### Example 4: Disable soft-delete retention for the server
```powershell
Expand Down