Skip to content

Commit 98f3c52

Browse files
Find-DbaObject - Fix missing database DDL trigger matches (review of #10321)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7e66769 commit 98f3c52

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

docs/trackers/features/commit-bug-review-TRACKER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
137137
| a38bc6b35 | Get-DbaDbIdentity, Set-DbaDbIdentity, Invoke-DbaDbDbccUpdateUsage - Normalize table names (#10318) | DONE | Fixed escaped-bracket table name regression and added unit regression tests. |
138138
| 9899bd274 | Copy-DbaDbTableData - Add -ScriptingOptionsObject parameter (#10317) | DONE | Propagated ScriptingOptionsObject to Copy-DbaDbViewData and added unit coverage. |
139139
| 0c486b964 | Backup-DbaDbCertificate: Don't use decryption password if cert encrypted by master key (#10329) | DONE | Reviewed SMO export overload selection for master-key-encrypted certs; no bugs found. |
140-
| db77a3476 | Find-DbaObject - Add unified command to search database objects by name (#10321) | PENDING | |
140+
| db77a3476 | Find-DbaObject - Add unified command to search database objects by name (#10321) | DONE | Added database DDL trigger name search via sys.triggers and covered it with an integration regression test. |
141141
| 6416b4e91 | Add Compare-DbaLogin command (#10319) | PENDING | |
142142
| bee08f8e7 | Copy-DbaSsisCatalog - Add standard MigrationObject output, integrate with Start-DbaMigration (#10311) | PENDING | |
143143
| 21e4795ee | Get-DbaService - Add PowerBI Report Server detection (#10298) | PENDING | |

public/Find-DbaObject.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Find-DbaObject {
4444
- ScalarFunction: Scalar-valued functions (sys.objects type FN)
4545
- TableValuedFunction: Inline and multi-statement table-valued functions (sys.objects type IF/TF)
4646
- Synonym: Synonyms (sys.objects type SN)
47-
- Trigger: SQL triggers (sys.objects type TR)
47+
- Trigger: Object-level DML triggers plus database DDL SQL triggers
4848
- All: All of the above (default)
4949
5050
.PARAMETER IncludeColumns
@@ -86,7 +86,7 @@ function Find-DbaObject {
8686
- ComputerName: The computer name of the SQL Server instance
8787
- SqlInstance: The SQL Server instance name
8888
- Database: The database containing the matched object
89-
- Schema: The schema of the matched object
89+
- Schema: The schema of the matched object (null for database DDL triggers)
9090
- Name: The name of the matched object
9191
- ObjectType: The SQL Server type description (e.g., USER_TABLE, VIEW, SQL_STORED_PROCEDURE)
9292
- MatchType: "ObjectName" when the object name matched, "ColumnName" when a column name matched
@@ -159,7 +159,9 @@ function Find-DbaObject {
159159
$typeFilter = ($typeCodes | Select-Object -Unique) -join ", "
160160
}
161161

162+
$includeDatabaseTriggers = "All" -in $ObjectType -or "Trigger" -in $ObjectType
162163
$sysFilter = if ($IncludeSystemObjects) { "" } else { "AND o.is_ms_shipped = 0" }
164+
$triggerFilter = if ($IncludeSystemObjects) { "" } else { "AND tr.is_ms_shipped = 0" }
163165

164166
$sqlObjects = "
165167
SELECT
@@ -173,6 +175,22 @@ function Find-DbaObject {
173175
WHERE o.type IN ($typeFilter)
174176
$sysFilter"
175177

178+
if ($includeDatabaseTriggers) {
179+
$sqlObjects += "
180+
UNION ALL
181+
SELECT
182+
CAST(NULL AS sysname) AS SchemaName,
183+
tr.name AS ObjectName,
184+
RTRIM(tr.type) AS ObjectTypeCode,
185+
tr.type_desc AS ObjectType,
186+
tr.create_date AS CreateDate,
187+
tr.modify_date AS LastModified
188+
FROM sys.triggers tr
189+
WHERE tr.parent_class = 0
190+
AND tr.type = 'TR'
191+
$triggerFilter"
192+
}
193+
176194
$sqlColumns = "
177195
SELECT
178196
OBJECT_SCHEMA_NAME(c.object_id) AS SchemaName,

tests/Find-DbaObject.Tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ CREATE PROCEDURE dbo.usp_GetServiceOrders
6363
AS
6464
SELECT * FROM dbo.ServiceOrder;
6565
GO
66+
67+
CREATE TRIGGER trg_ServiceAudit
68+
ON DATABASE
69+
FOR CREATE_TABLE
70+
AS
71+
BEGIN
72+
SET NOCOUNT ON;
73+
END;
74+
GO
6675
"@
6776
$splatCreateObjects = @{
6877
SqlInstance = $TestConfig.InstanceSingle
@@ -124,6 +133,19 @@ GO
124133
$results.Name | Should -Contain "usp_GetServiceOrders"
125134
}
126135

136+
It "Should find database DDL triggers whose names match the pattern" {
137+
$splatFind = @{
138+
SqlInstance = $TestConfig.InstanceSingle
139+
Database = $testDbName
140+
Pattern = "ServiceAudit"
141+
ObjectType = "Trigger"
142+
}
143+
$results = Find-DbaObject @splatFind
144+
$results | Should -Not -BeNullOrEmpty
145+
$results.Name | Should -Contain "trg_ServiceAudit"
146+
$results.Schema | Should -BeNullOrEmpty
147+
}
148+
127149
It "Should return MatchType of ObjectName for object name matches" {
128150
$splatFind = @{
129151
SqlInstance = $TestConfig.InstanceSingle

0 commit comments

Comments
 (0)