Skip to content

Instantly share code, notes, and snippets.

@Alex-Yates
Created July 22, 2016 10:08
Show Gist options
  • Save Alex-Yates/2360d1ed97f1c1f45792250fb046132b to your computer and use it in GitHub Desktop.
Save Alex-Yates/2360d1ed97f1c1f45792250fb046132b to your computer and use it in GitHub Desktop.
DLM Automation PS scripts: Deploy from database package
param
(
[Parameter(Mandatory=$true)]
[string]$PackageFilePathPattern = $null,
[Parameter(Mandatory=$true)]
[string]$DatabaseServer = $null,
[Parameter(Mandatory=$true)]
[string]$DatabaseName = $null,
[Parameter(Mandatory=$false)]
[string]$DatabaseUserName = $null,
[Parameter(Mandatory=$false)]
[string]$DatabasePassword = $null,
[Parameter(Mandatory=$false)]
[string]$SQLReleaseTransactionIsolationLevel = $null,
[Parameter(Mandatory=$false)]
[string]$SQLReleaseFilterPath = $null,
[Parameter(Mandatory=$false)]
[string]$SQLReleaseCompareOptions = $null,
[Parameter(Mandatory=$false)]
[bool]$SQLReleaseIgnoreStaticData = $False
)
# 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"
}
$possiblePackageFiles = (Get-ChildItem $PackageFilePathPattern)
if ($possiblePackageFiles.Count -ne 1)
{
[string]$message = "Error: " + $possiblePackageFiles.Count.ToString() + " files match. Precisely 1 file should match pattern."
throw [System.Exception] $message
}
$PackageFilePath = $possiblePackageFiles.FullName
# Determine whether or not static data should be included in the deployment
$ignoreStaticData = $SQLReleaseIgnoreStaticData -eq "True"
$targetDb = New-DlmDatabaseConnection -ServerInstance $DatabaseServer -Database $DatabaseName -Username $DatabaseUserName -Password $DatabasePassword | 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
}
$update = New-DlmDatabaseRelease -Target $targetDb `
-Source $PackageFilePath `
-TransactionIsolationLevel $SQLReleaseTransactionIsolationLevel `
-IgnoreStaticData:$ignoreStaticData `
-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
}
Use-DlmDatabaseRelease $update -DeployTo $targetDb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment