Created
July 22, 2016 10:08
-
-
Save Alex-Yates/97b5851ba5bb1df46d92e5765a12747d to your computer and use it in GitHub Desktop.
DLM Automation PS scripts: Deploy from database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$TargetDatabaseServer = $null, | |
[Parameter(Mandatory=$true)] | |
[string]$TargetDatabaseName = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$TargetDatabaseUserName = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$TargetDatabasePassword = $null, | |
[Parameter(Mandatory=$true)] | |
[string]$SourceDatabaseServer = $null, | |
[Parameter(Mandatory=$true)] | |
[string]$SourceDatabaseName = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SourceDatabaseUserName = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SourceDatabasePassword = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseTransactionIsolationLevel = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseFilterPath = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseCompareOptions = $null | |
) | |
# Check optional parameters | |
if ([string]::IsNullOrWhiteSpace($SQLReleaseTransactionIsolationLevel)) { $SQLReleaseTransactionIsolationLevel = "Serializable" } | |
if ([string]::IsNullOrWhiteSpace($SQLReleaseFilterPath)) { $SQLReleaseFilterPath = $null } | |
if ([string]::IsNullOrWhiteSpace($SQLReleaseCompareOptions)) { $SQLReleaseCompareOptions = $null } | |
$ErrorActionPreference = "Stop" | |
# Check if SQL Release is installed. | |
$sqlReleaseModule = Get-Module -ListAvailable -Name SQLRelease | |
if ($sqlReleaseModule -eq $null) { | |
throw "Cannot find SQL Release on your Octopus Tentacle. If SQL Release is installed, try restarting the Tentacle service for it to be detected." | |
} | |
$currentVersion = $sqlReleaseModule.Version | |
$minimumRequiredVersion = [version] '1.2.1.3109' | |
if ($currentVersion -lt $minimumRequiredVersion) { | |
throw "This step requires SQL Release version $minimumRequiredVersion or later. The current version is $currentVersion. The latest version can be found at http://www.red-gate.com/dlmas/download" | |
} | |
# Sets up connection strings for the target and source databases. | |
$targetDatabase = New-DlmDatabaseConnection -ServerInstance $TargetDatabaseServer -Database $TargetDatabaseName -Username $TargetDatabaseUserName -Password $TargetDatabasePassword | Test-DlmDatabaseConnection | |
$sourceDatabase = New-DlmDatabaseConnection -ServerInstance $SourceDatabaseServer -Database $SourceDatabaseName -Username $SourceDatabaseUserName -Password $SourceDatabasePassword | Test-DlmDatabaseConnection | |
# Stop on any warnings | |
$highWarnings = $databaseUpdate.Warnings | Where { $_.Severity -eq "High" } | |
if($highWarnings.Count -gt 0) | |
{ | |
[string]$highWarningsDetails = "" | |
$highWarnings | ForEach-Object { $highWarningsDetails = $highWarningsDetails + $_.Details + "`n" } | |
throw [System.Exception] $highWarningsDetails | |
} | |
# Create release object | |
$databaseUpdate = New-DlmDatabaseRelease -Target $targetDatabase -Source $sourceDatabase ` | |
-TransactionIsolationLevel $SQLReleaseTransactionIsolationLevel ` | |
-FilterPath $SQLReleaseFilterPath ` | |
-SQLCompareOptions $SQLReleaseCompareOptions ` | |
-Verbose | |
# Stop on any warnings | |
$highWarnings = $databaseUpdate.Warnings | Where { $_.Severity -eq "High" } | |
if($highWarnings.Count -gt 0) | |
{ | |
[string]$highWarningsDetails = "" | |
$highWarnings | ForEach-Object { $highWarningsDetails = $highWarningsDetails + $_.Details + "`n" } | |
throw [System.Exception] $highWarningsDetails | |
} | |
# Update target DB to be like staging DB | |
Use-DlmDatabaseRelease $databaseUpdate -DeployTo $targetDatabase -SkipPreUpdateSchemaCheck -Verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment