Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
913e672
`Set-SqlDscDatabase`: Refactor command
johlju Oct 26, 2025
8d3f53c
Update Get-SqlDscDatabase calls to include ErrorAction parameter for …
johlju Oct 27, 2025
4b60d19
Remove CatalogCollation parameter and update documentation for read-o…
johlju Oct 27, 2025
8633f82
Update CHANGELOG.md to clarify removal of OwnerName parameter and add…
johlju Oct 27, 2025
31bd4b7
Enhance Set-SqlDscDatabaseOwner with idempotence check and error hand…
johlju Oct 27, 2025
cfad5dd
Update Set-SqlDscDatabase documentation to clarify output behavior fo…
johlju Oct 27, 2025
c42783f
Fix error handling in Set-SqlDscDatabase to use correct database obje…
johlju Oct 27, 2025
a707b2a
Update compatibility level in Set-SqlDscDatabase tests to Version140 …
johlju Oct 27, 2025
6ae63ed
Add integration tests for Get-SqlDscCompatibilityLevel to verify comp…
johlju Oct 27, 2025
5586319
Refactor assertions in Set-SqlDscDatabase integration tests to use Sh…
johlju Oct 27, 2025
5f69cce
Add ErrorAction parameter to Get-SqlDscDatabase calls for improved er…
johlju Oct 27, 2025
929a663
Fix build script path in test files for correct dependency resolution
johlju Oct 27, 2025
914d116
Refactor Set-SqlDscDatabase tests to improve readability and add para…
johlju Oct 27, 2025
8a5caae
Add parameter validation tests for Set-SqlDscDatabaseOwner to ensure …
johlju Oct 27, 2025
baea0a7
Add Refresh parameter to Set-SqlDscDatabase call in test for desired …
johlju Oct 27, 2025
e8b5bc3
Refactor Get-SqlDscCompatibilityLevel function to improve readability…
johlju Oct 27, 2025
81587d9
Add suppression message for ScriptAnalyzer rule in Get-SqlDscCompatib…
johlju Oct 27, 2025
cfa078c
Add Refresh parameter to Set-SqlDscDatabase calls in tests for improv…
johlju Oct 27, 2025
b5af528
Add AfterAll block to disconnect from SQL database engine in Get-SqlD…
johlju Oct 27, 2025
1bf9a9d
Update stream redirection in test scripts to use the correct error st…
johlju Oct 27, 2025
a358150
Refactor Set-SqlDscDatabase to support all settable SMO Database prop…
johlju Oct 27, 2025
54ba3af
Add VersionMajor property to object initialization in SMO stub for en…
johlju Oct 27, 2025
0a6cc35
Add ErrorAction parameter to database connection and disconnection fo…
johlju Oct 27, 2025
8ac28ba
Add ErrorAction parameter to Connect-SqlDscDatabaseEngine and Disconn…
johlju Oct 27, 2025
77db78a
Add justification for suppressing PSUseDeclaredVarsMoreThanAssignment…
johlju Oct 27, 2025
063a2d0
Add tests for validating parameter sets in Get-SqlDscCompatibilityLevel
johlju Oct 27, 2025
806c6fc
Enhance mock server object by adding Refresh method to database colle…
johlju Oct 27, 2025
f29221f
Refactor Set-SqlDscDatabase to remove deprecated parameters and updat…
johlju Oct 27, 2025
308841f
Remove unused SetOwner method from mock database object in Set-SqlDsc…
johlju Oct 27, 2025
589c804
Add IsReadCommittedSnapshotOn parameter to Set-SqlDscDatabase and upd…
johlju Oct 28, 2025
5f22e1f
Add Set-SqlDscDatabaseProperty function to manage SQL Server database…
johlju Oct 28, 2025
da74cd8
Rename Set-SqlDscDatabase to Set-SqlDscDatabaseProperty and update re…
johlju Oct 28, 2025
03d87de
Add integration tests for Set-SqlDscDatabaseProperty to validate data…
johlju Oct 28, 2025
bf0579b
Add unit tests for Set-SqlDscDatabaseProperty to validate database pr…
johlju Oct 28, 2025
a9cbc61
Add parameters for DatabaseSnapshotBaseName, IsLedger, IsParameteriza…
johlju Oct 28, 2025
91f92e8
Add CatalogCollation parameter to New-SqlDscDatabase for system catal…
johlju Oct 28, 2025
c67fb70
Update type assertion for Set-SqlDscDatabaseProperty result to use ty…
johlju Oct 28, 2025
141c8fd
Remove OwnerName parameter from Set-SqlDscDatabaseProperty tests afte…
johlju Oct 28, 2025
eef8600
Add detailed status messages for Set-SqlDscDatabaseProperty and Set-S…
johlju Oct 28, 2025
17ba394
Add localized error message for property not found in Set-SqlDscDatab…
johlju Oct 28, 2025
f9bf9ca
Add Set-SqlDscDatabaseOwner integration tests to pipeline
johlju Oct 28, 2025
57b181b
Remove DefaultFullTextCatalog parameter and update MaxSizeInBytes typ…
johlju Oct 28, 2025
0bdc7fb
Update MaxSizeInBytes type to Double in parameter sets for Set-SqlDsc…
johlju Oct 28, 2025
9d98844
Fix Describe block name in Set-SqlDscDatabaseProperty tests
johlju Oct 28, 2025
d754bb3
Add note about SQL Server version requirement for CatalogCollation pa…
johlju Oct 28, 2025
d638be6
Add validation for CatalogCollation parameter and error message for u…
johlju Oct 28, 2025
6d34ecb
Add DelayedDurability parameter to Set-SqlDscDatabaseProperty and def…
johlju Oct 28, 2025
5a4f5ac
Fix error handling in Set-SqlDscDatabaseProperty to target the databa…
johlju Oct 28, 2025
be7d199
Change DelayedDurability property to use enum type in SMO stub
johlju Oct 28, 2025
d6b18d5
Update parameter sets in Set-SqlDscDatabaseProperty tests to include …
johlju Oct 28, 2025
c5cd62b
Add CatalogCollationType enum and update parameter set in New-SqlDscD…
johlju Oct 28, 2025
f29296f
Add refresh logic for database owner in Set-SqlDscDatabaseOwner funct…
johlju Oct 28, 2025
e191872
Implement feature X to enhance user experience and fix bug Y in module Z
johlju Oct 28, 2025
ff38703
Change CatalogCollation property to use CatalogCollationType enum for…
johlju Oct 28, 2025
4554951
Fix comments for clarity in BeforeDiscovery block of Set-SqlDscDataba…
johlju Oct 28, 2025
df95655
Refactor Set-SqlDscDatabaseOwner test to ensure SetOwner is not calle…
johlju Oct 28, 2025
d7a512a
Add CatalogCollation property to enum properties in database property…
johlju Oct 28, 2025
527ae77
Update Get-SqlDscDatabase calls to include Refresh parameter for accu…
johlju Oct 28, 2025
1878d66
Enhance Set-SqlDscDatabaseOwner to call Alter after setting owner for…
johlju Oct 28, 2025
810fe69
Add refresh logic to Get-SqlDscDatabase for updated database object r…
johlju Oct 29, 2025
75271aa
Add verbose logging for original database owner and update Set-SqlDsc…
johlju Oct 29, 2025
689fdfe
Add DropExistingUser parameter to Set-SqlDscDatabaseOwner for user ac…
johlju Oct 29, 2025
4959ec0
Add Refresh method to SMO class for improved state management
johlju Oct 29, 2025
9559d31
Refactor Set-SqlDscDatabaseOwner to ensure database object is refresh…
johlju Oct 29, 2025
a7ba2b8
Remove obsolete test case for non-existent database in Set-SqlDscData…
johlju Oct 29, 2025
480db6b
Update error code in Set-SqlDscDatabaseOwner for improved error handling
johlju Oct 29, 2025
a00092a
Fix formatting in documentation for Set-SqlDscDatabaseOwner parameter…
johlju Oct 30, 2025
1f2dd60
Comment out Alter method calls in Set-SqlDscDatabaseOwner for debuggi…
johlju Oct 30, 2025
4a7a5d3
Remove commented Alter method calls in Set-SqlDscDatabaseOwner for cl…
johlju Oct 30, 2025
9a86402
Add Get-SqlDscCompatibilityLevel integration test to pipeline and doc…
johlju Oct 31, 2025
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
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- BREAKING CHANGE: Removed public command `Test-SqlDscDatabase`. Use
`Test-SqlDscIsDatabase` to check existence. For property checks, use
`Test-SqlDscDatabaseProperty`. See [issue #2201](https://github.com/dsccommunity/SqlServerDsc/issues/2201).
- BREAKING CHANGE: `Set-SqlDscDatabase`
- Removed parameter `OwnerName` [issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177).
Use the new command `Set-SqlDscDatabaseOwner` to change database ownership instead.

### Added

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

### Changed

- BREAKING CHANGE: `Set-SqlDscDatabase` has been renamed to `Set-SqlDscDatabaseProperty`
to better reflect its purpose of setting database properties. All existing references
should be updated to use the new name.
- `Set-SqlDscDatabaseProperty` (formerly `Set-SqlDscDatabase`)
- BREAKING CHANGE: Completely refactored to support settable SMO Database
properties as parameters ([issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177)).
- `Remove-SqlDscAgentAlert`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2193](https://github.com/dsccommunity/SqlServerDsc/issues/2193)).
Expand All @@ -235,7 +249,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `New-SqlDscDatabase`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2200](https://github.com/dsccommunity/SqlServerDsc/issues/2200)).
- `Set-SqlDscDatabase`
- `Set-SqlDscDatabaseProperty` (formerly `Set-SqlDscDatabase`)
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2198](https://github.com/dsccommunity/SqlServerDsc/issues/2198)).
- `Add-SqlDscTraceFlag`
Expand Down
4 changes: 3 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ stages:
'tests/Integration/Commands/Get-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1'
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscCompatibilityLevel.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscDatabaseProperty.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscDatabaseOwner.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscIsDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscDatabaseProperty.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1'
Expand Down
138 changes: 138 additions & 0 deletions source/Public/Get-SqlDscCompatibilityLevel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<#
.SYNOPSIS
Gets the supported database compatibility levels for a SQL Server instance or version.

.DESCRIPTION
This command returns the supported database compatibility levels for a SQL Server
Database Engine instance or a specific SQL Server version.

The compatibility levels are determined based on the SQL Server version, following
the official Microsoft documentation for supported compatibility level ranges.

.PARAMETER ServerObject
Specifies the SQL Server connection object to get supported compatibility levels for.

.PARAMETER Version
Specifies the SQL Server version to get supported compatibility levels for.
Only the major version number is used for determining compatibility levels.

.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Get-SqlDscCompatibilityLevel -ServerObject $serverObject

Returns all supported compatibility levels for the connected SQL Server instance.

.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Get-SqlDscCompatibilityLevel

Returns all supported compatibility levels using pipeline input.

.EXAMPLE
Get-SqlDscCompatibilityLevel -Version '16.0.1000.6'

Returns all supported compatibility levels for SQL Server 2022 (version 16).

.INPUTS
Microsoft.SqlServer.Management.Smo.Server

The server object to get supported compatibility levels for.

.OUTPUTS
System.String[]

Returns an array of supported compatibility level names (e.g., 'Version160', 'Version150').
#>
function Get-SqlDscCompatibilityLevel
{
[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.')]
[CmdletBinding(DefaultParameterSetName = 'ServerObject')]
[OutputType([System.String[]])]
param
(
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,

[Parameter(ParameterSetName = 'Version', Mandatory = $true)]
[System.Version]
$Version
)

process
{
# Get the major version based on parameter set
$majorVersion = if ($PSCmdlet.ParameterSetName -eq 'ServerObject')
{
Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForInstance -f $ServerObject.InstanceName, $ServerObject.VersionMajor)
$ServerObject.VersionMajor
}
else
{
Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForVersion -f $Version, $Version.Major)
$Version.Major
}

# Get all available compatibility levels from the enum
$allCompatibilityLevels = [System.Enum]::GetNames([Microsoft.SqlServer.Management.Smo.CompatibilityLevel])

<#
Determine minimum supported compatibility level based on SQL Server version
Reference: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level
#>
$minimumCompatLevel = switch ($majorVersion)
{
{ $_ -ge 12 }
{
100 # SQL 2014 (v12) and later support minimum compat level 100
}

11
{
90 # SQL 2012 (v11) supports minimum compat level 90
}

{ $_ -le 10 }
{
80 # SQL 2008 R2 (v10.5) and earlier support minimum compat level 80
}
}

<#
Filter compatibility levels that are supported by this SQL Server version
CompatibilityLevel enum values are named like "Version80", "Version90", etc.
SQL Server supports compatibility levels from the minimum up to (version * 10)
#>
$supportedCompatibilityLevels = $allCompatibilityLevels | Where-Object -FilterScript {
if ($_ -match 'Version(\d+)')
{
$compatLevelVersion = [System.Int32] $Matches[1]
($compatLevelVersion -ge $minimumCompatLevel) -and ($compatLevelVersion -le ($majorVersion * 10))
}
else
{
$false
}
}

<#
Warn if SQL Server version is newer than what SMO library supports
Check if the expected maximum compatibility level is missing from the supported list
#>
$expectedMaxCompatLevel = "Version$($majorVersion * 10)"
if ($expectedMaxCompatLevel -notin $supportedCompatibilityLevels -and $supportedCompatibilityLevels.Count -gt 0)
{
# Get the actual maximum from the last element (they're in ascending order)
$lastCompatLevel = $supportedCompatibilityLevels[-1]
if ($lastCompatLevel -match 'Version(\d+)')
{
$maxCompatLevelInEnum = [System.Int32] $Matches[1]
Write-Warning -Message ($script:localizedData.GetCompatibilityLevel_SmoTooOld -f $majorVersion, $maxCompatLevelInEnum, ($majorVersion * 10))
}
}

Write-Debug -Message ($script:localizedData.GetCompatibilityLevel_Found -f $supportedCompatibilityLevels.Count, $majorVersion)

return $supportedCompatibilityLevels
}
}
6 changes: 6 additions & 0 deletions source/Public/Get-SqlDscDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ function Get-SqlDscDatabase
}
else
{
if ($Refresh.IsPresent)
{
# Refresh the database object
$databaseObject.Refresh()
}

Write-Verbose -Message ($script:localizedData.Database_Found -f $Name)
}
}
Expand Down
33 changes: 33 additions & 0 deletions source/Public/New-SqlDscDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
The name of the SQL collation to use for the new database.
Default value is server collation.

.PARAMETER CatalogCollation
Specifies the collation type for the system catalog. Valid values are
DATABASE_DEFAULT and SQL_Latin1_General_CP1_CI_AS. This property can
only be set during database creation and cannot be modified afterward.
This parameter requires SQL Server 2019 (version 15) or later.

.PARAMETER CompatibilityLevel
The version of the SQL compatibility level to use for the new database.
Default value is server version.
Expand Down Expand Up @@ -73,6 +79,10 @@ function New-SqlDscDatabase
[System.String]
$Collation,

[Parameter()]
[Microsoft.SqlServer.Management.Smo.CatalogCollationType]
$CatalogCollation,

[Parameter()]
[ValidateSet('Version80', 'Version90', 'Version100', 'Version110', 'Version120', 'Version130', 'Version140', 'Version150', 'Version160')]
[System.String]
Expand Down Expand Up @@ -177,6 +187,24 @@ function New-SqlDscDatabase
}
}

# Validate CatalogCollation if specified (requires SQL Server 2019+)
if ($PSBoundParameters.ContainsKey('CatalogCollation'))
{
if ($ServerObject.VersionMajor -lt 15)
{
$errorMessage = $script:localizedData.Database_CatalogCollationNotSupported -f $ServerObject.InstanceName, $ServerObject.VersionMajor

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage),
'NSD0005', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$CatalogCollation
)
)
}
}

$verboseDescriptionMessage = $script:localizedData.Database_Create_ShouldProcessVerboseDescription -f $Name, $ServerObject.InstanceName
$verboseWarningMessage = $script:localizedData.Database_Create_ShouldProcessVerboseWarning -f $Name
$captionMessage = $script:localizedData.Database_Create_ShouldProcessCaption
Expand All @@ -197,6 +225,11 @@ function New-SqlDscDatabase
$sqlDatabaseObjectToCreate.Collation = $Collation
}

if ($PSBoundParameters.ContainsKey('CatalogCollation'))
{
$sqlDatabaseObjectToCreate.CatalogCollation = $CatalogCollation
}

if ($PSBoundParameters.ContainsKey('CompatibilityLevel'))
{
$sqlDatabaseObjectToCreate.CompatibilityLevel = $CompatibilityLevel
Expand Down
Loading