Skip to content

Commit 3cf3898

Browse files
Get-DbaDbMailHistory - Make the Status filter work on case sensitive instances (#10624)
1 parent d1b18d4 commit 3cf3898

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

public/Get-DbaDbMailHistory.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ function Get-DbaDbMailHistory {
167167
}
168168

169169
if ($Status) {
170-
$Status = $Status -join "', '"
171-
$wherearray += "sent_status IN ('$Status')"
170+
# The sysmail_allitems view emits sent_status in lowercase, so compare lowercased or the
171+
# documented values (Sent, Unsent, Failed, Retrying) never match on a case sensitive instance.
172+
# Invariantly: a culture-sensitive lowercase turns the I of FAILED or RETRYING into a
173+
# dotless i under Turkish rules, which never matches the ASCII tokens sysmail stores.
174+
$Status = ($Status -join "', '").ToLowerInvariant()
175+
$wherearray += "LOWER(sent_status) IN ('$Status')"
172176
}
173177

174178
$wherearray = $wherearray -join ' AND '

tests/Get-DbaDbMailHistory.Tests.ps1

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,45 @@ Describe $CommandName -Tag IntegrationTests {
6868
,[last_mod_date]
6969
,[last_mod_user])
7070
VALUES
71-
($profile_id,'dbatoolssci@dbatools.io',NULL,NULL,'Test Job',NULL,NULL,'A Test Job failed to run','TEXT','Normal','Normal',NULL,'MIME',NULL,NULL,
71+
($profile_id,'dbatoolssci@dbatools.io',NULL,NULL,'Test Job',NULL,NULL,'A Test Job failed to run','TEXT','NORMAL','NORMAL',NULL,'MIME',NULL,NULL,
7272
0,1,256,'',0,0,'2018-12-9 11:44:32.600','dbatools\dbatoolssci',1,1,'2018-12-9 11:44:33.000','2018-12-9 11:44:33.273','sa')"
7373
)
74+
# A second row with sent_status = 2 (failed): FAILED is a status whose uppercase form
75+
# contains the letter I, which a culture-sensitive lowercase turns into a dotless i under
76+
# the Turkish culture - the culture regression below needs exactly this row.
77+
$server.Query("INSERT INTO msdb.[dbo].[sysmail_mailitems]
78+
([profile_id]
79+
,[recipients]
80+
,[copy_recipients]
81+
,[blind_copy_recipients]
82+
,[subject]
83+
,[from_address]
84+
,[reply_to]
85+
,[body]
86+
,[body_format]
87+
,[importance]
88+
,[sensitivity]
89+
,[file_attachments]
90+
,[attachment_encoding]
91+
,[query]
92+
,[execute_query_database]
93+
,[attach_query_result_as_file]
94+
,[query_result_header]
95+
,[query_result_width]
96+
,[query_result_separator]
97+
,[exclude_query_output]
98+
,[append_query_error]
99+
,[send_request_date]
100+
,[send_request_user]
101+
,[sent_account_id]
102+
,[sent_status]
103+
,[sent_date]
104+
,[last_mod_date]
105+
,[last_mod_user])
106+
VALUES
107+
($profile_id,'dbatoolssci@dbatools.io',NULL,NULL,'Test Job Failed',NULL,NULL,'A Test Job failed to run','TEXT','NORMAL','NORMAL',NULL,'MIME',NULL,NULL,
108+
0,1,256,'',0,0,'2018-12-9 11:44:32.600','dbatools\dbatoolssci',1,2,'2018-12-9 11:44:33.000','2018-12-9 11:44:33.273','sa')"
109+
)
74110

75111
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
76112
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
@@ -126,6 +162,26 @@ Describe $CommandName -Tag IntegrationTests {
126162
}
127163
}
128164

165+
Context "Gets Db Mail History using -Status under a Turkish culture" {
166+
BeforeAll {
167+
# ValidateSet accepts case-insensitive input, so FAILED reaches the command as typed.
168+
# Its uppercase I is the letter a culture-sensitive lowercase turns into a dotless i
169+
# under Turkish rules, which can never match the ASCII tokens sysmail stores - the
170+
# command has to normalize invariantly.
171+
$cultureBefore = [System.Globalization.CultureInfo]::CurrentCulture
172+
try {
173+
[System.Globalization.CultureInfo]::CurrentCulture = "tr-TR"
174+
$resultsTurkish = Get-DbaDbMailHistory -SqlInstance $TestConfig.InstanceSingle -Status FAILED
175+
} finally {
176+
[System.Globalization.CultureInfo]::CurrentCulture = $cultureBefore
177+
}
178+
}
179+
180+
It "Finds the failed mail although the culture lowercases differently" {
181+
$resultsTurkish.SentStatus | Should -Be "Failed"
182+
}
183+
}
184+
129185
Context "Gets Db Mail History using -Since" {
130186
BeforeAll {
131187
$results = Get-DbaDbMailHistory -SqlInstance $TestConfig.InstanceSingle -Since "2018-01-01"

0 commit comments

Comments
 (0)