Skip to content

Commit

Permalink
Merge branch 'main' into parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jun 20, 2024
2 parents 255267e + 6e0668c commit a68eb33
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 25 deletions.
26 changes: 26 additions & 0 deletions src/Tests/Tests.SqlServerSchemaSettingsCustom.verified.sql
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 23 additions & 1 deletion src/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Data;
using System.Data.SqlTypes;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
Expand Down Expand Up @@ -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
}
}
22 changes: 9 additions & 13 deletions src/Verify.SqlServer/SchemaValidation/SchemaSettings.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
class SchemaSettings(
bool storedProcedures,
bool tables,
bool views,
bool userDefinedFunctions,
bool synonyms,
Func<string, bool> 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<string, bool> 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<NamedSmoObject, bool> IncludeItem { get; init; } = (_) => true;
}
4 changes: 2 additions & 2 deletions src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;

class SqlScriptBuilder(SchemaSettings settings)
Expand Down Expand Up @@ -122,7 +122,7 @@ void AppendType<T>(StringBuilder stringBuilder, ScriptingOptions options, SmoCol
where T : NamedSmoObject, IScriptable
{
var filtered = items.Cast<T>()
.Where(_ => !isSystem(_) && settings.IncludeItem(_.Name))
.Where(_ => !isSystem(_) && settings.IncludeItem(_))
.ToList();
if (filtered.Count == 0)
{
Expand Down
28 changes: 19 additions & 9 deletions src/Verify.SqlServer/SchemaValidation/VerifySettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace VerifyTests;
namespace VerifyTests;

public static class VerifySettingsSqlExtensions
{
Expand Down Expand Up @@ -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<string, object> context)
Expand All @@ -53,5 +63,5 @@ internal static SchemaSettings GetSchemaSettings(this IReadOnlyDictionary<string
return defaultSettings;
}

static SchemaSettings defaultSettings = new(true, true, true, true, true, _ => true);
static SchemaSettings defaultSettings = new();
}

0 comments on commit a68eb33

Please sign in to comment.