Last active
July 22, 2016 10:10
-
-
Save Alex-Yates/7eb00a6e47751a80c7343bf6443741b4 to your computer and use it in GitHub Desktop.
DLM Automation PS scripts: Create database release from package
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]$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=$true)] | |
[string]$DatabaseDeploymentResourcesPath = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseTransactionIsolationLevel = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseFilterPath = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseCompareOptions = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseIncludeIdenticalsInReport = $null, | |
[Parameter(Mandatory=$false)] | |
[string]$SQLReleaseIgnoreStaticData = $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" | |
} | |
# Match the package file name. Only one file should match | |
$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 | |
$PackageFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($PackageFilePath) | |
# Combine deployment path with NuGet package name for unique directory | |
$DatabaseDeploymentResourcesPath = Join-Path $DatabaseDeploymentResourcesPath $PackageFileNameWithoutExtension | |
# Makes sure the directory we're about to create doesn't already exist. | |
If (Test-Path $DatabaseDeploymentResourcesPath) { | |
rmdir $DatabaseDeploymentResourcesPath -Recurse -Force | |
} | |
# Determine whether or not to include identical objects in the report. | |
$includeIdenticalsInReport = $SQLReleaseIncludeIdenticalsInReport -eq "True" | |
# Determine whether or not static data should be included in the deployment | |
$ignoreStaticData = $SQLReleaseIgnoreStaticData -eq "True" | |
# Sets up connection string. | |
$target = 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 | |
} | |
# Creates the database release object | |
$databaseUpdate = New-DlmDatabaseRelease -Target $target ` | |
-Source $PackageFilePath ` | |
-TransactionIsolationLevel $SQLReleaseTransactionIsolationLevel ` | |
-IgnoreStaticData:$ignoreStaticData ` | |
-FilterPath $SQLReleaseFilterPath ` | |
-SQLCompareOptions $SQLReleaseCompareOptions ` | |
-IncludeIdenticalsInReport:$includeIdenticalsInReport ` | |
-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 | |
} | |
# Export the release object to disk | |
Export-DlmDatabaseRelease $databaseUpdate -Path $DatabaseDeploymentResourcesPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment