Skip to content

Commit 8cb2a30

Browse files
committed
Refactor CompatibilityLevel, MirroringSafetyLevel, and RetentionPeriodUnits properties to use ValidateSet and remove enum dependencies
1 parent 94cbd50 commit 8cb2a30

File tree

5 files changed

+14
-64
lines changed

5 files changed

+14
-64
lines changed

source/Classes/020.SqlDatabase.ps1

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ class SqlDatabase : SqlResourceBase
337337
$Collation
338338

339339
[DscProperty()]
340-
[DatabaseCompatibilityLevel]
340+
[ValidateSet('Version80', 'Version90', 'Version100', 'Version110', 'Version120', 'Version130', 'Version140', 'Version150', 'Version160')]
341+
[System.String]
341342
$CompatibilityLevel
342343

343344
[DscProperty()]
@@ -601,7 +602,8 @@ class SqlDatabase : SqlResourceBase
601602

602603
# Enum properties (using string to avoid SMO type dependencies)
603604
[DscProperty()]
604-
[RetentionPeriodUnits]
605+
[ValidateSet('None', 'Days', 'Hours', 'Minutes')]
606+
[System.String]
605607
$ChangeTrackingRetentionPeriodUnits
606608

607609
[DscProperty()]
@@ -630,7 +632,8 @@ class SqlDatabase : SqlResourceBase
630632
$LegacyCardinalityEstimationForSecondary
631633

632634
[DscProperty()]
633-
[MirroringSafetyLevel]
635+
[ValidateSet('None', 'Unknown', 'Off', 'Full')]
636+
[System.String]
634637
$MirroringSafetyLevel
635638

636639
[DscProperty()]
@@ -742,7 +745,7 @@ class SqlDatabase : SqlResourceBase
742745
# Only set CompatibilityLevel if it's a valid non-zero value
743746
if ($databaseObject.CompatibilityLevel -gt 0)
744747
{
745-
$currentState.CompatibilityLevel = [DatabaseCompatibilityLevel] $databaseObject.CompatibilityLevel.ToString()
748+
$currentState.CompatibilityLevel = $databaseObject.CompatibilityLevel.ToString()
746749
}
747750

748751
$currentState.RecoveryModel = $databaseObject.RecoveryModel.ToString()
@@ -858,11 +861,11 @@ class SqlDatabase : SqlResourceBase
858861
# Double properties
859862
$currentState.MaxSizeInBytes = [System.Double] $databaseObject.MaxSizeInBytes
860863

861-
# RetentionPeriodUnits enum (convert SMO enum to module enum)
862-
$currentState.ChangeTrackingRetentionPeriodUnits = [RetentionPeriodUnits] $databaseObject.ChangeTrackingRetentionPeriodUnits.ToString()
864+
# RetentionPeriodUnits (convert SMO enum to string)
865+
$currentState.ChangeTrackingRetentionPeriodUnits = $databaseObject.ChangeTrackingRetentionPeriodUnits.ToString()
863866

864-
# MirroringSafetyLevel enum (convert SMO enum to module enum)
865-
$currentState.MirroringSafetyLevel = [MirroringSafetyLevel] $databaseObject.MirroringSafetyLevel.ToString()
867+
# MirroringSafetyLevel (convert SMO enum to string)
868+
$currentState.MirroringSafetyLevel = $databaseObject.MirroringSafetyLevel.ToString()
866869

867870
# Enum properties (convert to string with null check)
868871
$enumProperties = @(

source/Enum/001.DatabaseCompatibilityLevel.ps1

Lines changed: 0 additions & 20 deletions
This file was deleted.

source/Enum/001.MirroringSafetyLevel.ps1

Lines changed: 0 additions & 16 deletions
This file was deleted.

source/Enum/001.RetentionPeriodUnits.ps1

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/Unit/Classes/SqlDatabase.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ Describe 'SqlDatabase\GetCurrentState()' -Tag 'GetCurrentState' {
489489
$currentState.Credential | Should -BeNullOrEmpty
490490
$currentState.Name | Should -Be 'TestDatabase'
491491
$currentState.Collation | Should -Be 'SQL_Latin1_General_CP1_CI_AS'
492-
$currentState.CompatibilityLevel | Should -Be ([DatabaseCompatibilityLevel]::Version150)
492+
$currentState.CompatibilityLevel | Should -Be 'Version150'
493493
$currentState.RecoveryModel | Should -Be 'Full'
494494
$currentState.OwnerName | Should -Be 'sa'
495495
$currentState.SnapshotIsolation | Should -BeFalse
@@ -1037,7 +1037,7 @@ Describe 'SqlDatabase\AssertProperties()' -Tag 'AssertProperties' {
10371037
$script:mockSqlDatabaseInstance = [SqlDatabase] @{
10381038
Name = 'TestDatabase'
10391039
InstanceName = 'NamedInstance'
1040-
CompatibilityLevel = [DatabaseCompatibilityLevel]::Version80
1040+
CompatibilityLevel = 'Version80'
10411041
} |
10421042
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
10431043
return New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
@@ -1061,7 +1061,7 @@ Describe 'SqlDatabase\AssertProperties()' -Tag 'AssertProperties' {
10611061
{
10621062
$script:mockSqlDatabaseInstance.AssertProperties(
10631063
@{
1064-
CompatibilityLevel = [DatabaseCompatibilityLevel]::Version80
1064+
CompatibilityLevel = 'Version80'
10651065
}
10661066
)
10671067
} | Should -Throw -ExpectedMessage $mockErrorMessage

0 commit comments

Comments
 (0)