-
-
Notifications
You must be signed in to change notification settings - Fork 848
Copy-DbaAgentJob - Add UseLastModified parameter for intelligent date-based synchronization #9956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…-based synchronization (do Copy-DbaAgentJob) Co-authored-by: Chrissy LeMaire <[email protected]>
|
gonna fix all of these failures locally. just gotta install some sqls |
Added logic to set the destination job's date_modified to match the source after copying in Copy-DbaAgentJob integration tests. This ensures consistency for date_modified between source and destination jobs, improving test reliability. (do Copy-DbaAgentJob)
Code Review for PR #9956I've reviewed the implementation of the ✅ Strengths
🔴 Critical Issues1. SQL Injection Vulnerability (Security)Lines 250-252 in the diff: $escapedJobName = $jobName.Replace("'", "''")
$sourceDate = ($sourceserver.Query($sqlDateQuery.Replace("@jobName", "'$escapedJobName'"))).date_modified
$destDate = ($destServer.Query($sqlDateQuery.Replace("@jobName", "'$escapedJobName'"))).date_modifiedProblem: Using string replacement for SQL parameters is vulnerable to SQL injection and doesn't follow dbatools best practices. Solution: Use parameterized queries with $splatSourceDate = @{
SqlInstance = $sourceserver
Database = "msdb"
Query = "SELECT date_modified FROM dbo.sysjobs WHERE name = @jobName"
SqlParameter = @{ jobName = $jobName }
}
$sourceDate = (Invoke-DbaQuery @splatSourceDate).date_modified
$splatDestDate = @{
SqlInstance = $destServer
Database = "msdb"
Query = "SELECT date_modified FROM dbo.sysjobs WHERE name = @jobName"
SqlParameter = @{ jobName = $jobName }
}
$destDate = (Invoke-DbaQuery @splatDestDate).date_modifiedThis is safer and follows dbatools conventions. See the test file (line 163) where this pattern is already used correctly. 2. Style Guide Violation: Splat Usage (Code Quality)Per CLAUDE.md, splats are required for 3+ parameters but should NOT be used for 1-2 parameters. Lines throughout the new test code use splats for 3-4 parameters - This is correct ✅ However, ensure consistency throughout. The pattern looks good in the tests.
|
|
@claude but this PR didnt add that breaking continue right? sometimes we had to do silly looking things to accommodate whatif |
|
Claude finished @potatoqualitee's task —— View job Reviewing my previous feedbackGood question! Let me verify whether this PR actually introduced the Tasks:
My apologies - you're absolutely right!After reviewing the actual diff, I can confirm that this PR did NOT introduce the The pattern of placing if ($Pscmdlet.ShouldProcess($destinstance, "Job $jobName exists at destination. Use -Force to drop and migrate.")) {
$copyJobStatus.Status = "Skipped"
# ... output status
}
continue # <-- This was already here before your PRYour PR simply followed the existing codebase pattern when implementing the new Updated Review - What This PR Actually AddsLooking at what was actually changed, here's the corrected feedback: ✅ What's Good About This PR
|
|
@claude thanks pls fix the SQL parameterization. |
|
Claude finished @potatoqualitee's task —— View job SQL parameterization issue fixedTasks:
What was fixedReplaced the SQL injection vulnerability in the Before (lines 248-253): $sqlDateQuery = "SELECT date_modified FROM msdb.dbo.sysjobs WHERE name = @jobName"
$escapedJobName = $jobName.Replace("'", "''")
$sourceDate = ($sourceserver.Query($sqlDateQuery.Replace("@jobName", "'$escapedJobName'"))).date_modified
$destDate = ($destServer.Query($sqlDateQuery.Replace("@jobName", "'$escapedJobName'"))).date_modifiedAfter: $splatSourceDate = @{
SqlInstance = $sourceserver
Database = "msdb"
Query = "SELECT date_modified FROM dbo.sysjobs WHERE name = @jobName"
SqlParameter = @{ jobName = $jobName }
}
$sourceDate = (Invoke-DbaQuery @splatSourceDate).date_modified
$splatDestDate = @{
SqlInstance = $destServer
Database = "msdb"
Query = "SELECT date_modified FROM dbo.sysjobs WHERE name = @jobName"
SqlParameter = @{ jobName = $jobName }
}
$destDate = (Invoke-DbaQuery @splatDestDate).date_modifiedChanges made
The code now safely handles job names with special characters and follows dbatools security best practices. --- | Branch |
…ized queries Replace string concatenation with Invoke-DbaQuery and SqlParameter for date_modified queries. This ensures safe SQL execution and follows dbatools best practices. (do Copy-DbaAgentJob) Co-authored-by: Chrissy LeMaire <[email protected]>
Summary
Adds a new
-UseLastModifiedparameter toCopy-DbaAgentJobthat implements intelligent date-based synchronization as requested in #5189.Changes
-UseLastModifiedswitch parameterdate_modifiedfrommsdb.dbo.sysjobsBehavior
When
-UseLastModifiedis enabled:Backward compatible - existing behavior unchanged when parameter not used.
Fixes #5189
Generated with Claude Code