Skip to content

Commit

Permalink
Add replication support to dbatools! (#8958)
Browse files Browse the repository at this point in the history
Co-authored-by: ClaudioESSilva <claudiosil100@gmail.com>
Co-authored-by: Shawn Melton <11204251+wsmelton@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 27, 2023
1 parent 0a7ef74 commit 85c8f21
Show file tree
Hide file tree
Showing 52 changed files with 4,169 additions and 197 deletions.
152 changes: 152 additions & 0 deletions .github/workflows/integration-tests-repl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: Run Replication Tests
on: [push]
defaults:
run:
shell: pwsh
jobs:
repl-tests-part1:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

steps:
- uses: actions/checkout@v3

- name: Install and cache PowerShell modules
uses: potatoqualitee/psmodulecache@v5.2
with:
modules-to-cache: dbatools.library:2023.5.5

- name: Set encryption values
run: |
Import-Module ./dbatools.psd1 -Force
Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true -Register
Set-DbatoolsConfig -FullName sql.connection.encrypt -Value Optional -Register
Get-DbatoolsConfigValue -FullName sql.connection.encrypt | Write-Warning
- name: Setup docker images
run: |
# create a shared network
docker network create localnet
# Expose engine and endpoint then setup a shared path for migrations
docker run -p 1433:1433 --volume shared:/shared:z --name mssql1 --hostname mssql1 --network localnet -d dbatools/sqlinstance
# Expose second engine and endpoint on different port
docker run -p 14333:1433 --volume shared:/shared:z --name mssql2 --hostname mssql2 --network localnet -d dbatools/sqlinstance2
- name: Add hostname to hosts file
run: |
echo "127.0.0.1 mssql1 mssql2" | sudo tee -a /etc/hosts
- name: 👥 Clone appveyor repo
working-directory: /tmp
run: |
gh repo clone dataplat/appveyor-lab
- name: Setup Replication
run: |
Import-Module ./dbatools.psd1 -Force
# need some folders for our repl stuff
docker exec mssql1 mkdir /shared/data /shared/repldata /var/opt/mssql/ReplData
- name: Run replication tests part 1
run: |
Import-Module ./dbatools.psd1 -Force
$null = Invoke-Pester ./tests/gh-actions-repl-1.ps1 -Output Detailed -PassThru -Verbose
repl-tests-part2:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

steps:
- uses: actions/checkout@v3

- name: Install and cache PowerShell modules
uses: potatoqualitee/psmodulecache@v5.2
with:
modules-to-cache: dbatools.library:2023.5.5

- name: Set encryption values
run: |
Import-Module ./dbatools.psd1 -Force
Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true -Register
Set-DbatoolsConfig -FullName sql.connection.encrypt -Value Optional -Register
Get-DbatoolsConfigValue -FullName sql.connection.encrypt | Write-Warning
- name: Setup docker images
run: |
# create a shared network
docker network create localnet
# Expose engine and endpoint then setup a shared path for migrations
docker run -p 1433:1433 --volume shared:/shared:z --name mssql1 --hostname mssql1 --network localnet -d dbatools/sqlinstance
# Expose second engine and endpoint on different port
docker run -p 14333:1433 --volume shared:/shared:z --name mssql2 --hostname mssql2 --network localnet -d dbatools/sqlinstance2
- name: Add hostname to hosts file
run: |
echo "127.0.0.1 mssql1 mssql2" | sudo tee -a /etc/hosts
- name: 👥 Clone appveyor repo
working-directory: /tmp
run: |
gh repo clone dataplat/appveyor-lab
- name: Setup Replication
run: |
Import-Module ./dbatools.psd1 -Force
# need some folders for our repl stuff
docker exec mssql1 mkdir /shared/data /shared/repldata /var/opt/mssql/ReplData
- name: Run replication tests part 2
run: |
Import-Module ./dbatools.psd1 -Force
$null = Invoke-Pester ./tests/gh-actions-repl-2.ps1 -Output Detailed -PassThru -Verbose
repl-tests-part3:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

steps:
- uses: actions/checkout@v3

- name: Install and cache PowerShell modules
uses: potatoqualitee/psmodulecache@v5.2
with:
modules-to-cache: dbatools.library:2023.5.5

- name: Set encryption values
run: |
Import-Module ./dbatools.psd1 -Force
Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true -Register
Set-DbatoolsConfig -FullName sql.connection.encrypt -Value Optional -Register
Get-DbatoolsConfigValue -FullName sql.connection.encrypt | Write-Warning
- name: Setup docker images
run: |
# create a shared network
docker network create localnet
# Expose engine and endpoint then setup a shared path for migrations
docker run -p 1433:1433 --volume shared:/shared:z --name mssql1 --hostname mssql1 --network localnet -d dbatools/sqlinstance
# Expose second engine and endpoint on different port
docker run -p 14333:1433 --volume shared:/shared:z --name mssql2 --hostname mssql2 --network localnet -d dbatools/sqlinstance2
- name: Add hostname to hosts file
run: |
echo "127.0.0.1 mssql1 mssql2" | sudo tee -a /etc/hosts
- name: 👥 Clone appveyor repo
working-directory: /tmp
run: |
gh repo clone dataplat/appveyor-lab
- name: Setup Replication
run: |
Import-Module ./dbatools.psd1 -Force
# need some folders for our repl stuff
docker exec mssql1 mkdir /shared/data /shared/repldata /var/opt/mssql/ReplData
- name: Run replication tests part 3
run: |
Import-Module ./dbatools.psd1 -Force
Invoke-Pester ./tests/gh-actions-repl-3.ps1 -Output Detailed -PassThru -Verbose
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"Kokkinos",
"Kravtsov",
"Mikey",
"mssql",
"nchar",
"niphlod",
"ntext",
Expand Down
99 changes: 99 additions & 0 deletions ReplicationDemo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# dbatools 💜 dbatools

##############################
# create docker environment
##############################
# create a shared network
docker network create localnet

# Expose engines and setup shared path for migrations
docker run -p 2500:1433 --volume shared:/shared:z --name mssql1 --hostname mssql1 --network localnet -d dbatools/sqlinstance
docker run -p 2600:1433 --volume shared:/shared:z --name mssql2 --hostname mssql2 --network localnet -d dbatools/sqlinstance2

# create the repl folder
docker exec mssql1 mkdir /var/opt/mssql/ReplData

# also need these folders for setting up replication
docker exec mssql1 mkdir /shared/data /shared/repldata

##############################

# import out version of the module
cd C:\GitHub\DMM-GitHub\dbatools
Import-Module .\dbatools.psd1

# lets save the password for connecting to containers because I'm lazy
$securePassword = ('dbatools.IO' | ConvertTo-SecureString -AsPlainText -Force)
$credential = New-Object System.Management.Automation.PSCredential('sqladmin', $securePassword)

$PSDefaultParameterValues = @{
"*:SqlCredential" = $credential
"*:DestinationCredential" = $credential
"*:DestinationSqlCredential" = $credential
"*:SourceSqlCredential" = $credential
"*:PublisherSqlCredential" = $credential
}

# what do we have so far
Get-DbaReplServer -SqlInstance mssql1
Get-DbaReplDistributor -SqlInstance mssql1
Get-DbaReplPublisher -SqlInstance mssql1

# enable distribution
Enable-DbaReplDistributor -SqlInstance mssql1

# enable publishing
Enable-DbaReplPublishing -SqlInstance mssql1

# create a transactional publication using splat format
$pub = @{
SqlInstance = 'mssql1'
Database = 'pubs'
PublicationName = 'testPub'
Type = 'Transactional'
}
New-DbaReplPublication @pub

# add an article to the publication
$article = @{
SqlInstance = 'mssql1'
Database = 'pubs'
PublicationName = 'testpub'
Name = 'authors'
}
Add-DbaReplArticle @article

# create a pubs database on mssql2 to replicate to
New-DbaDatabase -SqlInstance mssql2 -Name pubs

# if you don't the New-DbaReplSubscription command will create the database for you

# add a subscription to the publication
$sub = @{
SqlInstance = 'mssql2'
Database = 'pubs'
PublicationDatabase = 'pubs'
PublisherSqlInstance = 'mssql1'
PublicationName = 'testpub'
Type = 'Push'
SubscriptionSqlCredential = $credential

}
New-DbaReplSubscription @sub

# creates the snapshot job with a daily schedule at 8am - is that expected? good default?
# should adding a subscription kick off snapshot? should that be an param -StartSnapshotNow -- yes
# create that without a schedule by default maybe a param for a schedule
#

# stats on the subscription - in the distribution database
# could we make a command to get stats





## when adding an article - we need the options
# - action if name is in use 'drop existing object and create new'
# copy nonclusterd indexes
# nuno
42 changes: 35 additions & 7 deletions dbatools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
'Export-DbaLogin',
'Export-DbaPfDataCollectorSetTemplate',
'Export-DbaRegServer',
'Export-DbaRepServerSetting',
'Export-DbaReplServerSetting',
'Export-DbaScript',
'Export-DbaServerRole',
'Export-DbaSpConfigure',
Expand Down Expand Up @@ -350,9 +350,10 @@
'Get-DbaRegServer',
'Get-DbaRegServerGroup',
'Get-DbaRegServerStore',
'Get-DbaRepDistributor',
'Get-DbaRepPublication',
'Get-DbaRepServer',
'Get-DbaReplDistributor',
'Get-DbaReplPublication',
'Get-DbaReplPublisher',
'Get-DbaReplServer',
'Get-DbaResourceGovernor',
'Get-DbaRgClassifierFunction',
'Get-DbaRgResourcePool',
Expand Down Expand Up @@ -693,7 +694,7 @@
'Test-DbaOptimizeForAdHoc',
'Test-DbaPath',
'Test-DbaPowerPlan',
'Test-DbaRepLatency',
'Test-DbaReplLatency',
'Test-DbaSpn',
'Test-DbaTempDbConfig',
'Test-DbaWindowsLogin',
Expand Down Expand Up @@ -728,7 +729,23 @@
'New-DbaLinkedServerLogin',
'Remove-DbaLinkedServerLogin',
'Remove-DbaCredential',
'Remove-DbaAgentProxy'
'Remove-DbaAgentProxy',

# NEW REPLICATION STUFF
'Disable-DbaReplDistributor',
'Enable-DbaReplDistributor',
'Disable-DbaReplPublishing',
'Enable-DbaReplPublishing',
'New-DbaReplPublication',
'Get-DbaReplArticle',
'Get-DbaReplArticleColumn',
'Add-DbaReplArticle',
'Remove-DbaReplArticle',
'Remove-DbaReplPublication',
'New-DbaReplSubscription',
'Remove-DbaReplSubscription',
'New-DbaReplCreationScriptOptions',
'Get-DbaReplSubscription'
)

# Cmdlets to export from this module
Expand All @@ -751,9 +768,20 @@
'Write-DbaDataTable',
'Get-DbaDbModule',
'Get-DbaBuildReference',
'Copy-DbaSysDbUserObject'
'Copy-DbaSysDbUserObject',

# replication aliases - these existed before the repl overhaul in 2.0+
'Get-DbaRepServer',
'Export-DbaRepServerSetting',
'Get-DbaRepDistributor',
'Test-DbaRepLatency',
'Get-DbaRepDistributor',
'Get-DbaRepPublication',
'Get-DbaRepServer'

)


# List of all modules packaged with this module
ModuleList = @()

Expand Down
22 changes: 17 additions & 5 deletions dbatools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ $forever = @{
foreach ($_ in $forever.GetEnumerator()) {
Set-Alias -Name $_.Key -Value $_.Value
}

# Replication Aliases
$replAliases = @{
'Get-DbaRepServer' = 'Get-DbaReplServer'
'Export-DbaRepServerSetting' = 'Export-DbaReplServerSetting'
'Get-DbaRepDistributor' = 'Get-DbaReplDistributor'
'Test-DbaRepLatency' = 'Test-DbaReplLatency'
'Get-DbaRepPublication' = 'Get-DbaReplPublication'
}
foreach ($_ in $replAliases.GetEnumerator()) {
Set-Alias -Name $_.Key -Value $_.Value
}
#endregion Aliases

# apparently this is no longer required? :O
Expand Down Expand Up @@ -846,7 +858,7 @@ if ($PSVersionTable.PSVersion.Major -lt 5) {
)
$script:noncoresmo = @(
# SMO issues
'Get-DbaRepDistributor',
'Get-DbaReplDistributor',
'Copy-DbaPolicyManagement',
'Copy-DbaDataCollector',
'Get-DbaPbmCategory',
Expand All @@ -855,10 +867,10 @@ if ($PSVersionTable.PSVersion.Major -lt 5) {
'Get-DbaPbmObjectSet',
'Get-DbaPbmPolicy',
'Get-DbaPbmStore',
'Get-DbaRepPublication',
'Test-DbaRepLatency',
'Export-DbaRepServerSetting',
'Get-DbaRepServer'
'Get-DbaReplPublication',
'Test-DbaReplLatency',
'Export-DbaReplServerSetting',
'Get-DbaReplServer'
)
$script:windowsonly = @(
# filesystem (\\ related),
Expand Down
Loading

0 comments on commit 85c8f21

Please sign in to comment.