Skip to content
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

Copy-DbaDatabase Readonly - fixes #630 #1857

Merged
merged 1 commit into from
Jul 23, 2017
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
2 changes: 1 addition & 1 deletion functions/Copy-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ function Copy-DbaDatabase {
if ($SetSourceReadOnly) {
If ($Pscmdlet.ShouldProcess($source, "Set $dbName to read-only")) {
Write-Message -Level Verbose -Message "Setting database to read-only"
$result = Update-SqldbReadOnly -SqlInstance $sourceServer -dbname $dbName -readonly $true
$result = Update-SqldbReadOnly -SqlInstance $sourceServer -dbname $dbName -readonly:$true

if ($result -eq $false) {
Write-Message -Level Warning -Message "Couldn't set database to read-only. Aborting routine for this database"
Expand Down
20 changes: 8 additions & 12 deletions internal/Update-SqlDbReadOnly.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Function Update-SqlDbReadOnly
{
Function Update-SqlDbReadOnly {
<#
.SYNOPSIS
Internal function. Updates specified database to read-only or read-write. Necessary because SMO doesn't appear to support NO_WAIT.
Expand All @@ -18,24 +17,21 @@ Internal function. Updates specified database to read-only or read-write. Necess
[bool]$readonly
)

if ($readonly)
{
if ($readonly) {
Stop-DbaProcess -SqlInstance $SqlInstance -Database $dbname
$sql = "ALTER DATABASE [$dbname] SET READ_ONLY WITH NO_WAIT"
}
else
{
else {
$sql = "ALTER DATABASE [$dbname] SET READ_WRITE WITH NO_WAIT"
}

try
{
$server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
try {
$server = Connect-SqlInstance -SqlInstance $SqlInstance
$null = $server.Query($sql)
Write-Output "Changed ReadOnly status to $readonly for $dbname on $($server.name)"
Write-Verbose "Changed ReadOnly status to $readonly for $dbname on $($server.name)"
return $true
}
catch
{
catch {
Write-Error "Could not change readonly status for $dbname on $($server.name)"
return $false
}
Expand Down