Skip to content

Export-DbaUser - Add schema ownership to exported scripts#10275

Draft
andreasjordan wants to merge 5 commits intodevelopmentfrom
claude/issue-9458-20251128-1635
Draft

Export-DbaUser - Add schema ownership to exported scripts#10275
andreasjordan wants to merge 5 commits intodevelopmentfrom
claude/issue-9458-20251128-1635

Conversation

@andreasjordan
Copy link
Collaborator

This PR adds schema ownership information to the output of Export-DbaUser.

Previously, if a user owned a schema, that ownership was not included in the exported script, causing issues when recreating security contexts.

Changes:

  • Added schema ownership iteration and ALTER AUTHORIZATION statement generation
  • Supports -Template parameter for placeholder substitution
  • Added comprehensive tests for schema ownership export

Fixes #9458


Generated with Claude Code

github-actions bot and others added 3 commits November 28, 2025 16:37
This change adds schema ownership information to the output of Export-DbaUser.
Previously, if a user owned a schema, that ownership was not included in the
exported script, causing issues when recreating security contexts.

The implementation:
- Iterates through schemas owned by the user
- Generates ALTER AUTHORIZATION statements for each owned schema
- Supports the -Template parameter for placeholder substitution
- Follows dbatools style guidelines (double quotes, proper formatting)

Tests added:
- Verify schema ownership is exported for users that own schemas
- Verify template placeholders work correctly for schema ownership

Fixes #9458

(do Export-DbaUser)

Co-authored-by: Chrissy LeMaire <potatoqualitee@users.noreply.github.com>
@andreasjordan
Copy link
Collaborator Author

@claude we need to exclude the schems owned by @("sa", "dbo", "information_schema", "sys") like in lines 388 and 479.

@claude
Copy link

claude bot commented Mar 21, 2026

Claude finished @andreasjordan's task in 39s —— View job


Tasks

  • Read the changed code to understand the current schema ownership implementation
  • Add exclusion filter for sa, dbo, information_schema, sys schemas
  • Commit and push the change

Done

Added the exclusion filter to the #Schema Ownership loop in Export-DbaUser.ps1 (line 566), matching the same pattern used at lines 388 and 479:

foreach ($schema in $db.Schemas | Where-Object { $_.Owner -eq $dbuser.Name -and @("sa", "dbo", "information_schema", "sys") -notcontains $_.Name }) {

This excludes schemas named sa, dbo, information_schema, or sys from the ALTER AUTHORIZATION output, consistent with how the existing database and object permission exports filter out those system principals.
|

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

@claude we need to exclude the schema "guest" as well.

@claude
Copy link

claude bot commented Mar 21, 2026

Claude finished @andreasjordan's task in 33s —— View job


Tasks

  • Read the changed code to understand the current schema ownership implementation
  • Add "guest" to the exclusion filter for schema ownership
  • Commit and push the change

Done

Added "guest" to the exclusion list in the #Schema Ownership loop in Export-DbaUser.ps1 (line 566):

foreach ($schema in $db.Schemas | Where-Object { $_.Owner -eq $dbuser.Name -and @("sa", "dbo", "information_schema", "sys", "guest") -notcontains $_.Name }) {

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
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.

Export-DbaUser should include schema ownership

1 participant