Skip to content

Export-DbaCredential - Add IF NOT EXISTS guard to exported SQL scripts#10295

Open
andreasjordan wants to merge 1 commit intodevelopmentfrom
claude/issue-8618-20260331-0625
Open

Export-DbaCredential - Add IF NOT EXISTS guard to exported SQL scripts#10295
andreasjordan wants to merge 1 commit intodevelopmentfrom
claude/issue-8618-20260331-0625

Conversation

@andreasjordan
Copy link
Copy Markdown
Collaborator

Wraps each CREATE CREDENTIAL statement with an IF NOT EXISTS check against sys.credentials so that exported scripts are idempotent and only create missing credentials during migrations or DR script replays.

Closes #8618

Generated with Claude Code

Wraps each CREATE CREDENTIAL statement with an IF NOT EXISTS check against
sys.credentials so that exported scripts are idempotent and only create
missing credentials during migrations or DR script replays.

Closes #8618

(do Export-DbaCredential)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Copy Markdown
Collaborator Author

@claude please read the other Export-Dba* commands and see if this pattern should be applied there as well.

@claude
Copy link
Copy Markdown

claude bot commented Mar 31, 2026

Claude finished @andreasjordan's task in 1m 26s —— View job


Analysis: IF NOT EXISTS Pattern Across Export-Dba* Commands

  • Find all Export-Dba* commands
  • Analyze each command for CREATE statements that could use IF NOT EXISTS
  • Report findings

Here's the status of each Export-Dba* command that generates executable SQL statements:

Command Status Notes
Export-DbaCredential ✅ Added (this PR) IF NOT EXISTS (SELECT 1 FROM sys.credentials ...)
Export-DbaLogin ✅ Already handled IF NOT EXISTS (SELECT loginname FROM master.dbo.syslogins ...) (lines 410, 414)
Export-DbaServerRole ✅ Already handled ScriptingOptionsObject.IncludeIfNotExists = $true (line 230)
Export-DbaUser ✅ Already handled ScriptingOptionsObject.IncludeIfNotExists = $true (line 250)
Export-DbaSpConfigure ✅ N/A Generates EXEC sp_configure calls, not CREATE statements
Export-DbaDbRole ⚠️ Gap found Explicitly sets IncludeIfNotExists = $false (line 261)
Export-DbaLinkedServer ⚠️ Partial gap Uses SMO Script() which generates EXEC sp_addlinkedserver — no idempotency guard
Export-DbaSysDbUserObject ℹ️ Minor Delegates to Export-DbaScript; IncludeIfNotExists not set in scripting options

Key Finding: Export-DbaDbRole

This is the clearest gap. While Export-DbaServerRole and Export-DbaUser both set IncludeIfNotExists = $true, Export-DbaDbRole explicitly sets it to $false at public/Export-DbaDbRole.ps1:261:

$ScriptingOptionsObject.IncludeIfNotExists = $false

Changing this to $true would make Export-DbaDbRole consistent with its sibling commands and make its generated CREATE ROLE statements idempotent.

Fix this →

Secondary Finding: Export-DbaLinkedServer

This command uses SMO's Script() method which generates EXEC sp_addlinkedserver / EXEC sp_addlinkedsrvlogin stored procedure calls rather than CREATE statements. There's no idempotency guard — if a linked server already exists, the script will fail. However, fixing this would require a custom approach (checking sys.servers) similar to what was done for credentials, rather than just flipping an SMO flag. That's a more involved change worth its own issue/PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

If EXIST Logic check for Export-DbaCredential

1 participant