Skip to content

Commit

Permalink
Merge pull request dataplat#4776 from sqlcollaborative/replicafix
Browse files Browse the repository at this point in the history
Fixed Add-DbaAgReplica
  • Loading branch information
potatoqualitee authored Dec 5, 2018
2 parents fbf00eb + f880d5c commit def28fe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
20 changes: 5 additions & 15 deletions functions/Add-DbaAgReplica.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ function Add-DbaAgReplica {
.PARAMETER Name
The name of the replica. Defaults to the SQL Server instance name.
.PARAMETER AvailabilityGroup
The Availability Group to which a replica will be bestowed upon.
.PARAMETER AvailabilityMode
Sets the availability mode of the availability group replica. Options are: AsynchronousCommit and SynchronousCommit. SynchronousCommit is default.
Expand Down Expand Up @@ -97,7 +94,6 @@ function Add-DbaAgReplica {
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string]$AvailabilityGroup,
[string]$Name,
[ValidateSet('AsynchronousCommit', 'SynchronousCommit')]
[string]$AvailabilityMode = "SynchronousCommit",
Expand All @@ -114,16 +110,11 @@ function Add-DbaAgReplica {
[switch]$Passthru,
[string]$ReadonlyRoutingConnectionUrl,
[string]$Certificate,
[parameter(ValueFromPipeline)]
[parameter(ValueFromPipeline, Mandatory)]
[Microsoft.SqlServer.Management.Smo.AvailabilityGroup]$InputObject,
[switch]$EnableException
)
process {
if (-not $AvailabilityGroup -and -not $InputObject) {
Stop-Function -Message "You must specify either AvailabilityGroup or pipe in an availabilty group to continue."
return
}

foreach ($instance in $SqlInstance) {
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 11
Expand All @@ -138,10 +129,6 @@ function Add-DbaAgReplica {
}
}

if ($AvailabilityGroup) {
$InputObject = Get-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $AvailabilityGroup
}

$ep = Get-DbaEndpoint -SqlInstance $server -Type DatabaseMirroring

if (-not $ep) {
Expand Down Expand Up @@ -183,9 +170,12 @@ function Add-DbaAgReplica {
}

$defaults = 'ComputerName', 'InstanceName', 'SqlInstance', 'AvailabilityGroup', 'Name', 'Role', 'RollupSynchronizationState', 'AvailabilityMode', 'BackupPriority', 'EndpointUrl', 'SessionTimeout', 'FailoverMode', 'ReadonlyRoutingList'

$InputObject.AvailabilityReplicas.Add($replica)
$agreplica = $InputObject.AvailabilityReplicas[$Name]
if ($InputObject.State -eq 'Existing') {
Invoke-Create -Object $replica
$null = Join-DbaAvailabilityGroup -SqlInstance $instance -SqlCredential $SqlCredential -AvailabilityGroup $InputObject.Name
}
Add-Member -Force -InputObject $agreplica -MemberType NoteProperty -Name ComputerName -value $agreplica.Parent.ComputerName
Add-Member -Force -InputObject $agreplica -MemberType NoteProperty -Name InstanceName -value $agreplica.Parent.InstanceName
Add-Member -Force -InputObject $agreplica -MemberType NoteProperty -Name SqlInstance -value $agreplica.Parent.SqlInstance
Expand Down
10 changes: 3 additions & 7 deletions tests/Add-DbaAgReplica.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ Describe "$commandname Unit Tests" -Tag 'UnitTests' {
<#
Get commands, Default count = 11
Commands with SupportShouldProcess = 13
#>
#>
$defaultParamCount = 13
[object[]]$params = (Get-ChildItem function:\Add-DbaAgReplica).Parameters.Keys
$knownParameters = 'SqlInstance', 'SqlCredential', 'AvailabilityGroup', 'Name', 'AvailabilityMode', 'FailoverMode', 'BackupPriority', 'ConnectionModeInPrimaryRole', 'ConnectionModeInSecondaryRole', 'SeedingMode', 'Endpoint', 'Passthru', 'ReadonlyRoutingConnectionUrl', 'Certificate', 'InputObject', 'EnableException'
$paramCount = $knownParameters.Count
$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'AvailabilityMode', 'FailoverMode', 'BackupPriority', 'ConnectionModeInPrimaryRole', 'ConnectionModeInSecondaryRole', 'SeedingMode', 'Endpoint', 'Passthru', 'ReadonlyRoutingConnectionUrl', 'Certificate', 'InputObject', 'EnableException'
It "Should contain our specific parameters" {
((Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params -IncludeEqual | Where-Object SideIndicator -eq "==").Count) | Should Be $paramCount
}
It "Should only contain $paramCount parameters" {
$params.Count - $defaultParamCount | Should Be $paramCount
((Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params -IncludeEqual | Where-Object SideIndicator -eq "==").Count) | Should Be $knownParameters.Count
}
}
}
Expand Down

0 comments on commit def28fe

Please sign in to comment.