Skip to content

Commit

Permalink
Set-DbaDbCompatbility - Fix for using piped input and don't trust mas…
Browse files Browse the repository at this point in the history
…ter compatibility level (#9146)
  • Loading branch information
andreasjordan authored Nov 5, 2023
1 parent ed4c3ec commit 368ea57
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions public/Set-DbaDbCompatibility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,19 @@ function Set-DbaDbCompatibility {
$InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database
}

if (Test-Bound -ParameterName 'Compatibility') {
$targetCompatibility = $Compatibility
} else {
$targetCompatibility =
try {
(Get-DbaDbCompatibility -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database master -EnableException).Compatibility
} catch {
Stop-Function -Message 'Unable to detect instance level compatibility level' -ErrorRecord $_ -Target $SqlInstance
}

Write-Message -Level Verbose -Message "No Compatibility value provided, setting databases to match the SQL Server Instance version: $targetCompatibility"
}
Write-Message -Level Verbose -Message "SQL Server instance Compatibility Level: $targetCompatibility"

foreach ($db in $InputObject) {
$server = $db.Parent
$dbLevel = $db.CompatibilityLevel
Write-Message -Level Verbose -Message "Database $db current Compatibility Level: $dbLevel"

if (Test-Bound -ParameterName 'Compatibility') {
$targetCompatibility = $Compatibility
} else {
$serverVersion = $server.VersionMajor
$targetCompatibility = [Microsoft.SqlServer.Management.Smo.CompatibilityLevel]"Version$($serverVersion)0"
Write-Message -Level Verbose -Message "No Compatibility value provided, setting databases to match the SQL Server Instance version: $targetCompatibility"
}

if ($dbLevel -ne $targetCompatibility) {
if ($PSCmdlet.ShouldProcess($server.Name, "Setting $db Compatibility Level to $targetCompatibility")) {
try {
Expand All @@ -125,7 +119,7 @@ function Set-DbaDbCompatibility {
}
}
} else {
Write-Message -Level Verbose -Message "Database $db current Compatibility Level matches instance level [$targetCompatibility]"
Write-Message -Level Verbose -Message "Database $db current Compatibility Level matches target level [$targetCompatibility]"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Set-DbaDbCompatibility.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Describe "$CommandName Integration Tests" -Tag 'IntegrationTests' {
$sqlCn.Refresh()
$sqlCn.Databases.Refresh()
$resultMatches = Set-DbaDbCompatibility -SqlInstance $sqlCn -Database 'master' -Verbose 4>&1
$verboseMsg = '*current Compatibility Level matches instance level*'
$verboseMsg = '*current Compatibility Level matches target level*'

$sqlCn.Refresh()
$sqlCn.Databases.Refresh()
Expand Down

0 comments on commit 368ea57

Please sign in to comment.