Created
November 17, 2017 23:54
-
-
Save RichieBzzzt/50f67846aca0e93da46437be6f420bfa to your computer and use it in GitHub Desktop.
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
Function Publish-DatabaseDeployment { | |
param( | |
$dacfxPath | |
, $dacpac | |
, $publishXml | |
, $targetConnectionString | |
, $targetDatabaseName | |
) | |
Write-Verbose 'Testing if DACfx was installed...' -Verbose | |
Write-Verbose $dacfxPath -Verbose | |
if (!$dacfxPath) { | |
throw 'No usable version of Dac Fx found.' | |
} | |
else { | |
try { | |
Write-Verbose 'DacFX found, attempting to load DAC assembly...' -Verbose | |
Add-Type -Path $dacfxPath | |
Write-Verbose 'Loaded DAC assembly.' -Verbose | |
} | |
catch [System.Management.Automation.RuntimeException] { | |
throw "Exception caught: " + $_.Exception.GetType().FullName | |
} | |
} | |
if (Test-Path $dacpac) { | |
$dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($Dacpac) | |
Write-Host ('Loaded dacpac ''{0}''.' -f $Dacpac) -ForegroundColor White -BackgroundColor DarkMagenta | |
} | |
else { | |
Write-Verbose "$dacpac not found!" -Verbose | |
throw | |
} | |
if (Test-Path $publishXml) { | |
$dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($publishXml) | |
Write-Host ('Loaded publish profile ''{0}''.' -f $publishXml) -ForegroundColor White -BackgroundColor DarkMagenta | |
} | |
else { | |
Write-Verbose "$publishXml not found!" -Verbose | |
throw | |
} | |
$dacServices = New-Object Microsoft.SqlServer.Dac.DacServices $targetConnectionString | |
try { | |
Write-Host "Executing Deployment..." -ForegroundColor Yellow | |
Register-ObjectEvent -InputObject $dacServices -EventName "Message" -Source "msg" -Action { Write-Host $EventArgs.Message.Message } | Out-Null | |
$dacServices.Deploy($dacPackage, $targetDatabaseName, $true, $dacProfile.DeployOptions, $null) | |
Unregister-Event -SourceIdentifier "msg" | |
Write-Host "Deployment successful!" -ForegroundColor DarkGreen | |
} | |
catch [Microsoft.SqlServer.Dac.DacServicesException] { | |
throw ('Deployment failed: ''{0}'' Reason: ''{1}''' -f $_.Exception.Message, $_.Exception.InnerException.Message) | |
} | |
} | |
$svrConnstring = "SERVER=.;Integrated Security=True;Database=master" | |
$WWI_OLTP_NAME = "WideWorldImporters" | |
$WWI_OLTP = Join-Path (Split-Path -Path $PSScriptRoot -Parent) "C:\projects" | |
$WWI_OLTP_DAC = Join-Path $WWI_OLTP "\Microsoft.Data.Tools.Msbuild\lib\net46" | |
$WWI_OLTP_DACFX = Join-Path $WWI_OLTP_DAC "\Microsoft.SqlServer.Dac.dll" | |
$WWI_OLTP_DACPAC = Join-Path $WWI_OLTP "\bin\Debug\WideWorldImporters.dacpac" | |
$WWI_OLTP_PUB = Join-Path $WWI_OLTP "\bin\Debug\WideWorldImporters.publish.xml" | |
#run the darn thing! | |
Publish-DatabaseDeployment -dacfxPath $WWI_OLTP_DACFX -dacpac $WWI_OLTP_DACPAC -publishXml $WWI_OLTP_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_OLTP_NAME | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment