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 - Use correct variable for testing for same database #9032

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
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
50 changes: 25 additions & 25 deletions public/Copy-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ function Copy-DbaDatabase {
SourceServer = $sourceServer.Name
DestinationServer = $destServer.Name
Name = $dbName
DestinationDatabase = $DestinationDbname
DestinationDatabase = $destinationDbName
Type = "Database"
Status = $null
Notes = $null
Expand Down Expand Up @@ -1100,26 +1100,26 @@ function Copy-DbaDatabase {
continue
}

if (($null -ne $destServer.Databases[$DestinationdbName]) -and !$force -and !$WithReplace -and !$Continue) {
if ($Pscmdlet.ShouldProcess($destinstance, "$DestinationdbName exists at destination. Use -Force to drop and migrate. Aborting routine for this database.")) {
Write-Message -Level Verbose -Message "$DestinationdbName exists at destination. Use -Force to drop and migrate. Aborting routine for this database."
if (($null -ne $destServer.Databases[$destinationDbName]) -and !$force -and !$WithReplace -and !$Continue) {
if ($Pscmdlet.ShouldProcess($destinstance, "$destinationDbName exists at destination. Use -Force to drop and migrate. Aborting routine for this database.")) {
Write-Message -Level Verbose -Message "$destinationDbName exists at destination. Use -Force to drop and migrate. Aborting routine for this database."

$copyDatabaseStatus.Status = "Skipped"
$copyDatabaseStatus.Notes = "Already exists on destination"
$copyDatabaseStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
}
continue
} elseif ($null -ne $destServer.Databases[$DestinationdbName] -and $force) {
if ($sourceServer.Name -eq $destServer.Name -and $sourceServer.Databases[$DestinationdbName].Name -eq $destServer.Databases[$DestinationdbName].Name) {
} elseif ($null -ne $destServer.Databases[$destinationDbName] -and $force) {
if ($sourceServer.Name -eq $destServer.Name -and $dbName -eq $destinationDbName) {
Write-Message -Level Verbose -Message "Source and destination database are the same. Aborting routine for this database."
$copyDatabaseStatus.Status = "Failed"
$copyDatabaseStatus.Notes = "Source and destination database are the same."
$copyDatabaseStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
continue
}
if ($Pscmdlet.ShouldProcess($destinstance, "DROP DATABASE $DestinationdbName")) {
Write-Message -Level Verbose -Message "$DestinationdbName already exists. -Force was specified. Dropping $DestinationdbName on $destinstance."
$removeresult = Remove-DbaDatabase -SqlInstance $destserver -Database $DestinationdbName -Confirm:$false
if ($Pscmdlet.ShouldProcess($destinstance, "DROP DATABASE $destinationDbName")) {
Write-Message -Level Verbose -Message "$destinationDbName already exists. -Force was specified. Dropping $destinationDbName on $destinstance."
$removeresult = Remove-DbaDatabase -SqlInstance $destserver -Database $destinationDbName -Confirm:$false
$dropResult = $removeresult.Status -eq 'Dropped'

if ($dropResult -eq $false) {
Expand Down Expand Up @@ -1213,9 +1213,9 @@ function Copy-DbaDatabase {
try {
$msg = $null
if ($miRestore) {
$restoreResultTmp = $backupTmpResult | Restore-DbaDatabase -SqlInstance $destServer -DatabaseName $DestinationdbName -TrustDbBackupHistory -WithReplace:$WithReplace -EnableException -AzureCredential $AzureCredential
$restoreResultTmp = $backupTmpResult | Restore-DbaDatabase -SqlInstance $destServer -DatabaseName $destinationDbName -TrustDbBackupHistory -WithReplace:$WithReplace -EnableException -AzureCredential $AzureCredential
} else {
$restoreResultTmp = $backupTmpResult | Restore-DbaDatabase -SqlInstance $destServer -DatabaseName $DestinationdbName -ReuseSourceFolderStructure:$ReuseSourceFolderStructure -NoRecovery:$NoRecovery -TrustDbBackupHistory -WithReplace:$WithReplace -Continue:$Continue -EnableException -ReplaceDbNameInFile -AzureCredential $AzureCredential -KeepCDC:$KeepCDC -KeepReplication:$KeepReplication
$restoreResultTmp = $backupTmpResult | Restore-DbaDatabase -SqlInstance $destServer -DatabaseName $destinationDbName -ReuseSourceFolderStructure:$ReuseSourceFolderStructure -NoRecovery:$NoRecovery -TrustDbBackupHistory -WithReplace:$WithReplace -Continue:$Continue -EnableException -ReplaceDbNameInFile -AzureCredential $AzureCredential -KeepCDC:$KeepCDC -KeepReplication:$KeepReplication
}
} catch {
$msg = $_.Exception.InnerException.InnerException.InnerException.InnerException.Message
Expand Down Expand Up @@ -1358,46 +1358,46 @@ function Copy-DbaDatabase {
}
}
}
$NewDatabase = Get-DbaDatabase -SqlInstance $destServer -database $DestinationdbName
$NewDatabase = Get-DbaDatabase -SqlInstance $destServer -database $destinationDbName

$propfailures = @()

# restore potentially lost settings
if ($destServer.VersionMajor -ge 9 -and $NoRecovery -eq $false) {
if ($sourceDbOwnerChaining -ne $NewDatabase.DatabaseOwnershipChaining) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating DatabaseOwnershipChaining on $DestinationdbName")) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating DatabaseOwnershipChaining on $destinationDbName")) {
try {
$NewDatabase.DatabaseOwnershipChaining = $sourceDbOwnerChaining
$NewDatabase.Alter()
Write-Message -Level Verbose -Message "Successfully updated DatabaseOwnershipChaining for $sourceDbOwnerChaining on $DestinationdbName on $destinstance."
Write-Message -Level Verbose -Message "Successfully updated DatabaseOwnershipChaining for $sourceDbOwnerChaining on $destinationDbName on $destinstance."
} catch {
Write-Message -Level Warning -Message "Failed to update DatabaseOwnershipChaining for $sourceDbOwnerChaining on $DestinationdbName on $destinstance."
Write-Message -Level Warning -Message "Failed to update DatabaseOwnershipChaining for $sourceDbOwnerChaining on $destinationDbName on $destinstance."
$propfailures += "Ownership chaining"
}
}
}

if ($sourceDbTrustworthy -ne $NewDatabase.Trustworthy) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating Trustworthy on $DestinationdbName")) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating Trustworthy on $destinationDbName")) {
try {
$NewDatabase.Trustworthy = $sourceDbTrustworthy
$NewDatabase.Alter()
Write-Message -Level Verbose -Message "Successfully updated Trustworthy to $sourceDbTrustworthy for $DestinationdbName on $destinstance"
Write-Message -Level Verbose -Message "Successfully updated Trustworthy to $sourceDbTrustworthy for $destinationDbName on $destinstance"
} catch {
Write-Message -Level Warning -Message "Failed to update Trustworthy to $sourceDbTrustworthy for $DestinationdbName on $destinstance."
Write-Message -Level Warning -Message "Failed to update Trustworthy to $sourceDbTrustworthy for $destinationDbName on $destinstance."
$propfailures += "Trustworthy"
}
}
}

if ($sourceDbBrokerEnabled -ne $NewDatabase.BrokerEnabled) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating BrokerEnabled on $DestinationDbName")) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating BrokerEnabled on $destinationDbName")) {
try {
$NewDatabase.BrokerEnabled = $sourceDbBrokerEnabled
$NewDatabase.Alter()
Write-Message -Level Verbose -Message "Successfully updated BrokerEnabled to $sourceDbBrokerEnabled for $DestinationdbName on $destinstance."
Write-Message -Level Verbose -Message "Successfully updated BrokerEnabled to $sourceDbBrokerEnabled for $destinationDbName on $destinstance."
} catch {
Write-Message -Level Warning -Message "Failed to update BrokerEnabled to $sourceDbBrokerEnabled for $DestinationdbName on $destinstance."
Write-Message -Level Warning -Message "Failed to update BrokerEnabled to $sourceDbBrokerEnabled for $destinationDbName on $destinstance."

$propfailures += "Message broker"
}
Expand All @@ -1406,15 +1406,15 @@ function Copy-DbaDatabase {
}

if ($sourceDbReadOnly -ne $NewDatabase.ReadOnly -and -not $NoRecovery) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating ReadOnly status on $DestinationdbName")) {
if ($Pscmdlet.ShouldProcess($destinstance, "Updating ReadOnly status on $destinationDbName")) {
try {
if ($sourceDbReadOnly) {
$result = Set-DbaDbState -SqlInstance $destserver -Database $DestinationdbName -ReadOnly -EnableException
$result = Set-DbaDbState -SqlInstance $destserver -Database $destinationDbName -ReadOnly -EnableException
} else {
$result = Set-DbaDbState -SqlInstance $destserver -Database $DestinationdbName -ReadWrite -EnableException
$result = Set-DbaDbState -SqlInstance $destserver -Database $destinationDbName -ReadWrite -EnableException
}
} catch {
Write-Message -Level Verbose -Message "Failed to update ReadOnly status on $DestinationdbName."
Write-Message -Level Verbose -Message "Failed to update ReadOnly status on $destinationDbName."
$propfailures = "Read only"
}
}
Expand Down