Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions public/Export-DbaUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ function Export-DbaUser {
$outsql += "$grantObjectPermission $($objectPermission.PermissionType) ON $object TO [$grantee]$withGrant AS [$($objectPermission.Grantor)];"
}

#Schema Ownership
foreach ($schema in $db.Schemas | Where-Object { $_.Owner -eq $dbuser.Name -and @("sa", "dbo", "information_schema", "sys", "guest") -notcontains $_.Name }) {
if ($Template) {
$ownerName = "{templateUser}"
} else {
$ownerName = $schema.Owner
}
$outsql += "ALTER AUTHORIZATION ON SCHEMA::[{0}] TO [{1}];" -f $schema.Name, $ownerName
}

} catch {
Stop-Function -Message "This user may be using functionality from $($versionName[$db.CompatibilityLevel.ToString()]) that does not exist on the destination version ($versionNameDesc)." -Continue -InnerErrorRecord $_ -Target $db
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Export-DbaUser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Describe $CommandName -Tag IntegrationTests {
$user2 = "dbatoolsci_exportdbauser_user2"
$table = "dbatoolsci_exportdbauser_table"
$role = "dbatoolsci_exportdbauser_role"
$schema = "dbatoolsci_exportdbauser_schema"

$outputPath = "$($TestConfig.Temp)\Dbatoolsci_user_CustomFolder"
$outputFile = "$($TestConfig.Temp)\Dbatoolsci_user_CustomFile.sql"
Expand Down Expand Up @@ -93,6 +94,7 @@ Describe $CommandName -Tag IntegrationTests {
$null = $db.Query("CREATE TABLE $table (C1 INT);")
$null = $db.Query("GRANT SELECT ON OBJECT::$table TO [$user];")
$null = $db.Query("EXEC sp_addrolemember '$role', '$user';")
$null = $db.Query("CREATE SCHEMA [$schema] AUTHORIZATION [$user];")
$null = $db.Query("EXEC sp_addrolemember '$role01', '$user01';")
$null = $db.Query("EXEC sp_addrolemember '$role02', '$user01';")
$null = $db.Query("EXEC sp_addrolemember '$role02', '$user02';")
Expand Down Expand Up @@ -216,4 +218,16 @@ Describe $CommandName -Tag IntegrationTests {
}
}
}

Context "Schema ownership" {
It "Exports schema ownership for users that own schemas" {
$results = Export-DbaUser -SqlInstance $TestConfig.InstanceSingle -Database $dbname -User $user -Passthru
$results | Should -BeLike "*ALTER AUTHORIZATION ON SCHEMA::[[]$schema] TO [[]$user]*"
}

It "Exports schema ownership with template placeholders" {
$results = Export-DbaUser -SqlInstance $TestConfig.InstanceSingle -Database $dbname -User $user -Template -Passthru
$results | Should -BeLike "*ALTER AUTHORIZATION ON SCHEMA::[[]$schema] TO [[]``{templateUser``}]*"
}
}
}
Loading