Skip to content

Commit 84de7e7

Browse files
authored
Allow overriding the dacpac filename (#397)
* Add test project * Allow overriding the dacpac name * Add some docs about the new feature * Remove unnecessary parameter * Add scenario for same DacpacName as package * Added a dedicated section about system databases * Fix typo * Fixed another typo
1 parent 0ac886b commit 84de7e7

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

README.md

+56-19
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dotnet new sqlproj -s Sql130
4242
You should now have a project file with the following contents:
4343

4444
```xml
45-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
45+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
4646
<PropertyGroup>
4747
<TargetFramework>netstandard2.0</TargetFramework>
4848
<SqlServerVersion>Sql130</SqlServerVersion>
@@ -92,7 +92,7 @@ If you already have a SSDT (.sqlproj) project in your solution, you can keep tha
9292
There are a lot of properties that can be set on the model in the resulting `.dacpac` file which can be influenced by setting those properties in the project file using the same name. For example, the snippet below sets the `RecoveryMode` property to `Simple`:
9393

9494
```xml
95-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
95+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
9696
<PropertyGroup>
9797
<TargetFramework>netstandard2.0</TargetFramework>
9898
<RecoveryMode>Simple</RecoveryMode>
@@ -110,7 +110,7 @@ Like `.sqlproj` projects `MSBuild.Sdk.SqlProj` supports controlling T-SQL build
110110
Treating warnings as errors can be optionally enabled by adding a property `TreatTSqlWarningsAsErrors` to the project file:
111111

112112
```xml
113-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
113+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
114114
<PropertyGroup>
115115
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
116116
...
@@ -122,7 +122,7 @@ Treating warnings as errors can be optionally enabled by adding a property `Trea
122122
To suppress specific warnings from being treated as errors, add a comma-separated list of warning codes to `SuppressTSqlWarnings` property in the project file:
123123

124124
```xml
125-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
125+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
126126
<PropertyGroup>
127127
<SuppressTSqlWarnings>71558,71502</SuppressTSqlWarnings>
128128
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
@@ -134,7 +134,7 @@ To suppress specific warnings from being treated as errors, add a comma-separate
134134
You can suppress warnings for a specific file by adding `SuppressTSqlWarnings` for this file:
135135

136136
```xml
137-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
137+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
138138
<PropertyGroup>
139139
...
140140
</PropertyGroup>
@@ -154,7 +154,7 @@ Support for pre- and post deployment scripts has been added in version 1.1.0. Th
154154
To include these scripts into your `.dacpac` add the following to your `.csproj`:
155155

156156
```xml
157-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
157+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
158158
<PropertyGroup>
159159
...
160160
</PropertyGroup>
@@ -171,7 +171,7 @@ It is important to note that scripts in the `Pre-Deployment` and `Post-Deploymen
171171
By default the pre- and/or post-deployment script of referenced packages (both [PackageReference](#package-references) and [ProjectReference](#project-references)) are not run when using `dotnet publish`. As of version 1.11.0 this can be optionally enabled by adding a property `RunScriptsFromReferences` to the project file as in the below example:
172172

173173
```xml
174-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
174+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
175175
<PropertyGroup>
176176
<RunScriptsFromReferences>True</RunScriptsFromReferences>
177177
...
@@ -187,7 +187,7 @@ By default the pre- and/or post-deployment script of referenced packages (both [
187187
Especially when using pre- and post deployment scripts, but also in other scenario's, it might be useful to define variables that can be controlled at deployment time. This is supported through the use of SQLCMD variables, added in version 1.1.0. These variables can be defined in your project file using the following syntax:
188188

189189
```xml
190-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
190+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
191191
<PropertyGroup>
192192
...
193193
</PropertyGroup>
@@ -205,13 +205,13 @@ Especially when using pre- and post deployment scripts, but also in other scenar
205205
</Project>
206206
```
207207

208-
> Note: In versions prior to 1.11.0 the `DefaultValue` element displayed above was not used. As of version 1.11.0 the value of `Value` is checked first and if it found to be empty we'll fall back to `DefaultValue`.
208+
> Note: In versions prior to 1.11.0 the `DefaultValue` element displayed above was not used. As of version 1.11.0 the value of `Value` is checked first and if it found to be empty we'll fall back to `DefaultValue`.
209209
210210
## Package references
211211
`MSBuild.Sdk.SqlProj` supports referencing NuGet packages that contain `.dacpac` packages. These can be referenced by using the `PackageReference` format familiar to .NET developers. They can also be installed through the NuGet Package Manager in Visual Studio.
212212

213213
```xml
214-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
214+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
215215
<PropertyGroup>
216216
<TargetFramework>netstandard2.0</TargetFramework>
217217
</PropertyGroup>
@@ -222,10 +222,27 @@ Especially when using pre- and post deployment scripts, but also in other scenar
222222
</Project>
223223
```
224224

225-
It will assume that the `.dacpac` file is inside the `tools` folder of the referenced package and that it has the same name as the NuGet package. Referenced packages that do not adhere to this convention will be silently ignored. By default the package reference is treated as being part of the same database. For example, if the reference package contains a `.dacpac` that has a table and a stored procedure and you would `dotnet publish` the project the table and stored procedure from that package will be deployed along with the contents of your project to the same database. If this is not desired, you can add the `DatabaseVariableLiteralValue` item metadata to the `PackageReference` specifying a different database name:
225+
It will assume that the `.dacpac` file is inside the `tools` folder of the referenced package and that it has the same name as the NuGet package. Referenced packages that do not adhere to this convention will be silently ignored. However, you have the ability to override this convention by using the `DacpacName` attribute on the `PackageReference` (introduced in version 2.5.0). For example:
226226

227227
```xml
228-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
228+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
229+
<PropertyGroup>
230+
<TargetFramework>netstandard2.0</TargetFramework>
231+
<SqlServerVersion>Sql160</SqlServerVersion>
232+
</PropertyGroup>
233+
234+
<ItemGroup>
235+
<PackageReference Include="MyDatabasePackage" Version="1.1.0" DacpacName="SomeOtherDatabase" />
236+
</ItemGroup>
237+
</Project>
238+
```
239+
240+
This will add a reference to the `tools\SomeOtherDatabase.dacpac` file inside the `MyDatabasePackage` package. Note that if that file doesn't exist within the package, the package reference will still be silently ignored. However the build will most likely fail if your project actually references objects from the reference package.
241+
242+
By default the package reference is treated as being part of the same database. For example, if the reference package contains a `.dacpac` that has a table and a stored procedure and you would `dotnet publish` the project the table and stored procedure from that package will be deployed along with the contents of your project to the same database. If this is not desired, you can add the `DatabaseVariableLiteralValue` item metadata to the `PackageReference` specifying a different database name:
243+
244+
```xml
245+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
229246
<PropertyGroup>
230247
<TargetFramework>netstandard2.0</TargetFramework>
231248
</PropertyGroup>
@@ -242,7 +259,7 @@ You can also use SQLCMD variables to set references, similar to the behavior of
242259
>Note: Don't forget to define appropriate [SQLCMD variables](#sqlcmd-variables)
243260
244261
```xml
245-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
262+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
246263
<PropertyGroup>
247264
<TargetFramework>netstandard2.0</TargetFramework>
248265
</PropertyGroup>
@@ -264,7 +281,7 @@ You can also use SQLCMD variables to set references, similar to the behavior of
264281
</Project>
265282
```
266283
In this scenario you can access the objects defined by `MyDatabasePackage` by using the `[$(SomeOtherServer)].[$(SomeOtherDatabase)].[<schema>].[<object>]` syntax.
267-
Also you can combine `ServerSqlCmdVariable` with `DatabaseVariableLiteralValue` and use `[$(SomeOtherServer)].[SomeOtherDatabase].[<schema>].[<object>]` syntax
284+
Also you can combine `ServerSqlCmdVariable` with `DatabaseVariableLiteralValue` and use `[$(SomeOtherServer)].[SomeOtherDatabase].[<schema>].[<object>]` syntax.
268285

269286
When deploying a dacpac with references to other dacpacs, if you want the contents of all dacpacs to be deployed to a single database you will need to specify the `IncludeCompositeObjects` property. For example:
270287

@@ -279,11 +296,31 @@ sqlpackage
279296
/Properties:IncludeCompositeObjects=True
280297
```
281298

299+
## Referencing system databases
300+
Microsoft has recently released NuGet packages containing the definitions of the `master` and `msdb` databases. This is useful if you want to reference objects from those databases within your own projects without getting warnings. To reference these, you'll need to use at least version 2.5.0 of MSBuild.Sdk.SqlProj as you'll need to use the `DacpacName` feature for package references described above. For example:
301+
302+
```xml
303+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
304+
<PropertyGroup>
305+
<TargetFramework>netstandard2.0</TargetFramework>
306+
<SqlServerVersion>160</SqlServerVersion>
307+
</PropertyGroup>
308+
309+
<ItemGroup>
310+
<PackageReference Include="Microsoft.SqlServer.Dacpacs" Version="160.0.0" DacpacName="master" />
311+
</ItemGroup>
312+
</Project>
313+
```
314+
315+
The above example references the `master` database from the [Microsoft.SqlServer.Dacpacs](https://www.nuget.org/packages/Microsoft.SqlServer.Dacpacs) NuGet package. Please note that there are different versions of that package for different versions of SQL Server. It is recommended to reference the same version of the package as the `SqlServerVersion` you are targeting with your project, as seen in the example above.
316+
317+
For the Azure SQL and Synapse variants of SQL Server there are dedicated packages: [Microsoft.SqlServer.Dacpacs.Azure](https://www.nuget.org/packages/Microsoft.SqlServer.Dacpacs.Azure) and [Microsoft.SqlServer.Dacpacs.Synapse](https://www.nuget.org/packages/Microsoft.SqlServer.Dacpacs.Synapse) respectively.
318+
282319
## Project references
283320
Similar to package references you can also reference another project by using a `ProjectReference`. These references can be added manually to the project file or they can be added through Visual Studio. For example, consider the following example:
284321

285322
```xml
286-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
323+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
287324
<PropertyGroup>
288325
<TargetFramework>netstandard2.0</TargetFramework>
289326
</PropertyGroup>
@@ -297,7 +334,7 @@ Similar to package references you can also reference another project by using a
297334
This will ensure that `MyOtherProject` is built first and the resulting `.dacpac` will be referenced by this project. This means you can use the objects defined in the other project within the scope of this project. If the other project is representing an entirely different database you can also use `DatabaseVariableLiteralValue` or SQLCMD variables on the `ProjectReference` similar to `PackageReference`:
298335

299336
```xml
300-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
337+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
301338
<PropertyGroup>
302339
<TargetFramework>netstandard2.0</TargetFramework>
303340
</PropertyGroup>
@@ -331,7 +368,7 @@ In order to solve circular references between databases that may have been incor
331368
`SuppressMissingDependenciesErrors` to both [Package References](#package-references) and [ProjectReferences](#project-references)):
332369

333370
```xml
334-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
371+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
335372
<PropertyGroup>
336373
<TargetFramework>netstandard2.0</TargetFramework>
337374
</PropertyGroup>
@@ -451,7 +488,7 @@ To further customize the deployment process, you can use the following propertie
451488
In addition to these properties, you can also set any of the [documented](https://docs.microsoft.com/dotnet/api/microsoft.sqlserver.dac.dacdeployoptions) deployment options. These are typically set in the project file, for example:
452489

453490
```xml
454-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
491+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
455492
<PropertyGroup>
456493
...
457494
<BackupDatabaseBeforeChanges>True</BackupDatabaseBeforeChanges>
@@ -474,7 +511,7 @@ Most of those properties are simple values (like booleans, strings and integers)
474511
Instead of using `dotnet publish` to deploy changes to a database, you can also have a full SQL script generated that will create the database from scratch and then run that script against a SQL Server. This can be achieved by adding the following to the project file:
475512

476513
```xml
477-
<Project Sdk="MSBuild.Sdk.SqlProj/2.2.0">
514+
<Project Sdk="MSBuild.Sdk.SqlProj/2.5.0">
478515
<PropertyGroup>
479516
<GenerateCreateScript>True</GenerateCreateScript>
480517
<IncludeCompositeObjects>True</IncludeCompositeObjects>

src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@
118118
-->
119119
<PhysicalLocation Condition="'%(_ResolvedPackageReference.PhysicalLocation)'==''">$([System.String]::new('$(NuGetPackageRoot)%(PackageReference.Identity)/%(PackageReference.Version)').ToLower())</PhysicalLocation>
120120

121-
<DacpacFile>%(_ResolvedPackageReference.PhysicalLocation)/tools/%(Identity).dacpac</DacpacFile>
121+
<DacpacFile>%(_ResolvedPackageReference.PhysicalLocation)/tools/%(PackageReference.Identity).dacpac</DacpacFile>
122+
<DacpacFile Condition="Exists('%(_ResolvedPackageReference.PhysicalLocation)/tools/%(PackageReference.DacpacName).dacpac')">%(_ResolvedPackageReference.PhysicalLocation)/tools/%(PackageReference.DacpacName).dacpac</DacpacFile>
122123
<DatabaseVariableLiteralValue>%(PackageReference.DatabaseVariableLiteralValue)</DatabaseVariableLiteralValue>
123124
<SuppressMissingDependenciesErrors>%(PackageReference.SuppressMissingDependenciesErrors)</SuppressMissingDependenciesErrors>
124125
<!-- Constructs variable to make external parts -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE PROCEDURE [dbo].[csp_MyProcedureThatUsesMaster]
2+
AS
3+
BEGIN
4+
SELECT * FROM sys.master_files;
5+
END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project>
2+
<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.props" />
3+
4+
<PropertyGroup>
5+
<TargetFramework>netstandard2.0</TargetFramework>
6+
<SqlServerVersion>Sql150</SqlServerVersion>
7+
<RecoveryMode>Simple</RecoveryMode>
8+
<PackageProjectUrl>https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/</PackageProjectUrl>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.SqlServer.Dacpacs" Version="150.0.0" DacpacName="master" />
13+
</ItemGroup>
14+
15+
<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.targets" />
16+
</Project>

test/TestProjectWithPackageReference/TestProjectWithPackageReference.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="TestProject" Version="1.2.0-beta.9.gf2d69b1c16" />
18+
<PackageReference Include="TestProject" Version="1.2.0-beta.9.gf2d69b1c16" DacpacName="TestProject" />
1919
</ItemGroup>
2020

2121
<Import Project="$(MSBuildThisFileDirectory)../../src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets" />

0 commit comments

Comments
 (0)