From 97ce7f593bfde09d52b80891185fad72f4fb1aa5 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 22 Aug 2026 20:11:39 +0200 Subject: [PATCH] Testing Get-DbaDbQueryStoreOption - Create the database before anything has read model The Context added by #10592 created its database after the Contexts that read Query Store on model, and those leave a session parked in model. On the CI runners the next CREATE DATABASE then failed: Could not obtain exclusive lock on database 'model'. Retry the operation later. CREATE DATABASE failed. Some file names listed could not be created. Three attempts, all three the same, so development is red on it. It did not show up in the lab because the run there happened to get the lock. The database is created in the BeforeAll of the Describe now, which runs before any Context, and removed in a new AfterAll. Nothing else changes. The parked session is the leak of #10584. Once that is merged this workaround is no longer needed, but the test should not depend on the order of the Contexts either way. The same care was already taken in the Set-DbaDbQueryStoreOption test of #10593, which is why that one did not break. (do Get-DbaDbQueryStoreOption) --- tests/Get-DbaDbQueryStoreOption.Tests.ps1 | 30 ++++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/Get-DbaDbQueryStoreOption.Tests.ps1 b/tests/Get-DbaDbQueryStoreOption.Tests.ps1 index 4ab707ed7c8..95b32dd0cd6 100644 --- a/tests/Get-DbaDbQueryStoreOption.Tests.ps1 +++ b/tests/Get-DbaDbQueryStoreOption.Tests.ps1 @@ -37,10 +37,27 @@ Describe $CommandName -Tag IntegrationTests { $serverSingle = Connect-DbaInstance -SqlInstance $TestConfig.InstanceSingle + # Created here rather than in the Context that uses it, because the Contexts below read Query Store + # on model and leave a session parked in it, and the next CREATE DATABASE then fails with + # "Could not obtain exclusive lock on database 'model'". That is the leak of #10584, and until it + # is merged the only way past it is to create the database before anything has touched model. + $queryStoreDbName = "dbatoolsci_qso_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Name $queryStoreDbName + $null = Set-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -State ReadWrite + # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings. $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } + AfterAll { + # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails. + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -ErrorAction SilentlyContinue + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + Context "When a system database is named explicitly" { It "Warns about master and tempdb instead of silently returning nothing" { $resultsSystemDb = Get-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database master, tempdb -WarningVariable warnSystemDb -WarningAction SilentlyContinue @@ -76,23 +93,12 @@ Describe $CommandName -Tag IntegrationTests { BeforeAll { $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - $queryStoreDbName = "dbatoolsci_qso_$(Get-Random)" - $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Name $queryStoreDbName - $null = Set-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -State ReadWrite - + # The database itself comes from the BeforeAll of the Describe, see the note there. $resultsFromSmo = Get-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } - AfterAll { - $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - - $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -ErrorAction SilentlyContinue - - $PSDefaultParameterValues.Remove("*-Dba*:EnableException") - } - It "Leaves MaxPlansPerQuery and WaitStatsCaptureMode as the properties of the SMO object" { # Both are real properties of QueryStoreOptions. Adding them with Add-Member replaces the # property with a note property on the object of the caller - one that keeps its value even