You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: public/Export-DbaDacPackage.ps1
+17-29Lines changed: 17 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
functionExport-DbaDacPackage {
2
2
<#
3
3
.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
5
5
6
6
.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.
8
8
9
9
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.
10
10
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.
12
12
13
13
For help with the extract action parameters and properties, refer to https://learn.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-extract
14
14
@@ -55,10 +55,12 @@ function Export-DbaDacPackage {
55
55
.PARAMETERExtendedParameters
56
56
Passes additional command-line parameters directly to SqlPackage.exe for advanced scenarios (e.g., '/OverwriteFiles:true /Quiet:true').
57
57
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.
58
59
59
60
.PARAMETERExtendedProperties
60
61
Passes additional property settings directly to SqlPackage.exe for fine-tuned control over extraction behavior.
61
62
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.
62
64
63
65
.PARAMETEREnableException
64
66
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 {
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.
102
104
#>
103
105
[CmdletBinding(DefaultParameterSetName='SMO')]
104
-
param
105
-
(
106
+
param (
106
107
[parameter(Mandatory,ValueFromPipeline)]
107
108
[DbaInstance[]]$SqlInstance,
108
109
[PSCredential]$SqlCredential,
@@ -128,25 +129,12 @@ function Export-DbaDacPackage {
128
129
begin {
129
130
$null=Test-ExportDirectory-Path $Path
130
131
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."
150
138
return
151
139
}
152
140
}
@@ -280,12 +268,12 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
0 commit comments