Skip to content

Commit 68ea37e

Browse files
committed
Refactor compatibility level handling in SqlDatabase to use ConvertToSmoEnumType and remove type resolution logic
1 parent 16e3a6a commit 68ea37e

File tree

2 files changed

+1
-20
lines changed

2 files changed

+1
-20
lines changed

source/Classes/020.SqlDatabase.ps1

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -983,26 +983,8 @@ class SqlDatabase : SqlResourceBase
983983
$serverObject = $this.GetServerObject()
984984
$supportedLevels = $serverObject | Get-SqlDscCompatibilityLevel
985985

986-
# Use runtime type resolution to avoid parse-time errors when SMO isn't loaded
987-
$smoCompatibilityLevelType = [System.Type]::GetType('Microsoft.SqlServer.Management.Smo.CompatibilityLevel', $false, $true)
988-
989-
if ($null -eq $smoCompatibilityLevelType)
990-
{
991-
# Fallback: try to get the type from the loaded assemblies
992-
$smoCompatibilityLevelType = [System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() |
993-
Where-Object -FilterScript { $_.FullName -eq 'Microsoft.SqlServer.Management.Smo.CompatibilityLevel' } |
994-
Select-Object -First 1
995-
}
996-
997-
if ($null -eq $smoCompatibilityLevelType)
998-
{
999-
$errorMessage = $this.localizedData.SmoCompatibilityLevelTypeNotFound
1000-
1001-
New-InvalidOperationException -Message $errorMessage
1002-
}
1003-
1004986
# Convert the DatabaseCompatibilityLevel enum to the SMO CompatibilityLevel enum
1005-
$compatibilityLevelValue = [System.Enum]::Parse($smoCompatibilityLevelType, $properties.CompatibilityLevel.ToString(), $true)
987+
$compatibilityLevelValue = $this.ConvertToSmoEnumType('CompatibilityLevel', $properties.CompatibilityLevel.ToString())
1006988

1007989
if ($compatibilityLevelValue -notin $supportedLevels)
1008990
{

source/en-US/SqlDatabase.strings.psd1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ ConvertFrom-StringData @'
2626
IsLedgerCannotBeChanged = The property IsLedger cannot be changed after the database is created. (SD0015)
2727
FailedToEnableSnapshotIsolation = Failed to enable snapshot isolation for the database '{0}'. (SD0016)
2828
FailedToDisableSnapshotIsolation = Failed to disable snapshot isolation for the database '{0}'. (SD0017)
29-
SmoCompatibilityLevelTypeNotFound = Unable to find type 'Microsoft.SqlServer.Management.Smo.CompatibilityLevel'. Ensure SQL Server Management Objects (SMO) are installed. (SD0018)
3029
'@

0 commit comments

Comments
 (0)