Skip to content

Commit

Permalink
xSQLServerDatabase: Add Collation as a property when defining a Datab…
Browse files Browse the repository at this point in the history
…ase (#850)

- Changes to xSQLServerDatabase
  - Added parameter to specify collation for a database to be different from server
    collation (issue #767).
  - Fixed unit tests for Get-TargetResource to ensure correctly testing return
    values (issue #849)
  • Loading branch information
ChrisLGardner authored and johlju committed Oct 8, 2017
1 parent fa87101 commit c99891f
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Changes to xSQLServerDatabase
- Added parameter to specify collation for a database to be different from server
collation ([issue #767](https://github.com/PowerShell/xSQLServer/issues/767)).
- Fixed unit tests for Get-TargetResource to ensure correctly testing return
values ([issue #849](https://github.com/PowerShell/xSQLServer/issues/849))

## 8.2.0.0

- Changes to xSQLServer
Expand Down
107 changes: 91 additions & 16 deletions DSCResources/MSFT_xSQLServerDatabase/MSFT_xSQLServerDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Pare
.PARAMETER SQLInstanceName
The name of the SQL instance to be configured.
.PARAMETER Collation
The name of the SQL collation to use for the new database.
Defaults to server collation.
#>

function Get-TargetResource
Expand Down Expand Up @@ -44,13 +48,19 @@ function Get-TargetResource
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$SQLInstanceName
$SQLInstanceName,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Collation
)

$sqlServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName

if ($sqlServerObject)
{
$sqlDatabaseCollation = $sqlServerObject.Collation
Write-Verbose -Message 'Getting SQL Databases'
# Check database exists
$sqlDatabaseObject = $sqlServerObject.Databases[$Name]
Expand All @@ -59,6 +69,7 @@ function Get-TargetResource
{
Write-Verbose -Message "SQL Database name $Name is present"
$Ensure = 'Present'
$sqlDatabaseCollation = $sqlDatabaseObject.Collation
}
else
{
Expand All @@ -72,6 +83,7 @@ function Get-TargetResource
Ensure = $Ensure
SQLServer = $SQLServer
SQLInstanceName = $SQLInstanceName
Collation = $sqlDatabaseCollation
}

$returnValue
Expand All @@ -93,6 +105,10 @@ function Get-TargetResource
.PARAMETER SQLInstanceName
The name of the SQL instance to be configured.
.PARAMETER Collation
The name of the SQL collation to use for the new database.
Defaults to server collation.
#>
function Set-TargetResource
{
Expand All @@ -118,31 +134,71 @@ function Set-TargetResource
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$SQLInstanceName
$SQLInstanceName,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Collation
)

$sqlServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName

if ($sqlServerObject)
{

if ($Ensure -eq 'Present')
{
try
if (-not $PSBoundParameters.ContainsKey('Collation'))
{
$Collation = $sqlServerObject.Collation
}
elseif ($Collation -notin $sqlServerObject.EnumCollations().Name)
{
throw New-TerminatingError -ErrorType InvalidCollationError `
-FormatArgs @($SQLServer, $SQLInstanceName, $Name, $Collation) `
-ErrorCategory InvalidOperation
}

$sqlDatabaseObject = $sqlServerObject.Databases[$Name]

if ($sqlDatabaseObject)
{
$sqlDatabaseObjectToCreate = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -ArgumentList $sqlServerObject, $Name
if ($sqlDatabaseObjectToCreate)
try
{
Write-Verbose -Message "Updating the database $Name with specified settings."
$sqlDatabaseObject.Collation = $Collation
$sqlDatabaseObject.Alter()
New-VerboseMessage -Message "Updated Database $Name."
}
catch
{
Write-Verbose -Message "Adding to SQL the database $Name"
$sqlDatabaseObjectToCreate.Create()
New-VerboseMessage -Message "Created Database $Name"
throw New-TerminatingError -ErrorType UpdateDatabaseSetError `
-FormatArgs @($SQLServer, $SQLInstanceName, $Name) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
}
}
catch
else
{
throw New-TerminatingError -ErrorType CreateDatabaseSetError `
-FormatArgs @($SQLServer, $SQLInstanceName, $Name) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
try
{
$sqlDatabaseObjectToCreate = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -ArgumentList $sqlServerObject, $Name
if ($sqlDatabaseObjectToCreate)
{
Write-Verbose -Message "Adding to SQL the database $Name."
$sqlDatabaseObjectToCreate.Collation = $Collation
$sqlDatabaseObjectToCreate.Create()
New-VerboseMessage -Message "Created Database $Name."
}
}
catch
{
throw New-TerminatingError -ErrorType CreateDatabaseSetError `
-FormatArgs @($SQLServer, $SQLInstanceName, $Name) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
}
}
}
else
Expand All @@ -152,9 +208,9 @@ function Set-TargetResource
$sqlDatabaseObjectToDrop = $sqlServerObject.Databases[$Name]
if ($sqlDatabaseObjectToDrop)
{
Write-Verbose -Message "Deleting to SQL the database $Name"
Write-Verbose -Message "Deleting to SQL the database $Name."
$sqlDatabaseObjectToDrop.Drop()
New-VerboseMessage -Message "Dropped Database $Name"
New-VerboseMessage -Message "Dropped Database $Name."
}
}
catch
Expand Down Expand Up @@ -184,6 +240,10 @@ function Set-TargetResource
.PARAMETER SQLInstanceName
The name of the SQL instance to be configured.
.PARAMETER Collation
The name of the SQL collation to use for the new database.
Defaults to server collation.
#>
function Test-TargetResource
{
Expand All @@ -210,14 +270,24 @@ function Test-TargetResource
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$SQLInstanceName
$SQLInstanceName,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Collation
)

Write-Verbose -Message "Checking if database named $Name is present or absent"

$getTargetResourceResult = Get-TargetResource @PSBoundParameters
$isDatabaseInDesiredState = $true

if (-not $PSBoundParameters.ContainsKey('Collation'))
{
$Collation = $getTargetResourceResult.Collation
}

switch ($Ensure)
{
'Absent'
Expand All @@ -236,6 +306,11 @@ function Test-TargetResource
New-VerboseMessage -Message "Ensure is set to Present. The database $Name should be created"
$isDatabaseInDesiredState = $false
}
elseif ($getTargetResourceResult.Collation -ne $Collation)
{
New-VerboseMessage -Message 'Database exist but has the wrong collation.'
$isDatabaseInDesiredState = $false
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class MSFT_xSQLServerDatabase : OMI_BaseResource
[Write, Description("An enumerated value that describes if the database is added (Present) or dropped (Absent). Valid values are 'Present' or 'Absent'. Default Value is 'Present'."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Key, Description("The host name of the SQL Server to be configured.")] String SQLServer;
[Key, Description("The name of the SQL instance to be configured.")] String SQLInstanceName;
[Write, Description("The name of the SQL collation to use for the new database. Defaults to server collation.")] String Collation;
};
12 changes: 12 additions & 0 deletions Examples/Resources/xSQLServerDatabase/1-CreateDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.EXAMPLE
This example shows how to create a database with
the database name equal to 'Contoso'.
The second example shows how to create a database
with a different collation.
#>
Configuration Example
{
Expand All @@ -24,5 +27,14 @@ Configuration Example
SQLInstanceName = 'DSC'
Name = 'Contoso'
}

xSQLServerDatabase Create_Database_with_different_collation
{
Ensure = 'Present'
SQLServer = 'SQLServer'
SQLInstanceName = 'DSC'
Name = 'AdventureWorks'
Collation = 'SQL_Latin1_General_Pref_CP850_CI_AS'
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ database, please read:
* **`[String]` SQLServer** _(Key)_: The host name of the SQL Server to be configured.
* **`[String]` SQLInstanceName** _(Key)_: The name of the SQL instance to be configured.
* **`[String]` Name** _(Key)_: The name of database to be created or dropped.
* **`[String]` Collation** _(Write)_: The name of the SQL collation to use
for the new database. Defaults to server collation.
* **`[String]` Ensure** _(Write)_: When set to 'Present', the database will be created.
When set to 'Absent', the database will be dropped. { *Present* | Absent }.

Expand Down
Loading

0 comments on commit c99891f

Please sign in to comment.