Skip to content

Commit

Permalink
Refacts around storage selections. Using DbAgnistoc in suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Stenbom committed Nov 22, 2024
1 parent 0603968 commit 0ae630d
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
<PropertyGroup>
<RootNamespace>Hangfire.Configuration.Test</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10</LangVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DbAgnostic" Version="3.0.25" />
<PackageReference Include="HangFire.Autofac" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
Expand Down
15 changes: 4 additions & 11 deletions Hangfire.Configuration.Test/Infrastructure/DatabaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using DbAgnostic;
using NUnit.Framework;

namespace Hangfire.Configuration.Test.Infrastructure;
Expand All @@ -9,25 +10,17 @@ namespace Hangfire.Configuration.Test.Infrastructure;
public abstract class DatabaseTest
{
protected readonly string ConnectionString;
private readonly bool _isSqlServer;

protected DatabaseTest(string connectionString)
{
ConnectionString = connectionString;
_isSqlServer = ConnectionString.Equals(ConnectionStrings.SqlServer);
}

[SetUp]
public void Setup()
{
if(_isSqlServer)
DatabaseTestSetup.SetupSqlServer(ConnectionString);
else
DatabaseTestSetup.SetupPostgres(ConnectionString);
}

protected T SelectDialect<T>(Func<T> sqlServer, Func<T> postgres)
{
return _isSqlServer ? sqlServer() : postgres();
ConnectionString.PickAction(
() => DatabaseTestSetup.SetupSqlServer(ConnectionString),
() => DatabaseTestSetup.SetupPostgres(ConnectionString));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using Npgsql;
using DbAgnostic;
using NUnit.Framework;

namespace Hangfire.Configuration.Test.Infrastructure
Expand All @@ -16,16 +14,16 @@ public HangfireConfigurationSchemaInstallerTest(string connectionString) : base(
[Test]
public void ShouldUpgradeFrom0ToLatest()
{
using var c = SelectDialect<DbConnection>(() => new SqlConnection(ConnectionString), () => new NpgsqlConnection(ConnectionString));
using var c = ConnectionString.CreateConnection();
HangfireConfigurationSchemaInstaller.Install(c);

Assert.AreEqual(HangfireConfigurationSchemaInstaller.SchemaVersion, version());
}

private int version()
{
var schemaName = SelectDialect(() => "[Schema]", () => "schema");
using var c = SelectDialect<DbConnection>(() => new SqlConnection(ConnectionString), () => new NpgsqlConnection(ConnectionString));
var schemaName = ConnectionString.PickDialect("[Schema]", "schema");
using var c = ConnectionString.CreateConnection();
return c.Query<int>($"SELECT Version FROM HangfireConfiguration.{schemaName}").Single();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data.Common;
using System.Data.SqlClient;
using Dapper;
using DbAgnostic;
using Npgsql;
using NUnit.Framework;
using SharpTestsEx;
Expand All @@ -25,9 +26,7 @@ public void ShouldConnect()
public void ShouldThrowExceptionWhenNoDatabase()
{
var creator = new SchemaInstaller();
var connectionString = SelectDialect(
() => new SqlConnectionStringBuilder(ConnectionString) {InitialCatalog = "Does_Not_Exist"}.ToString(),
() => new NpgsqlConnectionStringBuilder(ConnectionString) {Database = "Does_Not_Exist"}.ToString());
var connectionString = ConnectionString.ChangeDatabase("Does_Not_Exist");

var exception = Assert.Catch(() => creator.TryConnect(connectionString));

Expand All @@ -41,7 +40,7 @@ public void ShouldCreateSchema()

creator.InstallHangfireStorageSchema("hangfiretestschema", ConnectionString);

using var conn = SelectDialect<DbConnection>(() => new SqlConnection(ConnectionString), () => new NpgsqlConnection(ConnectionString));
using var conn = ConnectionString.CreateConnection();
Assert.AreEqual("hangfiretestschema", conn.ExecuteScalar<string>("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'hangfiretestschema'"));
}

Expand All @@ -52,8 +51,8 @@ public void ShouldCreateSchemaWithDefaultSchemaName()

creator.InstallHangfireStorageSchema("", ConnectionString);

var expected = SelectDialect(() => "HangFire", () => "hangfire");
using var conn = SelectDialect<DbConnection>(() => new SqlConnection(ConnectionString), () => new NpgsqlConnection(ConnectionString));
var expected = ConnectionString.PickDialect("HangFire", "hangfire");
using var conn = ConnectionString.CreateConnection();
Assert.AreEqual(expected, conn.ExecuteScalar<string>($"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{expected}'"));
}

Expand Down Expand Up @@ -86,7 +85,7 @@ public void ShouldInstallConfigurationSchema()
creator.InstallHangfireConfigurationSchema(ConnectionString);

var expected = "hangfireconfiguration";
using var conn = SelectDialect<DbConnection>(() => new SqlConnection(ConnectionString), () => new NpgsqlConnection(ConnectionString));
using var conn = ConnectionString.CreateConnection();
var actual = conn.ExecuteScalar<string>($"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{expected}'");
Assert.AreEqual(expected.ToLower(), actual.ToLower());
}
Expand Down
4 changes: 2 additions & 2 deletions Hangfire.Configuration/ConfigurationStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Transaction(Action action)
private string tableName()
{
var c = currentConnector();
return c.SelectDialect(
return c.PickDialect(
$@"[{HangfireConfigurationSchemaInstaller.SchemaName}].Configuration",
$@"{HangfireConfigurationSchemaInstaller.SchemaName}.Configuration");
}
Expand All @@ -56,7 +56,7 @@ private string tableName()
public void LockConfiguration()
{
var c = currentConnector();
var sql = c.SelectDialect(
var sql = c.PickDialect(
$@"SELECT * FROM {tableName()} WITH (TABLOCKX)",
$@"LOCK TABLE {tableName()}");
c.Execute(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Install(DbConnection connection, int schemaVersion)
{
if (connection == null)
throw new ArgumentNullException(nameof(connection));
var dbScript = connection.ToDbVendorSelector().SelectDialect(
var dbScript = connection.ToDbSelector().PickFunc(
() => sqlServerScript,
() => postgreSqlScript);
var scriptWithSchema = dbScript
Expand Down
2 changes: 1 addition & 1 deletion Hangfire.Configuration/Internals/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class Connector : ConnectorBase
{
protected override void operation(Action<IDbConnection, IDbTransaction> action)
{
this.ExecuteDialect(() =>
this.PickAction(() =>
{
using var connection = ConnectionString.CreateConnection();
OpenWithRetry(connection);
Expand Down
6 changes: 3 additions & 3 deletions Hangfire.Configuration/Internals/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hangfire.Configuration.Internals;

internal abstract class ConnectorBase : IDbVendorSelector
internal abstract class ConnectorBase : IDbSelector
{
protected abstract void operation(Action<IDbConnection, IDbTransaction> action);

Expand Down Expand Up @@ -51,8 +51,8 @@ protected static void OpenWithRetry(IDbConnection connection)
_connectionRetry.Execute(connection.Open);
}

public T SelectDialect<T>(Func<T> sqlServer, Func<T> postgres, Func<T> redis = null)
public T PickFunc<T>(Func<T> sqlServer, Func<T> postgres, Func<T> redis = null)
{
return ConnectionString.ToDbVendorSelector().SelectDialect(sqlServer, postgres);
return ConnectionString.ToDbSelector().PickFunc(sqlServer, postgres);
}
}
35 changes: 17 additions & 18 deletions Hangfire.Configuration/Internals/DbVendorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace Hangfire.Configuration.Internals;

internal static class DbVendorExtensions
{
public static IDbVendorSelector ToDbVendorSelector(this string connectionString) =>
public static IDbSelector ToDbSelector(this string connectionString) =>
new connectionStringDialectSelector(connectionString);

public static IDbVendorSelector ToDbVendorSelector(this DbConnection dbConnection) =>
ToDbVendorSelector(dbConnection.ConnectionString);
public static IDbSelector ToDbSelector(this DbConnection dbConnection) =>
ToDbSelector(dbConnection.ConnectionString);

public static void ExecuteDialect(this IDbVendorSelector selector, Action sqlServer, Action postgres, Action redis = null)
public static void PickAction(this IDbSelector selector, Action sqlServer, Action postgres, Action redis = null)
{
selector.SelectDialect(() =>
selector.PickFunc(() =>
{
sqlServer();
return true;
Expand All @@ -30,27 +30,26 @@ public static void ExecuteDialect(this IDbVendorSelector selector, Action sqlSer
});
}

public static T SelectDialect<T>(this IDbVendorSelector selector, Func<T> sqlServer, Func<T> postgres) =>
selector.SelectDialect(sqlServer, postgres, () => default);
public static T PickFunc<T>(this IDbSelector selector, Func<T> sqlServer, Func<T> postgres) =>
selector.PickFunc(sqlServer, postgres, () => default);

public static T SelectDialect<T>(this IDbVendorSelector selector, T sqlServer, T postgres, T redis = default) =>
selector.SelectDialect(() => sqlServer, () => postgres, () => redis);
public static T PickDialect<T>(this IDbSelector selector, T sqlServer, T postgres, T redis = default) =>
selector.PickFunc(() => sqlServer, () => postgres, () => redis);

public static string ApplicationName(this string connectionString) =>
connectionString.ToDbVendorSelector().SelectDialect(
connectionString.ToDbSelector().PickFunc(
() => new SqlConnectionStringBuilder(connectionString).ApplicationName,
() => new NpgsqlConnectionStringBuilder(connectionString).ApplicationName);

public static string ChangeApplicationName(this string connectionString, string applicationName) =>
connectionString.ToDbVendorSelector().SelectDialect(
connectionString.ToDbSelector().PickFunc(
() => new SqlConnectionStringBuilder(connectionString) {ApplicationName = applicationName}.ToString(),
() => new NpgsqlConnectionStringBuilder(connectionString) {ApplicationName = applicationName}.ToString(),
() => connectionString);



public static string SetUserNameAndPassword(this string connectionString, string userName, string password)
{
return new connectionStringDialectSelector(connectionString).SelectDialect(
return new connectionStringDialectSelector(connectionString).PickFunc(
() =>
{
var ret = new SqlConnectionStringBuilder(connectionString)
Expand All @@ -70,7 +69,7 @@ public static string SetUserNameAndPassword(this string connectionString, string

public static string SetCredentials(this string connectionString, bool useIntegratedSecurity, string userName, string password)
{
return new connectionStringDialectSelector(connectionString).SelectDialect(
return new connectionStringDialectSelector(connectionString).PickFunc(
() =>
{
if (useIntegratedSecurity)
Expand All @@ -88,13 +87,13 @@ public static string SetCredentials(this string connectionString, bool useIntegr

public static DbConnection CreateConnection(this string connectionString)
{
var connection = connectionString.ToDbVendorSelector().SelectDialect<DbConnection>(
var connection = connectionString.ToDbSelector().PickFunc<DbConnection>(
() => new SqlConnection(), () => new NpgsqlConnection());
connection.ConnectionString = connectionString;
return connection;
}

private class connectionStringDialectSelector : IDbVendorSelector
private class connectionStringDialectSelector : IDbSelector
{
private readonly string _connectionString;

Expand All @@ -103,7 +102,7 @@ public connectionStringDialectSelector(string connectionString)
_connectionString = connectionString;
}

public T SelectDialect<T>(Func<T> sqlServer, Func<T> postgres, Func<T> redis)
public T PickFunc<T>(Func<T> sqlServer, Func<T> postgres, Func<T> redis)
{
if (isSqlServer())
return sqlServer();
Expand Down
8 changes: 8 additions & 0 deletions Hangfire.Configuration/Internals/IDbSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Hangfire.Configuration.Internals;

internal interface IDbSelector
{
T PickFunc<T>(Func<T> sqlServer, Func<T> postgres, Func<T> redis);
}
8 changes: 0 additions & 8 deletions Hangfire.Configuration/Internals/IDbVendorSelector.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Hangfire.Configuration/Internals/WorkerServerUpgrader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Upgrade(UpgradeWorkerServers command)
.Where(x => !string.IsNullOrEmpty(x.ConnectionString))
.Where(x =>
{
var hasSchema = x.ConnectionString.ToDbVendorSelector().SelectDialect(true, true, false);
var hasSchema = x.ConnectionString.ToDbSelector().PickDialect(true, true, false);
return hasSchema;
})
.Select(x =>
Expand Down
6 changes: 3 additions & 3 deletions Hangfire.Configuration/KeyValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ internal KeyValueStore(Connector connector)

public void Write(string key, string value)
{
var updateSqlQuery = _connector.SelectDialect(
var updateSqlQuery = _connector.PickDialect(
$"UPDATE [{schema}].KeyValueStore SET [Value] = @Value WHERE [Key] = @Key",
$"UPDATE {schema}.KeyValueStore SET Value = @Value WHERE Key = @Key");
var insertSqlQuery = _connector.SelectDialect(
var insertSqlQuery = _connector.PickDialect(
$"INSERT INTO [{schema}].KeyValueStore ([Key], [Value]) VALUES (@Key, @Value)",
$"INSERT INTO {schema}.KeyValueStore (Key, Value) VALUES (@Key, @Value)");

Expand All @@ -35,7 +35,7 @@ public void Write(string key, string value)

public string Read(string key)
{
var sqlQuery = _connector.SelectDialect(
var sqlQuery = _connector.PickDialect(
$"SELECT [Value] FROM [{schema}].KeyValueStore WHERE [Key] = @Key",
$"SELECT Value FROM {schema}.KeyValueStore WHERE Key = @Key");
return _connector
Expand Down
4 changes: 2 additions & 2 deletions Hangfire.Configuration/Providers/StorageProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Hangfire.Configuration.Providers;
internal static class StorageProviderExtensions
{
public static IStorageProvider GetProvider(this string connectionString) =>
connectionString.ToDbVendorSelector()
.SelectDialect<IStorageProvider>(
connectionString.ToDbSelector()
.PickFunc<IStorageProvider>(
() => new SqlServerStorageProvider(),
() => new PostgresStorageProvider(),
#if Redis
Expand Down
2 changes: 1 addition & 1 deletion Hangfire.Configuration/SchemaInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void InstallHangfireConfigurationSchema(string connectionString)

public void InstallHangfireStorageSchema(string schemaName, string connectionString)
{
connectionString.ToDbVendorSelector().ExecuteDialect(
connectionString.ToDbSelector().PickAction(
() =>
{
using var conn = connectionString.CreateConnection();
Expand Down
2 changes: 1 addition & 1 deletion Hangfire.Configuration/ViewModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IEnumerable<ViewModel> BuildServerConfigurations()
private static string hidePassword(StoredConfiguration x)
{
const string hiddenPassword = "******";
return x.ConnectionString.ToDbVendorSelector().SelectDialect(
return x.ConnectionString.ToDbSelector().PickFunc(
() =>
{
var parsed = new SqlConnectionStringBuilder(x.ConnectionString);
Expand Down

0 comments on commit 0ae630d

Please sign in to comment.