Skip to content

Commit 297d6b1

Browse files
authored
Set-SqlDscDatabaseProperty: Refactor command (#2327)
1 parent d6111a3 commit 297d6b1

20 files changed

+2789
-581
lines changed

CHANGELOG.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- BREAKING CHANGE: Removed public command `Test-SqlDscDatabase`. Use
1111
`Test-SqlDscIsDatabase` to check existence. For property checks, use
1212
`Test-SqlDscDatabaseProperty`. See [issue #2201](https://github.com/dsccommunity/SqlServerDsc/issues/2201).
13+
- BREAKING CHANGE: `Set-SqlDscDatabase`
14+
- Removed parameter `OwnerName` [issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177).
15+
Use the new command `Set-SqlDscDatabaseOwner` to change database ownership instead.
1316

1417
### Added
1518

19+
- Added public command `Set-SqlDscDatabaseOwner` to change the owner of a SQL Server
20+
database [issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177).
21+
This command uses the SMO `SetOwner()` method and supports both `ServerObject`
22+
and `DatabaseObject` parameter sets. This replaces the ownership changes
23+
previously done via the `OwnerName` parameter in `Set-SqlDscDatabase`.
1624
- Added public command `Test-SqlDscIsDatabase` to test if a database exists on a
1725
SQL Server Database Engine instance ([issue #2201](https://github.com/dsccommunity/SqlServerDsc/issues/2201)).
1826
- Added public command `Get-SqlDscSetupLog` to retrieve SQL Server setup bootstrap
@@ -220,6 +228,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
220228

221229
### Changed
222230

231+
- BREAKING CHANGE: `Set-SqlDscDatabase` has been renamed to `Set-SqlDscDatabaseProperty`
232+
to better reflect its purpose of setting database properties. All existing references
233+
should be updated to use the new name.
234+
- `Set-SqlDscDatabaseProperty` (formerly `Set-SqlDscDatabase`)
235+
- BREAKING CHANGE: Completely refactored to support settable SMO Database
236+
properties as parameters ([issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177)).
223237
- `Remove-SqlDscAgentAlert`
224238
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
225239
functions for proper terminating error handling ([issue #2193](https://github.com/dsccommunity/SqlServerDsc/issues/2193)).
@@ -235,7 +249,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
235249
- `New-SqlDscDatabase`
236250
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
237251
functions for proper terminating error handling ([issue #2200](https://github.com/dsccommunity/SqlServerDsc/issues/2200)).
238-
- `Set-SqlDscDatabase`
252+
- `Set-SqlDscDatabaseProperty` (formerly `Set-SqlDscDatabase`)
239253
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
240254
functions for proper terminating error handling ([issue #2198](https://github.com/dsccommunity/SqlServerDsc/issues/2198)).
241255
- `Add-SqlDscTraceFlag`

azure-pipelines.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ stages:
335335
'tests/Integration/Commands/Get-SqlDscDatabase.Integration.Tests.ps1'
336336
'tests/Integration/Commands/ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1'
337337
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
338-
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
338+
'tests/Integration/Commands/Get-SqlDscCompatibilityLevel.Integration.Tests.ps1'
339+
'tests/Integration/Commands/Set-SqlDscDatabaseProperty.Integration.Tests.ps1'
340+
'tests/Integration/Commands/Set-SqlDscDatabaseOwner.Integration.Tests.ps1'
339341
'tests/Integration/Commands/Test-SqlDscIsDatabase.Integration.Tests.ps1'
340342
'tests/Integration/Commands/Test-SqlDscDatabaseProperty.Integration.Tests.ps1'
341343
'tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1'
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<#
2+
.SYNOPSIS
3+
Gets the supported database compatibility levels for a SQL Server instance or version.
4+
5+
.DESCRIPTION
6+
This command returns the supported database compatibility levels for a SQL Server
7+
Database Engine instance or a specific SQL Server version.
8+
9+
The compatibility levels are determined based on the SQL Server version, following
10+
the official Microsoft documentation for supported compatibility level ranges.
11+
12+
.PARAMETER ServerObject
13+
Specifies the SQL Server connection object to get supported compatibility levels for.
14+
15+
.PARAMETER Version
16+
Specifies the SQL Server version to get supported compatibility levels for.
17+
Only the major version number is used for determining compatibility levels.
18+
19+
.EXAMPLE
20+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
21+
Get-SqlDscCompatibilityLevel -ServerObject $serverObject
22+
23+
Returns all supported compatibility levels for the connected SQL Server instance.
24+
25+
.EXAMPLE
26+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
27+
$serverObject | Get-SqlDscCompatibilityLevel
28+
29+
Returns all supported compatibility levels using pipeline input.
30+
31+
.EXAMPLE
32+
Get-SqlDscCompatibilityLevel -Version '16.0.1000.6'
33+
34+
Returns all supported compatibility levels for SQL Server 2022 (version 16).
35+
36+
.INPUTS
37+
Microsoft.SqlServer.Management.Smo.Server
38+
39+
The server object to get supported compatibility levels for.
40+
41+
.OUTPUTS
42+
System.String[]
43+
44+
Returns an array of supported compatibility level names (e.g., 'Version160', 'Version150').
45+
#>
46+
function Get-SqlDscCompatibilityLevel
47+
{
48+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
49+
[CmdletBinding(DefaultParameterSetName = 'ServerObject')]
50+
[OutputType([System.String[]])]
51+
param
52+
(
53+
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)]
54+
[Microsoft.SqlServer.Management.Smo.Server]
55+
$ServerObject,
56+
57+
[Parameter(ParameterSetName = 'Version', Mandatory = $true)]
58+
[System.Version]
59+
$Version
60+
)
61+
62+
process
63+
{
64+
# Get the major version based on parameter set
65+
$majorVersion = if ($PSCmdlet.ParameterSetName -eq 'ServerObject')
66+
{
67+
Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForInstance -f $ServerObject.InstanceName, $ServerObject.VersionMajor)
68+
$ServerObject.VersionMajor
69+
}
70+
else
71+
{
72+
Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForVersion -f $Version, $Version.Major)
73+
$Version.Major
74+
}
75+
76+
# Get all available compatibility levels from the enum
77+
$allCompatibilityLevels = [System.Enum]::GetNames([Microsoft.SqlServer.Management.Smo.CompatibilityLevel])
78+
79+
<#
80+
Determine minimum supported compatibility level based on SQL Server version
81+
Reference: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level
82+
#>
83+
$minimumCompatLevel = switch ($majorVersion)
84+
{
85+
{ $_ -ge 12 }
86+
{
87+
100 # SQL 2014 (v12) and later support minimum compat level 100
88+
}
89+
90+
11
91+
{
92+
90 # SQL 2012 (v11) supports minimum compat level 90
93+
}
94+
95+
{ $_ -le 10 }
96+
{
97+
80 # SQL 2008 R2 (v10.5) and earlier support minimum compat level 80
98+
}
99+
}
100+
101+
<#
102+
Filter compatibility levels that are supported by this SQL Server version
103+
CompatibilityLevel enum values are named like "Version80", "Version90", etc.
104+
SQL Server supports compatibility levels from the minimum up to (version * 10)
105+
#>
106+
$supportedCompatibilityLevels = $allCompatibilityLevels | Where-Object -FilterScript {
107+
if ($_ -match 'Version(\d+)')
108+
{
109+
$compatLevelVersion = [System.Int32] $Matches[1]
110+
($compatLevelVersion -ge $minimumCompatLevel) -and ($compatLevelVersion -le ($majorVersion * 10))
111+
}
112+
else
113+
{
114+
$false
115+
}
116+
}
117+
118+
<#
119+
Warn if SQL Server version is newer than what SMO library supports
120+
Check if the expected maximum compatibility level is missing from the supported list
121+
#>
122+
$expectedMaxCompatLevel = "Version$($majorVersion * 10)"
123+
if ($expectedMaxCompatLevel -notin $supportedCompatibilityLevels -and $supportedCompatibilityLevels.Count -gt 0)
124+
{
125+
# Get the actual maximum from the last element (they're in ascending order)
126+
$lastCompatLevel = $supportedCompatibilityLevels[-1]
127+
if ($lastCompatLevel -match 'Version(\d+)')
128+
{
129+
$maxCompatLevelInEnum = [System.Int32] $Matches[1]
130+
Write-Warning -Message ($script:localizedData.GetCompatibilityLevel_SmoTooOld -f $majorVersion, $maxCompatLevelInEnum, ($majorVersion * 10))
131+
}
132+
}
133+
134+
Write-Debug -Message ($script:localizedData.GetCompatibilityLevel_Found -f $supportedCompatibilityLevels.Count, $majorVersion)
135+
136+
return $supportedCompatibilityLevels
137+
}
138+
}

source/Public/Get-SqlDscDatabase.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ function Get-SqlDscDatabase
8888
}
8989
else
9090
{
91+
if ($Refresh.IsPresent)
92+
{
93+
# Refresh the database object
94+
$databaseObject.Refresh()
95+
}
96+
9197
Write-Verbose -Message ($script:localizedData.Database_Found -f $Name)
9298
}
9399
}

source/Public/New-SqlDscDatabase.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
The name of the SQL collation to use for the new database.
1616
Default value is server collation.
1717
18+
.PARAMETER CatalogCollation
19+
Specifies the collation type for the system catalog. Valid values are
20+
DATABASE_DEFAULT and SQL_Latin1_General_CP1_CI_AS. This property can
21+
only be set during database creation and cannot be modified afterward.
22+
This parameter requires SQL Server 2019 (version 15) or later.
23+
1824
.PARAMETER CompatibilityLevel
1925
The version of the SQL compatibility level to use for the new database.
2026
Default value is server version.
@@ -73,6 +79,10 @@ function New-SqlDscDatabase
7379
[System.String]
7480
$Collation,
7581

82+
[Parameter()]
83+
[Microsoft.SqlServer.Management.Smo.CatalogCollationType]
84+
$CatalogCollation,
85+
7686
[Parameter()]
7787
[ValidateSet('Version80', 'Version90', 'Version100', 'Version110', 'Version120', 'Version130', 'Version140', 'Version150', 'Version160')]
7888
[System.String]
@@ -177,6 +187,24 @@ function New-SqlDscDatabase
177187
}
178188
}
179189

190+
# Validate CatalogCollation if specified (requires SQL Server 2019+)
191+
if ($PSBoundParameters.ContainsKey('CatalogCollation'))
192+
{
193+
if ($ServerObject.VersionMajor -lt 15)
194+
{
195+
$errorMessage = $script:localizedData.Database_CatalogCollationNotSupported -f $ServerObject.InstanceName, $ServerObject.VersionMajor
196+
197+
$PSCmdlet.ThrowTerminatingError(
198+
[System.Management.Automation.ErrorRecord]::new(
199+
[System.InvalidOperationException]::new($errorMessage),
200+
'NSD0005', # cspell: disable-line
201+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
202+
$CatalogCollation
203+
)
204+
)
205+
}
206+
}
207+
180208
$verboseDescriptionMessage = $script:localizedData.Database_Create_ShouldProcessVerboseDescription -f $Name, $ServerObject.InstanceName
181209
$verboseWarningMessage = $script:localizedData.Database_Create_ShouldProcessVerboseWarning -f $Name
182210
$captionMessage = $script:localizedData.Database_Create_ShouldProcessCaption
@@ -197,6 +225,11 @@ function New-SqlDscDatabase
197225
$sqlDatabaseObjectToCreate.Collation = $Collation
198226
}
199227

228+
if ($PSBoundParameters.ContainsKey('CatalogCollation'))
229+
{
230+
$sqlDatabaseObjectToCreate.CatalogCollation = $CatalogCollation
231+
}
232+
200233
if ($PSBoundParameters.ContainsKey('CompatibilityLevel'))
201234
{
202235
$sqlDatabaseObjectToCreate.CompatibilityLevel = $CompatibilityLevel

0 commit comments

Comments
 (0)