Skip to content

Instantly share code, notes, and snippets.

@nvarscar
Created August 24, 2018 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvarscar/5efdb01fb4e4916886559896d660bd84 to your computer and use it in GitHub Desktop.
Save nvarscar/5efdb01fb4e4916886559896d660bd84 to your computer and use it in GitHub Desktop.
[Cmdletbinding()]
Param (
$SourceServer,
$TargetServer = $SourceServer,
$SourceDatabaseName,
$TargetDatabaseName,
$Path,
$PublishXml = '.\publish.xml',
[switch]$IncludeData
)
#Stop on any error by default
$ErrorActionPreference = 'Stop'
# Construct export parameters
$exportProperties = "/p:IgnorePermissions=True /p:IgnoreUserLoginMappings=True"
if ($IncludeData) {
$exportProperties += " /p:ExtractAllTableData=True"
}
#Export database to path
Write-Verbose "Starting the export from $SourceServer.$SourceDatabaseName to $Path"
$exportFile = Export-DbaDacpac -SqlInstance $SourceServer -Database $SourceDatabaseName -Path $Path -ExtendedProperties $exportProperties -EnableException
Write-Verbose "Export completed`: $exportFile"
#publish dacpac with defined publish xml file
Write-Verbose "Starting the publication from $($exportFile.Path) to $TargetServer.$TargetDatabaseName"
$xml = (Get-Item $PublishXml).FullName
Publish-DbaDacpac -SqlInstance $TargetServer -Database $TargetDatabaseName -PublishXml $xml -Path $exportFile.Path -EnableException
#remove dacpac file
if (Test-Path $exportFile.Path) {
Write-Verbose "Removing dacpac file $($exportFile.Path)"
Remove-Item $exportFile.Path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment