Skip to content

Commit

Permalink
Add DatabaseId to the return object for Find-DbaDbGrowthEvent (#8510)
Browse files Browse the repository at this point in the history
  • Loading branch information
lancasteradam authored Aug 29, 2022
1 parent 811d83c commit 32fd3c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
5 changes: 3 additions & 2 deletions functions/Find-DbaDbGrowthEvent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function Find-DbaDbGrowthEvent {
CONVERT(INT,(DENSE_RANK() OVER (ORDER BY [StartTime] DESC))%2) AS OrderRank,
CONVERT(INT, [EventClass]) AS EventClass,
[DatabaseName],
(SELECT database_id FROM sys.databases WHERE Name = [DatabaseName]) AS DatabaseId,
[Filename],
CONVERT(INT,(Duration/1000)) AS Duration,
$(if (-not $UseLocalTime) { "
Expand Down Expand Up @@ -202,7 +203,7 @@ function Find-DbaDbGrowthEvent {
0 AS [HostName],
0 AS [SessionLoginName],
0 AS [SPID]
END TRY
END TRY
BEGIN CATCH
SELECT
SERVERPROPERTY('MachineName') AS ComputerName,
Expand Down Expand Up @@ -257,4 +258,4 @@ function Find-DbaDbGrowthEvent {
}
}
}
}
}
59 changes: 52 additions & 7 deletions tests/Find-DbaDbGrowthEvent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,61 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'EventType', 'FileType', 'UseLocalTime', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
<#
Integration test should appear below and are custom to the command you are writing.
Read https://github.com/dataplat/dbatools/blob/development/contributing.md#tests
for more guidence.
#>

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Context "Command actually works" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance1
$random = Get-Random
$databaseName1 = "dbatoolsci1_$random"
$db1 = New-DbaDatabase -SqlInstance $server -Name $databaseName1

$sqlGrowthAndShrink =
"CREATE TABLE Tab1 (ID INTEGER);
INSERT INTO Tab1 (ID)
SELECT
1
FROM
sys.all_objects a
CROSS JOIN
sys.all_objects b;
TRUNCATE TABLE Tab1;
DBCC SHRINKFILE ($databaseName1, TRUNCATEONLY);
DBCC SHRINKFILE ($($databaseName1)_Log, TRUNCATEONLY);
"

$null = $db1.Query($sqlGrowthAndShrink)
}
AfterAll {
$db1 | Remove-DbaDatabase -Confirm:$false
}

It "Should find auto growth events in the default trace" {
$results = Find-DbaDbGrowthEvent -SqlInstance $server -Database $databaseName1 -EventType Growth
($results | Where-Object { $_.EventClass -in (92, 93) }).count | Should -BeGreaterThan 0
$results.DatabaseName | unique | Should -Be $databaseName1
$results.DatabaseId | unique | Should -Be $db1.ID
}

<# Leaving this commented out since the background process for auto shrink cannot be triggered
It "Should find auto shrink events in the default trace" {
$results = Find-DbaDbGrowthEvent -SqlInstance $server -Database $databaseName1 -EventType Shrink
$results.EventClass | Should -Contain 94 # data file shrink
$results.EventClass | Should -Contain 95 # log file shrink
$results.DatabaseName | unique | Should -Be $databaseName1
$results.DatabaseId | unique | Should -Be $db1.ID
}
#>
}
}

0 comments on commit 32fd3c4

Please sign in to comment.