Skip to content

Commit

Permalink
Add support for "bring your own rules" (or update existing) (#533)
Browse files Browse the repository at this point in the history
* Add support for "bring your own rules" (or update existing)

fixes #530

* PR updates
  • Loading branch information
ErikEJ authored Mar 7, 2024
1 parent d2a10ac commit d917489
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dotnet new sqlproj -s Sql130
You should now have a project file with the following contents:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>Sql130</SqlServerVersion>
Expand Down Expand Up @@ -94,7 +94,7 @@ If you already have a SSDT (.sqlproj) project in your solution, you can keep tha
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`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RecoveryMode>Simple</RecoveryMode>
Expand All @@ -112,7 +112,7 @@ Like `.sqlproj` projects `MSBuild.Sdk.SqlProj` supports controlling T-SQL build
Treating warnings as errors can be optionally enabled by adding a property `TreatTSqlWarningsAsErrors` to the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
...
Expand All @@ -124,7 +124,7 @@ Treating warnings as errors can be optionally enabled by adding a property `Trea
To suppress specific warnings from being treated as errors, add a comma-separated list of warning codes to `SuppressTSqlWarnings` property in the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<SuppressTSqlWarnings>71558,71502</SuppressTSqlWarnings>
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
Expand All @@ -136,7 +136,7 @@ To suppress specific warnings from being treated as errors, add a comma-separate
You can suppress warnings for a specific file by adding `SuppressTSqlWarnings` for this file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -156,7 +156,7 @@ Support for pre- and post deployment scripts has been added in version 1.1.0. Th
To include these scripts into your `.dacpac` add the following to your `.csproj`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -173,7 +173,7 @@ It is important to note that scripts in the `Pre-Deployment` and `Post-Deploymen
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<RunScriptsFromReferences>True</RunScriptsFromReferences>
...
Expand All @@ -189,7 +189,7 @@ By default the pre- and/or post-deployment script of referenced packages (both [
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -213,7 +213,7 @@ Especially when using pre- and post deployment scripts, but also in other scenar
`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.

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -227,7 +227,7 @@ Especially when using pre- and post deployment scripts, but also in other scenar
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>Sql160</SqlServerVersion>
Expand All @@ -244,7 +244,7 @@ This will add a reference to the `tools\SomeOtherDatabase.dacpac` file inside th
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -261,7 +261,7 @@ You can also use SQLCMD variables to set references, similar to the behavior of
>Note: Don't forget to define appropriate [SQLCMD variables](#sqlcmd-variables)
```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand Down Expand Up @@ -302,7 +302,7 @@ sqlpackage
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>160</SqlServerVersion>
Expand All @@ -322,7 +322,7 @@ For other variants of SQL Server / Azure SQL Database there are dedicated packag
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -336,7 +336,7 @@ Similar to package references you can also reference another project by using a
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`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand Down Expand Up @@ -370,7 +370,7 @@ In order to solve circular references between databases that may have been incor
`SuppressMissingDependenciesErrors` to both [Package References](#package-references) and [ProjectReferences](#project-references)):

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand Down Expand Up @@ -416,7 +416,7 @@ In order to solve circular references between databases that may have been incor
Additionally you'll need to set the `PackageProjectUrl` property inside of the `.csproj` like this:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
...
<PackageProjectUrl>your-project-url</PackageProjectUrl>
Expand Down Expand Up @@ -490,7 +490,7 @@ To further customize the deployment process, you can use the following propertie
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
...
<BackupDatabaseBeforeChanges>True</BackupDatabaseBeforeChanges>
Expand All @@ -513,7 +513,7 @@ Most of those properties are simple values (like booleans, strings and integers)
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:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<GenerateCreateScript>True</GenerateCreateScript>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
Expand All @@ -539,7 +539,7 @@ Starting with version 2.7.0 of the SDK, there is support for running static code
Static code analysis can be enabled by adding the `RunSqlCodeAnalysis` property to the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>-SqlServer.Rules.SRD0006;-Smells.*</CodeAnalysisRules>
Expand All @@ -556,14 +556,24 @@ Any rule violations found during analysis are reported as build warnings.
Individual rule violations can be configured to be reported as build errors as shown below.

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>+!SqlServer.Rules.SRN0005</CodeAnalysisRules>
</PropertyGroup>
</Project>
```

You can also bring your own rules. For an example of custom rules, see [this repository](https://github.com/ErikEJ/SqlServer.Rules).

To use custom rules, place the rule .dll files in a `Rules` folder in the project, and add them as Content items:

```xml
<ItemGroup>
<Content Include="Rules\My.Own.Rules.dll" />
</ItemGroup>
```

## Workaround for parser errors (SQL46010)
This project relies on the publicly available T-SQL parser which may not support all T-SQL syntax constructions. Therefore you might encounter a SQL46010 error if you have a script file that contains unsupported syntax. If that happens, there's a couple of workarounds you can try:

Expand Down
26 changes: 26 additions & 0 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public PackageAnalyzer(IConsole console, string rulesExpression)
BuildRuleLists(rulesExpression);
}

public void AddRulesFile(FileInfo inputFile)
{
// Make sure the file exists
if (!inputFile.Exists)
{
throw new ArgumentException($"Unable to find rules file {inputFile}", nameof(inputFile));
}

if (inputFile.Directory.Name.Equals("rules", StringComparison.OrdinalIgnoreCase)
&& inputFile.Extension.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
CopyAdditionalRulesFile(inputFile);
}
}

public void Analyze(TSqlModel model, FileInfo outputFile)
{
_console.WriteLine($"Analyzing package '{outputFile.FullName}'");
Expand Down Expand Up @@ -102,5 +117,16 @@ private static string GetOutputFileName(FileInfo outputFile)
}
return outputFileName + ".CodeAnalysis.xml";
}

private void CopyAdditionalRulesFile(FileInfo rulesFile)
{
var destPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

var dest = Path.Combine(destPath, rulesFile.Name);

rulesFile.CopyTo(dest, overwrite: true);

_console.WriteLine($"Adding additional rules file from '{rulesFile.FullName}' to '{dest}'");
}
}
}
7 changes: 7 additions & 0 deletions src/DacpacTool/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public void AddInputFile(FileInfo inputFile)
throw new ArgumentException($"Unable to find input file {inputFile}", nameof(inputFile));
}

// Skip custom rules files, they will be added to the tools folder later by the analyzer
if (inputFile.Directory.Name.Equals("rules", StringComparison.OrdinalIgnoreCase)
&& inputFile.Extension.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
return;
}

Console.WriteLine($"Adding {inputFile.FullName} to the model");
Model.AddOrUpdateObjects(File.ReadAllText(inputFile.FullName), inputFile.FullName, new TSqlObjectOptions());
}
Expand Down
7 changes: 7 additions & 0 deletions src/DacpacTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ private static int BuildDacpac(BuildOptions options)
if (options.RunCodeAnalysis)
{
var analyzer = new PackageAnalyzer(new ActualConsole(), options.CodeAnalysisRules);

foreach (var line in File.ReadLines(options.InputFile.FullName))
{
FileInfo inputFile = new FileInfo(line); // Validation occurs in AddRulesFile
analyzer.AddRulesFile(inputFile);
}

analyzer.Analyze(packageBuilder.Model, options.Output);
}

Expand Down
Binary file not shown.
Binary file not shown.
9 changes: 7 additions & 2 deletions test/TestProjectWithAnalyzers/TestProjectWithAnalyzers.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<Project>
<Project>
<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>Sql150</SqlServerVersion>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>-SqlServer.Rules.SRD0006;-Smells.*;+!SqlServer.Rules.SRN0002</CodeAnalysisRules>
<CodeAnalysisRules>-SqlServer.Rules.SRD0006;-Smells.*;+!SqlServer.Rules.SRN0002</CodeAnalysisRules>
</PropertyGroup>

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

<ItemGroup>
<Content Include="Rules\SqlServer.Dac.dll" />
<Content Include="Rules\SqlServer.Rules.dll" />
</ItemGroup>
</Project>

0 comments on commit d917489

Please sign in to comment.