Skip to content

Commit

Permalink
SqlAgDatabase: Fixes Issue #1743 (#1744)
Browse files Browse the repository at this point in the history
- Changes to SqlAGDatabase
   - Added StatementTimeout optional parameter with default value of 600 seconds (10 mins) to SqlAGDatabase to fix Issue#1743
     Users will be able to specify the backup and restore timeout with it.
  • Loading branch information
dlenkov authored Apr 26, 2022
1 parent d8b4d9d commit a9850f2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Changes to SqlAGDatabase
- Added StatementTimeout optional parameter with default value of 600 seconds (10 mins) to SqlAGDatabase to fix Issue#1743
Users will be able to specify the backup and restore timeout with it.

### Removed

- The deprecated DSC resource SqlDatabaseOwner have been removed _(and replaced_
Expand Down
27 changes: 21 additions & 6 deletions source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ function Get-TargetResource
.PARAMETER ProcessOnlyOnActiveNode
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
Not used in Set-TargetResource.
.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10mins).
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -198,13 +201,18 @@ function Set-TargetResource

[Parameter()]
[System.Boolean]
$ProcessOnlyOnActiveNode
$ProcessOnlyOnActiveNode,

[Parameter()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600
)

Import-SQLPSModule

# Connect to the defined instance
$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -StatementTimeout $StatementTimeout

# Get the Availability Group
$availabilityGroup = $serverObject.AvailabilityGroups[$AvailabilityGroupName]
Expand Down Expand Up @@ -576,7 +584,6 @@ function Set-TargetResource
$restoreLogQueryStringBuilder.AppendLine() | Out-Null
$restoreLogQueryStringBuilder.Append('REVERT') | Out-Null
}

$restoreLogQueryString = $restoreLogQueryStringBuilder.ToString()
}

Expand All @@ -592,8 +599,8 @@ function Set-TargetResource
if ( $availabilityGroupReplica.SeedingMode -eq 'MANUAL')
{
# Restore the database
Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreDatabaseQueryString -StatementTimeout 0
Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreLogQueryString -StatementTimeout 0
Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreDatabaseQueryString -StatementTimeout $StatementTimeout
Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreLogQueryString -StatementTimeout $StatementTimeout
}

# Add the database to the Availability Group
Expand Down Expand Up @@ -714,6 +721,9 @@ function Set-TargetResource
.PARAMETER ProcessOnlyOnActiveNode
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10mins).
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -760,7 +770,12 @@ function Test-TargetResource

[Parameter()]
[System.Boolean]
$ProcessOnlyOnActiveNode
$ProcessOnlyOnActiveNode,

[Parameter()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600
)

$configurationInDesiredState = $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class DSC_SqlAGDatabase : OMI_BaseResource
[Write, Description("If set to `$true`, this ensures the database owner of the database on the primary replica is the owner of the database on all secondary replicas. This requires the database owner is available as a login on all replicas and that the **PsDscRunAsCredential** has _impersonate any login_, _control server_, _impersonate login_, or _control login_ permissions. If set to `$false`, the owner of the database will be the username specified in **PsDscRunAsCredential**. The default is `$false`.")] Boolean MatchDatabaseOwner;
[Write, Description("If set to `$true`, this adds the restore option `WITH REPLACE`. If set to `$false`, existing databases and files will block the restore and throw error. The default is `$false`.")] Boolean ReplaceExisting;
[Write, Description("Specifies that the resource will only determine if a change is needed if the target node is the active host of the _SQL Server_ instance.")] Boolean ProcessOnlyOnActiveNode;
[Write, Description("Set the query timeout in seconds for the backup and restore operations. The default is 600 seconds (10mins).")] SInt32 StatementTimeout;
[Read, Description("Returns if the current node is actively hosting the _SQL Server_ instance.")] Boolean IsActiveNode;
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ PARAMETER MatchDatabaseOwner
The default is '$false'.

.PARAMETER ProcessOnlyOnActiveNode
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.

.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10mins).
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<#
.DESCRIPTION
This example shows how to ensure that the databases 'DB*' and 'AdventureWorks'
are members in the Availability Group 'TestAG'.
are members of the Availability Group 'TestAG'.
In the event this is applied to a Failover Cluster Instance (FCI), the
ProcessOnlyOnActiveNode property will tell the Test-TargetResource function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<#
.DESCRIPTION
This example shows how to ensure that the databases 'DB*' and 'AdventureWorks'
'are the only members of the Availability Group 'TestAG'.
are the only members of the Availability Group 'TestAG'.
#>

Configuration Example
Expand Down

0 comments on commit a9850f2

Please sign in to comment.