diff --git a/src/Tests/Tests.SqlServerSchemaSettingsCustom.verified.sql b/src/Tests/Tests.SqlServerSchemaSettingsCustom.verified.sql new file mode 100644 index 0000000..bda065e --- /dev/null +++ b/src/Tests/Tests.SqlServerSchemaSettingsCustom.verified.sql @@ -0,0 +1,26 @@ +-- Tables + +CREATE TABLE [dbo].[MyTable]( + [Value] [int] NULL +) ON [PRIMARY] + +CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[MyTable] +( + [Value] ASC +) ON [PRIMARY] + +CREATE TRIGGER MyTrigger +ON MyTable +AFTER UPDATE +AS RAISERROR ('Notify Customer Relations', 16, 10); + +ALTER TABLE [dbo].[MyTable] ENABLE TRIGGER [MyTrigger] + + +-- Views + +CREATE VIEW MyView +AS + SELECT Value + FROM MyTable + WHERE (Value > 10); \ No newline at end of file diff --git a/src/Tests/Tests.cs b/src/Tests/Tests.cs index 75b4a3e..d7de344 100644 --- a/src/Tests/Tests.cs +++ b/src/Tests/Tests.cs @@ -1,4 +1,3 @@ -using System.Data; using System.Data.SqlTypes; using Microsoft.Data.SqlClient; using Microsoft.SqlServer.Management.Common; @@ -337,4 +336,27 @@ await Verify(connection) #endregion } + + [Test] + public async Task SqlServerSchemaSettingsCustom() + { + await using var database = await sqlInstance.Build(); + var connection = database.Connection; + + #region SqlServerSchemaSettings + + await Verify(connection) + .SchemaSettings(new SchemaSettings + { + StoredProcedures = true, + UserDefinedFunctions = true, + Synonyms = true, + Tables = true, + Views = true, + // this should cover tables & views but not stored procs / others + IncludeItem = (item) => item is TableViewBase, + }); + + #endregion + } } \ No newline at end of file diff --git a/src/Verify.SqlServer/SchemaValidation/SchemaSettings.cs b/src/Verify.SqlServer/SchemaValidation/SchemaSettings.cs index e0431f5..e52e570 100644 --- a/src/Verify.SqlServer/SchemaValidation/SchemaSettings.cs +++ b/src/Verify.SqlServer/SchemaValidation/SchemaSettings.cs @@ -1,15 +1,11 @@ -class SchemaSettings( - bool storedProcedures, - bool tables, - bool views, - bool userDefinedFunctions, - bool synonyms, - Func includeItem) +using Microsoft.SqlServer.Management.Smo; + +public class SchemaSettings() { - public bool StoredProcedures { get; } = storedProcedures; - public bool Tables { get; } = tables; - public bool Views { get; } = views; - public bool Synonyms { get; } = synonyms; - public bool UserDefinedFunctions { get; } = userDefinedFunctions; - public Func IncludeItem { get; } = includeItem; + public bool StoredProcedures { get; init; } = true; + public bool Tables { get; init; } = true; + public bool Views { get; init; } = true; + public bool Synonyms { get; init; } = true; + public bool UserDefinedFunctions { get; init; } = true; + public Func IncludeItem { get; init; } = (_) => true; } \ No newline at end of file diff --git a/src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs b/src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs index 348a182..d49a23e 100644 --- a/src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs +++ b/src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs @@ -1,4 +1,4 @@ -using Microsoft.Data.SqlClient; +using Microsoft.Data.SqlClient; using Microsoft.SqlServer.Management.Smo; class SqlScriptBuilder(SchemaSettings settings) @@ -122,7 +122,7 @@ void AppendType(StringBuilder stringBuilder, ScriptingOptions options, SmoCol where T : NamedSmoObject, IScriptable { var filtered = items.Cast() - .Where(_ => !isSystem(_) && settings.IncludeItem(_.Name)) + .Where(_ => !isSystem(_) && settings.IncludeItem(_)) .ToList(); if (filtered.Count == 0) { diff --git a/src/Verify.SqlServer/SchemaValidation/VerifySettingsExtensions.cs b/src/Verify.SqlServer/SchemaValidation/VerifySettingsExtensions.cs index d779a84..96f2f3d 100644 --- a/src/Verify.SqlServer/SchemaValidation/VerifySettingsExtensions.cs +++ b/src/Verify.SqlServer/SchemaValidation/VerifySettingsExtensions.cs @@ -1,4 +1,4 @@ -namespace VerifyTests; +namespace VerifyTests; public static class VerifySettingsSqlExtensions { @@ -34,13 +34,23 @@ public static void SchemaSettings( settings.Context.Add( "SqlServer", - new SchemaSettings( - storedProcedures, - tables, - views, - userDefinedFunctions, - synonyms, - includeItem)); + new SchemaSettings + { + StoredProcedures = storedProcedures, + Tables = tables, + Views = views, + UserDefinedFunctions = userDefinedFunctions, + Synonyms = synonyms, + IncludeItem = (_) => includeItem(_.Name), + }); + } + + public static void SchemaSettings(this VerifySettings settings, SchemaSettings schema) => settings.Context.Add("SqlServer", schema); + + public static SettingsTask SchemaSettings(this SettingsTask settings, SchemaSettings schema) + { + settings.CurrentSettings.SchemaSettings(schema); + return settings; } internal static SchemaSettings GetSchemaSettings(this IReadOnlyDictionary context) @@ -53,5 +63,5 @@ internal static SchemaSettings GetSchemaSettings(this IReadOnlyDictionary true); + static SchemaSettings defaultSettings = new(); } \ No newline at end of file