dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Export-DbaUser should include schema ownership

Open aRickInTheMud opened this issue 1 year ago • 2 comments

Summarize Functionality

This isn't quite a bug because there is not anything broken, per se, so I put it as a feature. But something that tripped us up when recreating security using Export-DbaUser was the user did not have its schema ownership scripted. If you have schema level permissions you are covered, but if you own the whole schema then the output is empty.

Is there a command that is similiar or close to what you are looking for?

Yes

Technical Details

If you stick this somewhere around line 362 it should cover it:

#Schema Ownership
foreach ($schema in $db.Schemas | Where-Object {$_.Owner -eq $dbuser.Name}) {
    $outsql += "ALTER AUTHORIZATION ON SCHEMA::[{0}] TO [{1}]" -f $schema.Name, $schema.Owner
}

aRickInTheMud avatar Aug 29 '24 20:08 aRickInTheMud

Thanks for the request. Seems reasonable @claude pls implement and be sure to add tests

potatoqualitee avatar Nov 28 '25 16:11 potatoqualitee

Claude finished @potatoqualitee's task —— View job • claude/issue-9458-20251128-1635 • Create PR ➔


Implementing schema ownership feature for Export-DbaUser

Tasks

  • [x] Read Export-DbaUser.ps1 to understand current implementation
  • [x] Implement schema ownership scripting feature
  • [x] Read Export-DbaUser.Tests.ps1 to understand test structure
  • [x] Add tests for schema ownership functionality
  • [x] Commit and push changes
  • [x] Generate PR link

Summary

I've successfully implemented the schema ownership feature for Export-DbaUser as requested in issue #9458. Here's what was done:

Implementation Details:

  • Added schema ownership scripting to Export-DbaUser.ps1 (lines 556-564)
  • The code iterates through all schemas owned by each user and generates ALTER AUTHORIZATION ON SCHEMA statements
  • Supports the -Template parameter to replace actual usernames with {templateUser} placeholders
  • Follows dbatools style guidelines: double quotes, proper formatting, SMO-based approach

Tests Added:

  • Created a test schema owned by the test user in the BeforeAll setup
  • Added two tests in a new "Schema ownership" context:
    1. Verifies schema ownership statements are included in exported scripts
    2. Verifies template placeholders work correctly for schema ownership

Code Quality:

  • No backticks used (per style guide)
  • Double quotes for all strings
  • Follows existing code patterns in Export-DbaUser
  • Uses SMO's $db.Schemas collection to enumerate schemas
  • Properly handles the Template parameter for consistency with other exports

--- | Branch: claude/issue-9458-20251128-1635

claude[bot] avatar Nov 28 '25 16:11 claude[bot]