Skip to content

Commit

Permalink
Changes to SqlDatabaseRecoveryModel
Browse files Browse the repository at this point in the history
- Added en-US localization (issue #609).
  • Loading branch information
johlju committed Apr 26, 2019
1 parent a15089f commit dc4b938
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
- Changes to SqlServerLogin
- Added en-US localization ([issue #615](https://github.com/PowerShell/SqlServerDsc/issues/615)).
- Added unit tests to improved code coverage.
- Changes to SqlDatabaseRecoveryModel
- Added en-US localization ([issue #609](https://github.com/PowerShell/SqlServerDsc/issues/609)).

## 12.4.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath '
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlDatabaseRecoveryModel'

<#
.SYNOPSIS
This function gets all Key properties defined in the resource schema file
.PARAMETER Database
.PARAMETER Name
This is the SQL database
.PARAMETER RecoveryModel
Expand Down Expand Up @@ -51,41 +53,42 @@ function Get-TargetResource
$Name
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Write-Verbose -Message (
$script:localizedData.GetRecoveryModel -f $Name, $InstanceName
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message "Getting RecoveryModel of SQL database '$Name'"
$sqlDatabaseObject = $sqlServerObject.Databases[$Name]

if ($sqlDatabaseObject)
{
$sqlDatabaseRecoveryModel = $sqlDatabaseObject.RecoveryModel
New-VerboseMessage -Message "The current recovery model used by database $Name is '$sqlDatabaseRecoveryModel'"

Write-Verbose -Message (
$script:localizedData.CurrentRecoveryModel -f $sqlDatabaseRecoveryModel, $Name
)
}
else
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
$errorMessage = $script:localizedData.DatabaseNotFound -f $Name
New-InvalidResultException -Message $errorMessage
}
}

$returnValue = @{
return @{
Name = $Name
RecoveryModel = $sqlDatabaseRecoveryModel
ServerName = $ServerName
InstanceName = $InstanceName
}

$returnValue
}

<#
.SYNOPSIS
This function gets all Key properties defined in the resource schema file
.PARAMETER Database
.PARAMETER Name
This is the SQL database
.PARAMETER RecoveryModel
Expand Down Expand Up @@ -125,26 +128,29 @@ function Set-TargetResource
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

if ($sqlServerObject)
{
Write-Verbose -Message "Setting RecoveryModel of SQL database '$Name'"
$sqlDatabaseObject = $sqlServerObject.Databases[$Name]
Write-Verbose -Message (
$script:localizedData.SetRecoveryModel -f $Name
)

$sqlDatabaseObject = $sqlServerObject.Databases[$Name]
if ($sqlDatabaseObject)
{
if ($sqlDatabaseObject.RecoveryModel -ne $RecoveryModel)
{
$sqlDatabaseObject.RecoveryModel = $RecoveryModel
$sqlDatabaseObject.Alter()
New-VerboseMessage -Message "The recovery model for the database $Name is changed to '$RecoveryModel'."

Write-Verbose -Message (
$script:localizedData.ChangeRecoveryModel -f $Name, $RecoveryModel
)
}
}
else
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
$errorMessage = $script:localizedData.DatabaseNotFound -f $Name
New-InvalidResultException -Message $errorMessage
}
}
}
Expand All @@ -153,7 +159,7 @@ function Set-TargetResource
.SYNOPSIS
This function gets all Key properties defined in the resource schema file
.PARAMETER Database
.PARAMETER Name
This is the SQL database
.PARAMETER RecoveryModel
Expand Down Expand Up @@ -193,7 +199,9 @@ function Test-TargetResource
$Name
)

Write-Verbose -Message "Testing RecoveryModel of database '$Name'"
Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name, $InstanceName
)

$currentValues = Get-TargetResource @PSBoundParameters

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ConvertFrom-StringData @'
GetRecoveryModel = Get the current recovery model of the database '{0}' in the instance '{1}'.
CurrentRecoveryModel = The current recovery model is '{0}' for the database '{1}'.
DatabaseNotFound = The database '{0}' does not exist.
SetRecoveryModel = Setting the recovery model of database '{0}'.
ChangeRecoveryModel = The recovery model for the database '{0}' was changed to '{1}'.
TestingConfiguration = Determines if the database '{0}' on the instance '{1}' has the desired recovery model.
'@
10 changes: 4 additions & 6 deletions Tests/Unit/MSFT_SqlDatabaseRecoveryModel.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ try
RecoveryModel = 'Full'
}

$throwInvalidOperation = ("Database 'UnknownDatabase' does not exist " + `
"on SQL server 'localhost\MSSQLSERVER'.")
$errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Name

{ Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
{ Get-TargetResource @testParameters } | Should -Throw $errorMessage
}

It 'Should call the mock function Connect-SQL' {
Expand Down Expand Up @@ -224,10 +223,9 @@ try
RecoveryModel = 'Full'
}

$throwInvalidOperation = ("Database 'UnknownDatabase' does not exist " + `
"on SQL server 'localhost\MSSQLSERVER'.")
$errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Name

{ Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
{ Set-TargetResource @testParameters } | Should -Throw $errorMessage
}

It 'Should call the mock function Connect-SQL' {
Expand Down

0 comments on commit dc4b938

Please sign in to comment.