Skip to content

Commit a8c131a

Browse files
Get-DbaDatabase - Add Pattern parameter for wildcard database filtering (#9897)
2 parents e206d14 + 4323988 commit a8c131a

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

CLAUDE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,21 @@ Failure to register in both locations will result in the command not being avail
376376
- Keep the description concise and descriptive (not vague)
377377
- Focus on what the change does, not implementation details
378378

379+
### Pattern Parameter Convention
380+
381+
**CRITICAL RULE**: When adding a -Pattern parameter to any dbatools command, it MUST use regular expressions (regex), not SQL LIKE wildcards or PowerShell wildcards.
382+
383+
```powershell
384+
# CORRECT - Regex pattern matching
385+
if ($name -match $pattern) { return $true }
386+
387+
# WRONG - SQL LIKE wildcards
388+
$psPattern = $pattern -replace '%', '*' -replace '_', '?'
389+
if ($name -like $psPattern) { return $true }
390+
```
391+
392+
This ensures consistency across all dbatools commands that support pattern matching.
393+
379394
### Parameter Validation Pattern
380395

381396
```powershell

public/Get-DbaDatabase.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ function Get-DbaDatabase {
2020
For MFA support, please use Connect-DbaInstance.
2121
2222
.PARAMETER Database
23-
Specifies one or more databases to include in the results. Supports wildcards and exact name matching.
23+
Specifies one or more databases to include in the results using exact name matching.
2424
Use this when you need to retrieve specific databases instead of all databases on the instance.
2525
2626
.PARAMETER ExcludeDatabase
27-
Specifies one or more databases to exclude from the results. Supports wildcards and exact name matching.
27+
Specifies one or more databases to exclude from the results using exact name matching.
2828
Use this to filter out specific databases like test or staging environments from your inventory.
2929
30+
.PARAMETER Pattern
31+
Specifies a pattern for filtering databases using regular expressions.
32+
Use this when you need to match databases by pattern, such as "^dbatools_" or ".*_prod$".
33+
This parameter supports standard .NET regular expression syntax.
34+
3035
.PARAMETER ExcludeUser
3136
Returns only system databases (master, model, msdb, tempdb).
3237
Use this when you need to focus on system database maintenance tasks or validation.
@@ -164,6 +169,11 @@ function Get-DbaDatabase {
164169
165170
Returns databases 'OneDb' and 'OtherDB' from SQL Server instances SQL2 and SQL3 if databases by those names exist on those instances.
166171
172+
.EXAMPLE
173+
PS C:\> Get-DbaDatabase -SqlInstance SQL2,SQL3 -Pattern "^dbatools_"
174+
175+
Returns all databases that match the regex pattern "^dbatools_" (e.g., dbatools_example1, dbatools_example2) from SQL Server instances SQL2 and SQL3.
176+
167177
#>
168178
[CmdletBinding(DefaultParameterSetName = "Default")]
169179
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "Internal functions are ignored")]
@@ -173,6 +183,7 @@ function Get-DbaDatabase {
173183
[PSCredential]$SqlCredential,
174184
[string[]]$Database,
175185
[string[]]$ExcludeDatabase,
186+
[string[]]$Pattern,
176187
[Alias("SystemDbOnly", "NoUserDb", "ExcludeAllUserDb")]
177188
[switch]$ExcludeUser,
178189
[Alias("UserDbOnly", "NoSystemDb", "ExcludeAllSystemDb")]
@@ -306,9 +317,21 @@ function Get-DbaDatabase {
306317
}
307318

308319
$backed_info = Invoke-QueryRawDatabases
320+
321+
# Helper function to test if a name matches any of the provided regex patterns
322+
$matchesPattern = {
323+
param($name, $patterns)
324+
if (!$patterns) { return $true }
325+
foreach ($pattern in $patterns) {
326+
if ($name -match $pattern) { return $true }
327+
}
328+
return $false
329+
}
330+
309331
$backed_info = $backed_info | Where-Object {
310332
($_.name -in $Database -or !$Database) -and
311333
($_.name -notin $ExcludeDatabase -or !$ExcludeDatabase) -and
334+
(& $matchesPattern $_.name $Pattern) -and
312335
($_.Owner -in $Owner -or !$Owner) -and
313336
($_.state -ne 6 -or !$OnlyAccessible)
314337
}
@@ -331,6 +354,7 @@ function Get-DbaDatabase {
331354
Where-Object {
332355
($_.Name -in $Database -or !$Database) -and
333356
($_.Name -notin $ExcludeDatabase -or !$ExcludeDatabase) -and
357+
(& $matchesPattern $_.Name $Pattern) -and
334358
($_.Owner -in $Owner -or !$Owner) -and
335359
($_.RecoveryModel -in $RecoveryModel -or !$_.RecoveryModel) -and
336360
$_.EncryptionEnabled -in $Encrypt
@@ -340,6 +364,7 @@ function Get-DbaDatabase {
340364
Where-Object {
341365
($_.Name -in $Database -or !$Database) -and
342366
($_.Name -notin $ExcludeDatabase -or !$ExcludeDatabase) -and
367+
(& $matchesPattern $_.Name $Pattern) -and
343368
($_.Owner -in $Owner -or !$Owner) -and
344369
$_.ReadOnly -in $Readonly -and
345370
$_.IsAccessible -in $AccessibleFilter -and

tests/Get-DbaDatabase.Tests.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Describe $CommandName -Tag UnitTests {
1515
"SqlCredential",
1616
"Database",
1717
"ExcludeDatabase",
18+
"Pattern",
1819
"ExcludeUser",
1920
"ExcludeSystem",
2021
"Owner",
@@ -83,6 +84,52 @@ Describe $CommandName -Tag IntegrationTests {
8384
}
8485
}
8586

87+
Describe $CommandName -Tag IntegrationTests {
88+
BeforeAll {
89+
$random = Get-Random
90+
$dbPrefix = "dbatoolsci_pattern"
91+
$dbname1 = "${dbPrefix}_test1_$random"
92+
$dbname2 = "${dbPrefix}_test2_$random"
93+
$dbname3 = "${dbPrefix}_prod1_$random"
94+
$dbname4 = "other_database_$random"
95+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance1 -Name $dbname1, $dbname2, $dbname3, $dbname4
96+
}
97+
AfterAll {
98+
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname1, $dbname2, $dbname3, $dbname4 | Remove-DbaDatabase
99+
}
100+
101+
Context "Pattern parameter filtering" {
102+
It "Should return databases matching pattern with regex" {
103+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Pattern "^${dbPrefix}_"
104+
$results.Name | Should -Contain $dbname1
105+
$results.Name | Should -Contain $dbname2
106+
$results.Name | Should -Contain $dbname3
107+
$results.Name | Should -Not -Contain $dbname4
108+
}
109+
110+
It "Should return databases matching pattern with _test segment" {
111+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Pattern "^${dbPrefix}_test"
112+
$results.Name | Should -Contain $dbname1
113+
$results.Name | Should -Contain $dbname2
114+
$results.Name | Should -Not -Contain $dbname3
115+
$results.Name | Should -Not -Contain $dbname4
116+
}
117+
118+
It "Should return databases matching multiple patterns" {
119+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Pattern "^${dbPrefix}_test", "^${dbPrefix}_prod"
120+
$results.Name | Should -Contain $dbname1
121+
$results.Name | Should -Contain $dbname2
122+
$results.Name | Should -Contain $dbname3
123+
$results.Name | Should -Not -Contain $dbname4
124+
}
125+
126+
It "Should return no results for non-matching pattern" {
127+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Pattern "^nonexistent_"
128+
$results | Should -BeNullOrEmpty
129+
}
130+
}
131+
}
132+
86133
Describe $CommandName -Tag UnitTests -Skip {
87134
# Skip UnitTests because they need refactoring.
88135

0 commit comments

Comments
 (0)