Skip to content

Commit

Permalink
Get-DbaDbView, add Schema parameter (do DbView) (#9475)
Browse files Browse the repository at this point in the history
  • Loading branch information
niphlod authored Oct 5, 2024
1 parent b58de53 commit 5fa6bbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions public/Get-DbaDbView.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function Get-DbaDbView {
.PARAMETER View
The view(s) to include - all views are selected if not populated
.PARAMETER Schema
Only return views from the specified schema
.PARAMETER InputObject
Enables piping from Get-DbaDatabase
Expand Down Expand Up @@ -92,6 +95,7 @@ function Get-DbaDbView {
[object[]]$ExcludeDatabase,
[switch]$ExcludeSystemView,
[string[]]$View,
[string[]]$Schema,
[Parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.Database[]]$InputObject,
[switch]$EnableException
Expand Down Expand Up @@ -167,6 +171,11 @@ function Get-DbaDbView {
Write-Message -Message "No views exist in the $db database on $($db.Parent.DomainInstanceName)" -Target $db -Level Verbose
continue
}

if ($Schema) {
$views = $views | Where-Object Schema -in $Schema
}

if (Test-Bound -ParameterName ExcludeSystemView) {
$views = $views | Where-Object { -not $_.IsSystemObject }
}
Expand Down
15 changes: 14 additions & 1 deletion tests/Get-DbaDbView.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
It "Should only contain our specific parameters" {
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'ExcludeSystemView', 'View', 'InputObject', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'ExcludeSystemView', 'View', 'Schema', 'InputObject', 'EnableException'
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty
}
}
Expand All @@ -16,10 +16,15 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$viewName = ("dbatoolsci_{0}" -f $(Get-Random))
$viewNameWithSchema = ("dbatoolsci_{0}" -f $(Get-Random))
$server.Query("CREATE VIEW $viewName AS (SELECT 1 as col1)", 'tempdb')
$server.Query("CREATE SCHEMA [someschema]", 'tempdb')
$server.Query("CREATE VIEW [someschema].$viewNameWithSchema AS (SELECT 1 as col1)", 'tempdb')
}
AfterAll {
$null = $server.Query("DROP VIEW $viewName", 'tempdb')
$null = $server.Query("DROP VIEW [someschema].$viewNameWithSchema", 'tempdb')
$null = $server.Query("DROP SCHEMA [someschema]", 'tempdb')
}

Context "Command actually works" {
Expand Down Expand Up @@ -59,4 +64,12 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
($results | Where-Object Name -eq $viewName).Name | Should -Be $viewName
}
}

Context "Schema parameter (see #9445)" {
It "Should return just one view with schema 'someschema'" {
$results = $script:instance2 | Get-DbaDbView -Database tempdb -Schema 'someschema'
($results | Where-Object Name -eq $viewNameWithSchema).Name | Should -Be $viewNameWithSchema
($results | Where-Object Schema -ne 'someschema').Count | Should -Be 0
}
}
}

0 comments on commit 5fa6bbd

Please sign in to comment.