Skip to content

Commit 3ede96b

Browse files
Introduce New-DbaDacPackage for source-based DACPAC builds and improve DacFx clarity in exports (#10059)
1 parent 552bf89 commit 3ede96b

6 files changed

Lines changed: 585 additions & 30 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "2025.12.4-preview-csvvvvvv-20251204203741",
2+
"version": "2025.12.21-preview-main-20251221192830",
33
"notes": "Version of dbatools.library to use for CI/CD and development"
44
}

dbatools.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@
496496
'New-DbaCredential',
497497
'New-DbaCustomError',
498498
'New-DbaDacOption',
499+
'New-DbaDacPackage',
499500
'New-DbaDacProfile',
500501
'New-DbaDatabase',
501502
'New-DbaDbAsymmetricKey',

dbatools.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ if ($PSVersionTable.PSVersion.Major -lt 5) {
648648
'Test-DbaBackupInformation',
649649
'Invoke-DbaBalanceDataFiles',
650650
'Select-DbaBackupInformation',
651+
'New-DbaDacPackage',
651652
'Publish-DbaDacPackage',
652653
'Copy-DbaDbTableData',
653654
'Copy-DbaDbViewData',

public/Export-DbaDacPackage.ps1

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
function Export-DbaDacPackage {
22
<#
33
.SYNOPSIS
4-
Creates DACPAC or BACPAC deployment packages from SQL Server databases using SqlPackage
4+
Exports DACPAC or BACPAC packages from SQL Server databases using the DacFx framework
55
66
.DESCRIPTION
7-
Creates database deployment packages for version control, migrations, and schema distribution. Generates DACPAC files containing database schema definitions or BACPAC files that include both schema and data. This automates the SqlPackage utility so you don't have to remember complex command-line syntax or manage connection strings manually.
7+
Creates database deployment packages for version control, migrations, and schema distribution. Generates DACPAC files containing database schema definitions or BACPAC files that include both schema and data.
88
99
Perfect for creating deployable packages from development databases, capturing schema snapshots for source control, or preparing migration artifacts for different environments. The function handles multiple databases in batch operations and provides flexible table filtering when you only need specific objects.
1010
11-
Uses Microsoft DAC Services under the hood with automatic SqlPackage installation if needed. Note that extraction can fail with three-part references to external databases or complex cross-database dependencies.
11+
Uses Microsoft DacFx API from dbatools.library. Note that extraction can fail with three-part references to external databases or complex cross-database dependencies.
1212
1313
For help with the extract action parameters and properties, refer to https://learn.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-extract
1414
@@ -55,10 +55,12 @@ function Export-DbaDacPackage {
5555
.PARAMETER ExtendedParameters
5656
Passes additional command-line parameters directly to SqlPackage.exe for advanced scenarios (e.g., '/OverwriteFiles:true /Quiet:true').
5757
Use this when you need SqlPackage options not available through DacOption or when integrating with existing SqlPackage workflows.
58+
Note: This parameter requires SqlPackage.exe to be installed via Install-DbaSqlPackage or locally.
5859
5960
.PARAMETER ExtendedProperties
6061
Passes additional property settings directly to SqlPackage.exe for fine-tuned control over extraction behavior.
6162
Use this when you need to set specific SqlPackage properties that aren't exposed through the standard DacOption parameter.
63+
Note: This parameter requires SqlPackage.exe to be installed via Install-DbaSqlPackage.
6264
6365
.PARAMETER EnableException
6466
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
@@ -98,11 +100,10 @@ function Export-DbaDacPackage {
98100
PS C:\> $moreparams = "/OverwriteFiles:$true /Quiet:$true"
99101
PS C:\> Export-DbaDacPackage -SqlInstance sql2016 -Database SharePoint_Config -Path C:\temp -ExtendedParameters $moreparams
100102
101-
Using extended parameters to over-write the files and performs the extraction in quiet mode to C:\temp\sql2016-SharePoint_Config-20201227140759-dacpackage.dacpac. Uses command line instead of SMO behind the scenes. As noted the generated filename will contain the server name, database name, and the current timestamp in the "%Y%m%d%H%M%S" format.
103+
Using extended parameters to over-write the files and performs the extraction in quiet mode to C:\temp\sql2016-SharePoint_Config-20201227140759-dacpackage.dacpac. Uses SqlPackage.exe command line instead of DacFx API behind the scenes. As noted the generated filename will contain the server name, database name, and the current timestamp in the "%Y%m%d%H%M%S" format.
102104
#>
103105
[CmdletBinding(DefaultParameterSetName = 'SMO')]
104-
param
105-
(
106+
param (
106107
[parameter(Mandatory, ValueFromPipeline)]
107108
[DbaInstance[]]$SqlInstance,
108109
[PSCredential]$SqlCredential,
@@ -128,25 +129,12 @@ function Export-DbaDacPackage {
128129
begin {
129130
$null = Test-ExportDirectory -Path $Path
130131

131-
# Check if sqlpackage is available
132-
$sqlPackagePath = Get-DbaSqlPackagePath
133-
if (-not $sqlPackagePath) {
134-
$installChoice = Read-Host "SqlPackage is required but not found. Would you like to install it now using Install-DbaSqlPackage? (Y/N)"
135-
if ($installChoice -match '^[Yy]') {
136-
try {
137-
Install-DbaSqlPackage
138-
Write-Message -Level Output -Message "SqlPackage installed successfully. Continuing with export..."
139-
$sqlPackagePath = Get-DbaSqlPackagePath
140-
if (-not $sqlPackagePath) {
141-
Stop-Function -Message "Failed to locate SqlPackage after installation. Please verify the installation." -EnableException:$EnableException
142-
return
143-
}
144-
} catch {
145-
Stop-Function -Message "Failed to install SqlPackage. Please install manually or use Install-DbaSqlPackage." -EnableException:$EnableException
146-
return
147-
}
148-
} else {
149-
Stop-Function -Message "SqlPackage is required for this operation. Please install SqlPackage manually or use Install-DbaSqlPackage." -EnableException:$EnableException
132+
# For CMD parameter set (ExtendedParameters/ExtendedProperties), we need SqlPackage.exe
133+
# For SMO parameter set (default), we use the DacFx API from dbatools.library
134+
if ($PSCmdlet.ParameterSetName -eq 'CMD') {
135+
$sqlPackagePath = Get-DbaSqlPackagePath
136+
if (-not $sqlPackagePath) {
137+
Stop-Function -Message "SqlPackage.exe is required when using -ExtendedParameters or -ExtendedProperties. Install it using Install-DbaSqlPackage or use the default DacFx API mode without these parameters."
150138
return
151139
}
152140
}
@@ -280,12 +268,12 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
280268

281269
$FilePath = Get-ExportFilePath -Path $PSBoundParameters.Path -FilePath $PSBoundParameters.FilePath -Type $ext -ServerName $instance -DatabaseName $dbName
282270

283-
#using SMO by default
271+
#using DacFx API by default
284272
if ($PsCmdlet.ParameterSetName -eq 'SMO') {
285273
try {
286274
$dacSvc = New-Object -TypeName Microsoft.SqlServer.Dac.DacServices -ArgumentList $connstring -ErrorAction Stop
287275
} catch {
288-
Stop-Function -Message "Could not connect to the connection string $connstring"-Target $instance -Continue
276+
Stop-Function -Message "Could not connect to the connection string $connstring" -Target $instance -Continue
289277
}
290278
if (-not $DacOption) {
291279
$opts = New-DbaDacOption -Type $Type -Action Export
@@ -351,7 +339,7 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
351339
}
352340

353341
if ($process.ExitCode -ne 0) {
354-
Stop-Function -Message "Standard output - $stderr"-Continue
342+
Stop-Function -Message "Standard output - $stderr" -Continue
355343
}
356344
}
357345
[PSCustomObject]@{
@@ -366,4 +354,4 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
366354
}
367355
}
368356
}
369-
}
357+
}

0 commit comments

Comments
 (0)