bin/deploy.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "")]
[CmdletBinding(SupportsShouldProcess)]
Param (
    [Alias('Server', 'SqlServer', 'DBServer', 'Instance')]
    [string]$SqlInstance,
    [string]$Database,
    [ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction')]
    [string]$DeploymentMethod = 'NoTransaction',
    [int]$ConnectionTimeout,
    [int]$ExecutionTimeout,
    [switch]$Encrypt,
    [pscredential]$Credential,
    [string]$UserName,
    [securestring]$Password,
    [AllowNull()]
    [string]$SchemaVersionTable,
    [switch]$Silent,
    [Alias('ArgumentList')]
    [hashtable]$Variables,
    [string]$OutputFile,
    [switch]$Append,
    [Alias('Config')]
    [object]$Configuration,
    [string[]]$Build,
    [string]$Schema,
    [switch]$CreateDatabase,
    [AllowNull()]
    [string]$ConnectionString,
    [ValidateSet('SqlServer', 'Oracle', 'MySQL', 'PostgreSQL')]
    [Alias('ConnectionType', 'ServerType')]
    [string]$Type = 'SQLServer'
)

#Import module
If (-not (Get-Module dbops)) {
    Import-Module "$PSScriptRoot\Modules\dbops\dbops.psd1"
}
. "$PSScriptRoot\Modules\dbops\internal\classes\DBOps.enums.ps1"

$config = Get-DBOConfig -Path "$PSScriptRoot\dbops.config.json" -Configuration $Configuration

#Merge custom parameters into a configuration
$newConfig = @{}
foreach ($key in ($PSBoundParameters.Keys)) {
    if ($key -in [DBOpsConfig]::EnumProperties()) {
        $newConfig.$key = $PSBoundParameters[$key]
    }
}
$config.Merge($newConfig)

#Prepare deployment function call parameters
$params = @{
    PackageFile   = "$PSScriptRoot\dbops.package.json"
    Configuration = $config
}
foreach ($key in ($PSBoundParameters.Keys)) {
    #If any custom properties were specified
    if ($key -in @('OutputFile', 'Append', 'Type', 'Build')) {
        $params += @{ $key = $PSBoundParameters[$key] }
    }
}

if ($PSCmdlet.ShouldProcess($params.PackageFile, "Initiating the deployment of the package")) {
    Invoke-DBODeployment @params
}
else {
    Invoke-DBODeployment @params -WhatIf
}