functions/Publish-DbaDacpac.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
function Publish-DbaDacpac {
    <#
        .SYNOPSIS
            The Publish-Database command takes a dacpac which is the output from an SSDT project and publishes it to a database. Changing the schema to match the dacpac and also to run any scripts in the dacpac (pre/post deploy scripts).
 
        .DESCRIPTION
            Deploying a dacpac uses the DacFx which historically needed to be installed on a machine prior to use. In 2016 the DacFx was supplied by Microsoft as a nuget package and this uses that nuget package.
 
        .PARAMETER SqlInstance
            SQL Server name or SMO object representing the SQL Server to connect to.
 
        .PARAMETER SqlCredential
            Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use:
 
            $scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
 
            Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
 
            To connect as a different Windows user, run PowerShell as that user.
 
        .PARAMETER Path
            Specifies the filesystem path to the DACPAC
 
        .PARAMETER PublishXml
            Specifies the publish profile which will include options and sqlCmdVariables.
 
        .PARAMETER Database
            Specifies the name of the database being published.
 
        .PARAMETER ConnectionString
            Specifies the connection string to the database you are upgrading. This is not required if SqlInstance is specified.
 
        .PARAMETER GenerateDeploymentScript
            If this switch is enabled, the publish script will be generated.
 
        .PARAMETER GenerateDeploymentReport
            If this switch is enabled, the publish XML report will be generated.
 
        .PARAMETER OutputPath
            Specifies the filesystem path (directory) where output files will be generated.
 
        .PARAMETER ScriptOnly
            If this switch is enabled, only the change scripts will be generated.
 
        .PARAMETER IncludeSqlCmdVars
            If this switch is enabled, SqlCmdVars in publish.xml will have their values overwritten.
 
        .PARAMETER EnableException
            By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
            This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
        .NOTES
            Tags: Migration, Database, Dacpac
            Author: Richie lee (@bzzzt_io)
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
        .LINK
            https://dbatools.io/Publish-DbaDacpac
 
        .EXAMPLE
            Publish-DbaDacpac -SqlInstance sql2017 -Database WideWorldImporters -Path C:\temp\sql2016-WideWorldImporters.dacpac -PublishXml C:\temp\sql2016-WideWorldImporters-publish.xml
 
            Updates WideWorldImporters on sql2017 from the sql2016-WideWorldImporters.dacpac using the sql2016-WideWorldImporters-publish.xml publish profile
 
        .EXAMPLE
            New-DbaPublishProfile -SqlInstance sql2016 -Database db2 -Path C:\temp
            Export-DbaDacpac -SqlInstance sql2016 -Database db2 | Publish-DbaDacpac -PublishXml C:\temp\sql2016-db2-publish.xml -Database db1, db2 -SqlInstance sql2017
 
            Creates a publish profile at C:\temp\sql2016-db2-publish.xml, exports the .dacpac to $home\Documents\sql2016-db2.dacpac
            then publishes it to the sql2017 server database db2
 
  #>

    [CmdletBinding()]
    param (
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstance[]]$SqlInstance,
        [Alias("Credential")]
        [PSCredential]$SqlCredential,
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]$Path,
        [Parameter(Mandatory)]
        [string]$PublishXml,
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string[]]$Database,
        [string[]]$ConnectionString,
        [switch]$GenerateDeploymentScript,
        [switch]$GenerateDeploymentReport,
        [Switch]$ScriptOnly,
        [string]$OutputPath = "$home\Documents",
        [switch]$IncludeSqlCmdVars,
        [switch]$EnableException
    )

    begin {
        if ((Test-Bound -Not -ParameterName SqlInstance) -and (Test-Bound -Not -ParameterName ConnectionString)) {
            Stop-Function -Message "You must specify either SqlInstance or ConnectionString."
        }
        if ((Test-Bound -ParameterName GenerateDeploymentScript) -or (Test-Bound -ParameterName GenerateDeploymentReport)) {
            $defaultcolumns = 'ComputerName', 'InstanceName', 'SqlInstance', 'Database', 'Dacpac', 'PublishXml', 'Result', 'DatabaseScriptPath', 'MasterDbScriptPath', 'DeploymentReport', 'DeployOptions', 'SqlCmdVariableValues'
        }
        else {
            $defaultcolumns = 'ComputerName', 'InstanceName', 'SqlInstance', 'Database', 'Dacpac', 'PublishXml', 'Result', 'DeployOptions', 'SqlCmdVariableValues'
        }
        if ((Test-Bound -ParameterName ScriptOnly) -and (Test-Bound -Not -ParameterName GenerateDeploymentScript) -and (Test-Bound -Not -ParameterName GenerateDeploymentScript)) {
            Stop-Function -Message "You must at least one of GenerateDeploymentScript or GenerateDeploymentReport when using ScriptOnly"
        }

        function Get-ServerName ($connstring) {
            $builder = New-Object System.Data.Common.DbConnectionStringBuilder
            $builder.set_ConnectionString($connstring)
            $instance = $builder['data source']

            if (-not $instance) {
                $instance = $builder['server']
            }

            return $instance.ToString().Replace('\', '-')
        }

        $dacfxPath = "$script:PSModuleRoot\bin\smo\Microsoft.SqlServer.Dac.dll"
        if ((Test-Path $dacfxPath) -eq $false) {
            Stop-Function -Message 'No usable version of Dac Fx found.' -EnableException $EnableException
        }
        else {
            try {
                Add-Type -Path $dacfxPath
                Write-Message -Level Verbose -Message "Dac Fx loaded."
            }
            catch {
                Stop-Function -Message 'No usable version of Dac Fx found.' -EnableException $EnableException -ErrorRecord $_
            }
        }
    }

    process {
        if (Test-FunctionInterrupt) { return }

        if (-not (Test-Path -Path $Path)) {
            Stop-Function -Message "$Path not found!"
        }

        if (-not (Test-Path -Path $PublishXml)) {
            Stop-Function -Message "$PublishXml not found!"
        }

        foreach ($instance in $sqlinstance) {
            try {
                Write-Message -Level Verbose -Message "Connecting to $instance."
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
            }
            catch {
                Stop-Function -Message "Failure." -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $ConnectionString += $server.ConnectionContext.ConnectionString.Replace('"', "'")
        }

        try {
            $dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($Path)
        }
        catch {
            Stop-Function -Message "Could not load package." -ErrorRecord $_
        }

        try {
            $dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($PublishXml)
        }
        catch {
            Stop-Function -Message "Could not load profile." -ErrorRecord $_
        }

        if ($IncludeSqlCmdVars) {
            Get-SqlCmdVars -SqlCommandVariableValues $dacProfile.DeployOptions.SqlCommandVariableValues
        }

        foreach ($connstring in $ConnectionString) {
            $cleaninstance = Get-ServerName $connstring
            $instance = $cleaninstance.ToString().Replace('--', '\')

            foreach ($dbname in $database) {
                if ($GenerateDeploymentScript -or $GenerateDeploymentReport) {
                    $timeStamp = (Get-Date).ToString("yyMMdd_HHmmss_f")
                    $DatabaseScriptPath = Join-Path $OutputPath "$cleaninstance-$dbname`_DeployScript_$timeStamp.sql"
                    $MasterDbScriptPath = Join-Path $OutputPath "$cleaninstance-$dbname`_Master.DeployScript_$timeStamp.sql"
                    $DeploymentReport = Join-Path $OutputPath "$cleaninstance-$dbname`_Result.DeploymentReport_$timeStamp.xml"
                }

                if ($connstring -notmatch 'Database=') {
                    $connstring = "$connstring;Database=$dbname"
                }

                try {
                    $dacServices = New-Object Microsoft.SqlServer.Dac.DacServices $connstring
                }
                catch {
                    Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $server -Continue
                }

                $options = @{
                    GenerateDeploymentScript = $GenerateDeploymentScript
                    GenerateDeploymentReport = $GenerateDeploymentReport
                    DatabaseScriptPath       = $DatabaseScriptPath
                    MasterDbScriptPath       = $MasterDbScriptPath
                    DeployOptions            = $dacProfile.DeployOptions
                }

                try {
                    $global:output = @()
                    Register-ObjectEvent -InputObject $dacServices -EventName "Message" -SourceIdentifier "msg" -Action { $global:output += $EventArgs.Message.Message } | Out-Null
                    if ($ScriptOnly) {
                        Write-Message -Level Verbose -Message "Generating script."
                        $result = $dacServices.Script($dacPackage, $dbname, $options)
                    }
                    else {
                        Write-Message -Level Verbose -Message "Executing Deployment."
                        $result = $dacServices.Publish($dacPackage, $dbname, $options)
                    }
                }
                catch [Microsoft.SqlServer.Dac.DacServicesException] {
                    $message = ("Deployment failed: {0} `n Reason: {1}" -f $_.Exception.Message, $_.Exception.InnerException.Message)
                }
                finally {
                    Unregister-Event -SourceIdentifier "msg"
                    if ($message) {
                        Stop-Function -Message $message
                    }
                    if ($GenerateDeploymentReport) {
                        $result.DeploymentReport | Out-File $DeploymentReport
                        Write-Message -Level Verbose -Message "Deployment Report - $DeploymentReport."
                    }
                    if ($GenerateDeploymentScript) {
                        Write-Message -Level Verbose -Message "Database change script - $DatabaseScriptPath."
                        if ((Test-Path $MasterDbScriptPath)) {
                            Write-Message -Level Verbose -Message "Master database change script - $($result.MasterDbScript)."
                        }
                    }
                    $resultoutput = ($global:output -join "`r`n" | Out-String).Trim()
                    if ($resultoutput -match "Failed" -and ($GenerateDeploymentReport -or $GenerateDeploymentScript)) {
                        Write-Message -Level Warning -Message "Seems like the attempt to publish/script may have failed. If scripts have not generated load dacpac into Visual Studio to check SQL is valid."
                    }
                    $server = [dbainstance]$instance
                    $deployOptions = $dacProfile.DeployOptions | Select-Object -Property * -ExcludeProperty "SqlCommandVariableValues"
                    [pscustomobject]@{
                        ComputerName         = $server.ComputerName
                        InstanceName         = $server.InstanceName
                        SqlInstance          = $server.FullName
                        Database             = $dbname
                        Result               = $resultoutput
                        Dacpac               = $Path
                        PublishXml           = $PublishXml
                        ConnectionString     = $connstring
                        DatabaseScriptPath   = $DatabaseScriptPath
                        MasterDbScriptPath   = $MasterDbScriptPath
                        DeploymentReport     = $DeploymentReport
                        DeployOptions        = $deployOptions
                        SqlCmdVariableValues = $dacProfile.DeployOptions.SqlCommandVariableValues.Keys

                    } | Select-DefaultView -Property $defaultcolumns
                }
            }
        }
    }
}