Skip to content

Commit

Permalink
Migration fixes (#5414)
Browse files Browse the repository at this point in the history
* add supports should process

* add StartupProcedures to start-dbamigration
  • Loading branch information
potatoqualitee authored Apr 26, 2019
1 parent 1b3e4df commit 591b1cf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
11 changes: 9 additions & 2 deletions functions/Start-DbaMigration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function Start-DbaMigration {
All Resource Governor objects. Use -Exclude ResourceGovernor to skip.
All Server Audit Specifications. Use -Exclude ServerAuditSpecifications to skip.
All Custom Errors (User Defined Messages). Use -Exclude CustomErrors to skip.
Copies All Data Collector collection sets. Does not configure the server. Use -Exclude DataCollector to skip.
All Data Collector collection sets. Does not configure the server. Use -Exclude DataCollector to skip.
All startup procedures. Use -Exclude StartupProcedures to skip.
This script provides the ability to migrate databases using detach/copy/attach or backup/restore. SQL Server logins, including passwords, SID and database/server roles can also be migrated. In addition, job server objects can be migrated and server configuration settings can be exported or migrated. This script works with named instances, clusters and SQL Express.
Expand Down Expand Up @@ -104,6 +105,7 @@ function Start-DbaMigration {
ServerAuditSpecifications
CustomErrors
DataCollector
StartupProcedures
.PARAMETER ExcludeSaRename
If this switch is enabled, the sa account will not be renamed on the destination instance to match the source.
Expand Down Expand Up @@ -204,7 +206,7 @@ function Start-DbaMigration {
[switch]$IncludeSupportDbs,
[PSCredential]$SourceSqlCredential,
[PSCredential]$DestinationSqlCredential,
[ValidateSet('Databases', 'Logins', 'AgentServer', 'Credentials', 'LinkedServers', 'SpConfigure', 'CentralManagementServer', 'DatabaseMail', 'SysDbUserObjects', 'SystemTriggers', 'BackupDevices', 'Audits', 'Endpoints', 'ExtendedEvents', 'PolicyManagement', 'ResourceGovernor', 'ServerAuditSpecifications', 'CustomErrors', 'DataCollector')]
[ValidateSet('Databases', 'Logins', 'AgentServer', 'Credentials', 'LinkedServers', 'SpConfigure', 'CentralManagementServer', 'DatabaseMail', 'SysDbUserObjects', 'SystemTriggers', 'BackupDevices', 'Audits', 'Endpoints', 'ExtendedEvents', 'PolicyManagement', 'ResourceGovernor', 'ServerAuditSpecifications', 'CustomErrors', 'DataCollector', 'StartupProcedures')]
[string[]]$Exclude,
[switch]$DisableJobsOnDestination,
[switch]$DisableJobsOnSource,
Expand Down Expand Up @@ -405,6 +407,11 @@ function Start-DbaMigration {
Write-Message -Level Verbose -Message "Migrating job server"
Copy-DbaAgentServer -Source $sourceserver -Destination $Destination -DestinationSqlCredential $DestinationSqlCredential -DisableJobsOnDestination:$DisableJobsOnDestination -DisableJobsOnSource:$DisableJobsOnSource -Force:$Force
}

if ($Exclude -notcontains 'StartupProcedures') {
Write-Message -Level Verbose -Message "Migrating startup procedures"
Copy-DbaStartupProcedure -Source $sourceserver -Destination $Destination -DestinationSqlCredential $DestinationSqlCredential
}
}
end {
if (Test-FunctionInterrupt) { return }
Expand Down
30 changes: 16 additions & 14 deletions internal/functions/Update-SqlDbOwner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Update-SqlDbOwner {
Internal function. Updates specified database dbowner.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
Expand Down Expand Up @@ -41,7 +41,7 @@ function Update-SqlDbOwner {
Write-Message -Level Verbose -Message "Database [$dbName] is part of an availability group. Skipping."
continue
}

if ($null -eq $dbowner -or $null -eq $destServer.logins[$dbowner]) {
try {
$dbowner = ($destServer.logins | Where-Object { $_.id -eq 1 }).Name
Expand All @@ -50,21 +50,23 @@ function Update-SqlDbOwner {
}
}

try {
if ($destdb.ReadOnly -eq $true) {
$changeroback = $true
Update-SqlDbReadOnly $destServer $DbName $false
}
if ($Pscmdlet.ShouldProcess($destdb.Parent.Name, "Setting $destdb owner to $dbowner")) {
try {
if ($destdb.ReadOnly -eq $true) {
$changeroback = $true
Update-SqlDbReadOnly $destServer $DbName $false
}

$destdb.SetOwner($dbowner)
Write-Output "Changed $DbName owner to $dbowner"
$destdb.SetOwner($dbowner)
Write-Output "Changed $DbName owner to $dbowner"

if ($changeroback) {
Update-SqlDbReadOnly $destServer $DbName $true
$changeroback = $null
if ($changeroback) {
Update-SqlDbReadOnly $destServer $DbName $true
$changeroback = $null
}
} catch {
Stop-Function -Message "Failed to update $DbName owner to $dbowner." -ErrorRecord $_
}
} catch {
Stop-Function -Message "Failed to update $DbName owner to $dbowner." -ErrorRecord $_
}
} else {
Write-Message -Level Verbose -Message "Proper owner already set on $DbName"
Expand Down
10 changes: 7 additions & 3 deletions internal/functions/Update-SqlDbReadOnly.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Update-SqlDbReadOnly {
Internal function. Updates specified database to read-only or read-write. Necessary because SMO doesn't appear to support NO_WAIT.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
Expand All @@ -19,15 +19,19 @@ function Update-SqlDbReadOnly {
)

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

try {
$server = Connect-SqlInstance -SqlInstance $SqlInstance
$null = $server.Query($sql)
if ($Pscmdlet.ShouldProcess($server.Name, "Setting $dbname to readonly")) {
if ($readonly) {
Stop-DbaProcess -SqlInstance $SqlInstance -Database $dbname
}
$null = $server.Query($sql)
}
Write-Message -Level Verbose -Message "Changed ReadOnly status to $readonly for $dbname on $($server.name)"
return $true
} catch {
Expand Down
7 changes: 4 additions & 3 deletions tests/Start-DbaMigration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'Source', 'Destination', 'DetachAttach', 'Reattach', 'BackupRestore', 'SharedPath', 'WithReplace', 'NoRecovery', 'AzureCredential', 'SetSourceReadOnly', 'ReuseSourceFolderStructure', 'IncludeSupportDbs', 'SourceSqlCredential', 'DestinationSqlCredential', 'Exclude', 'DisableJobsOnDestination', 'DisableJobsOnSource', 'ExcludeSaRename', 'UseLastBackup', 'Continue', 'Force', 'EnableException'
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'Source', 'Destination', 'DetachAttach', 'Reattach', 'BackupRestore', 'SharedPath', 'WithReplace', 'NoRecovery', 'AzureCredential', 'SetSourceReadOnly', 'ReuseSourceFolderStructure', 'IncludeSupportDbs', 'SourceSqlCredential', 'DestinationSqlCredential', 'Exclude', 'DisableJobsOnDestination', 'DisableJobsOnSource', 'ExcludeSaRename', 'UseLastBackup', 'Continue', 'Force', 'EnableException'

$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
Expand Down Expand Up @@ -68,7 +69,7 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {

Context "Backup restore" {
$quickbackup = Get-DbaDatabase -SqlInstance $script:instance2 -ExcludeSystem | Backup-DbaDatabase -BackupDirectory C:\temp
$results = Start-DbaMigration -Force -Source $script:instance2 -Destination $script:instance3 -UseLastBackup -Exclude Logins, SpConfigure, SysDbUserObjects, AgentServer, CentralManagementServer, ExtendedEvents, PolicyManagement, ResourceGovernor, Endpoints, ServerAuditSpecifications, Audits, LinkedServers, SystemTriggers, DataCollector, DatabaseMail, BackupDevices, Credentials
$results = Start-DbaMigration -Force -Source $script:instance2 -Destination $script:instance3 -UseLastBackup -Exclude Logins, SpConfigure, SysDbUserObjects, AgentServer, CentralManagementServer, ExtendedEvents, PolicyManagement, ResourceGovernor, Endpoints, ServerAuditSpecifications, Audits, LinkedServers, SystemTriggers, DataCollector, DatabaseMail, BackupDevices, Credentials, StartupProcedures

It "returns at least one result" {
$results | Should -Not -Be $null
Expand Down

0 comments on commit 591b1cf

Please sign in to comment.