Skip to content

Commit 3a5527f

Browse files
authored
Merge pull request #244 from frankhommers/features/240-fix-misleading-argument-name
Fixed argument name and error message regarding connection strings
2 parents 165b308 + fe20c8b commit 3a5527f

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

src/Hangfire.PostgreSql/PostgreSqlBootstrapperConfigurationExtensions.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,46 +32,46 @@ public static class PostgreSqlBootstrapperConfigurationExtensions
3232
/// its name.
3333
/// </summary>
3434
/// <param name="configuration">Configuration</param>
35-
/// <param name="nameOrConnectionString">Connection string or its name</param>
35+
/// <param name="connectionString">Connection string</param>
3636
public static IGlobalConfiguration<PostgreSqlStorage> UsePostgreSqlStorage(
3737
this IGlobalConfiguration configuration,
38-
string nameOrConnectionString)
38+
string connectionString)
3939
{
40-
return configuration.UsePostgreSqlStorage(nameOrConnectionString, null, new PostgreSqlStorageOptions());
40+
return configuration.UsePostgreSqlStorage(connectionString, null, new PostgreSqlStorageOptions());
4141
}
4242

4343
/// <summary>
4444
/// Tells the bootstrapper to use PostgreSQL as a job storage
4545
/// with the given options, that can be accessed using the specified
46-
/// connection string or its name.
46+
/// connection string.
4747
/// </summary>
4848
/// <param name="configuration">Configuration</param>
49-
/// <param name="nameOrConnectionString">Connection string or its name</param>
49+
/// <param name="connectionString">Connection string</param>
5050
/// <param name="options">Advanced options</param>
5151
public static IGlobalConfiguration<PostgreSqlStorage> UsePostgreSqlStorage(
5252
this IGlobalConfiguration configuration,
53-
string nameOrConnectionString,
53+
string connectionString,
5454
PostgreSqlStorageOptions options)
5555
{
56-
return configuration.UsePostgreSqlStorage(nameOrConnectionString, null, options);
56+
return configuration.UsePostgreSqlStorage(connectionString, null, options);
5757
}
5858

5959
/// <summary>
6060
/// Tells the bootstrapper to use PostgreSQL as a job storage
6161
/// with the given options, that can be accessed using the specified
62-
/// connection string or its name.
62+
/// connection string.
6363
/// </summary>
6464
/// <param name="configuration">Configuration</param>
65-
/// <param name="nameOrConnectionString">Connection string or its name</param>
65+
/// <param name="connectionString">Connection string</param>
6666
/// <param name="connectionSetup">Optional setup action to apply to created connections</param>
6767
/// <param name="options">Advanced options</param>
6868
public static IGlobalConfiguration<PostgreSqlStorage> UsePostgreSqlStorage(
6969
this IGlobalConfiguration configuration,
70-
string nameOrConnectionString,
70+
string connectionString,
7171
Action<NpgsqlConnection> connectionSetup,
7272
PostgreSqlStorageOptions options)
7373
{
74-
PostgreSqlStorage storage = new(nameOrConnectionString, connectionSetup, options);
74+
PostgreSqlStorage storage = new(connectionString, connectionSetup, options);
7575

7676
return configuration.UseStorage(storage);
7777
}

src/Hangfire.PostgreSql/PostgreSqlStorage.cs

+35-36
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,42 @@ namespace Hangfire.PostgreSql
3737
public class PostgreSqlStorage : JobStorage
3838
{
3939
private readonly Action<NpgsqlConnection> _connectionSetup;
40-
private readonly string _connectionString;
40+
private readonly NpgsqlConnectionStringBuilder _connectionStringBuilder;
4141
private readonly NpgsqlConnection _existingConnection;
4242

43-
public PostgreSqlStorage(string nameOrConnectionString)
44-
: this(nameOrConnectionString, new PostgreSqlStorageOptions()) { }
43+
public PostgreSqlStorage(string connectionString)
44+
: this(connectionString, new PostgreSqlStorageOptions()) { }
4545

46-
public PostgreSqlStorage(string nameOrConnectionString, PostgreSqlStorageOptions options)
47-
: this(nameOrConnectionString, null, options) { }
46+
public PostgreSqlStorage(string connectionString, PostgreSqlStorageOptions options)
47+
: this(connectionString, null, options) { }
4848

4949
/// <summary>
50-
/// Initializes PostgreSqlStorage from the provided PostgreSqlStorageOptions and either the provided connection
51-
/// string or the connection string with provided name pulled from the application config file.
50+
/// Initializes PostgreSqlStorage from the provided PostgreSqlStorageOptions and either the provided connection string.
5251
/// </summary>
53-
/// <param name="nameOrConnectionString">
54-
/// Either a SQL Server connection string or the name of
55-
/// a SQL Server connection string located in the connectionStrings node in the application config
56-
/// </param>
52+
/// <param name="connectionString">PostgreSQL connection string</param>
5753
/// <param name="connectionSetup">Optional setup action to apply to created connections</param>
58-
/// <param name="options"></param>
59-
/// <exception cref="ArgumentNullException"><paramref name="nameOrConnectionString" /> argument is null.</exception>
54+
/// <param name="options">Storage options</param>
55+
/// <exception cref="ArgumentNullException"><paramref name="connectionString" /> argument is null.</exception>
6056
/// <exception cref="ArgumentNullException"><paramref name="options" /> argument is null.</exception>
61-
/// <exception cref="ArgumentException">
62-
/// <paramref name="nameOrConnectionString" /> argument is neither
63-
/// a valid SQL Server connection string nor the name of a connection string in the application
64-
/// config file.
65-
/// </exception>
66-
public PostgreSqlStorage(string nameOrConnectionString,
57+
/// <exception cref="ArgumentException"><paramref name="connectionString" /> argument not a valid PostgreSQL connection string config file.</exception>
58+
public PostgreSqlStorage(
59+
string connectionString,
6760
Action<NpgsqlConnection> connectionSetup,
6861
PostgreSqlStorageOptions options)
6962
{
70-
if (nameOrConnectionString == null)
63+
if (connectionString == null)
7164
{
72-
throw new ArgumentNullException(nameof(nameOrConnectionString));
65+
throw new ArgumentNullException(nameof(connectionString));
7366
}
7467

7568
Options = options ?? throw new ArgumentNullException(nameof(options));
7669

77-
_connectionString = IsConnectionString(nameOrConnectionString)
78-
? nameOrConnectionString
79-
: throw new ArgumentException($"Could not find connection string with name '{nameOrConnectionString}' in application config file");
70+
if (!TryCreateConnectionStringBuilder(connectionString, out NpgsqlConnectionStringBuilder builder))
71+
{
72+
throw new ArgumentException($"Connection string [{connectionString}] is not valid", nameof(connectionString));
73+
}
8074

75+
_connectionStringBuilder = builder;
8176
_connectionSetup = connectionSetup;
8277

8378
if (options.PrepareSchemaIfNecessary)
@@ -176,13 +171,12 @@ public override string ToString()
176171

177172
try
178173
{
179-
NpgsqlConnectionStringBuilder connectionStringBuilder = new(_connectionString);
180174
StringBuilder builder = new();
181175

182176
builder.Append("Host: ");
183-
builder.Append(connectionStringBuilder.Host);
177+
builder.Append(_connectionStringBuilder.Host);
184178
builder.Append(", DB: ");
185-
builder.Append(connectionStringBuilder.Database);
179+
builder.Append(_connectionStringBuilder.Database);
186180
builder.Append(", Schema: ");
187181
builder.Append(Options.SchemaName);
188182

@@ -211,15 +205,11 @@ internal NpgsqlConnection CreateAndOpenConnection()
211205
NpgsqlConnection connection = _existingConnection;
212206
if (connection == null)
213207
{
214-
NpgsqlConnectionStringBuilder connectionStringBuilder = new(_connectionString);
215-
if (!Options.EnableTransactionScopeEnlistment)
216-
{
217-
connectionStringBuilder.Enlist = false;
218-
}
208+
_connectionStringBuilder.Enlist = Options.EnableTransactionScopeEnlistment;
219209

220-
SetTimezoneToUtcForNpgsqlCompatibility(connectionStringBuilder);
210+
SetTimezoneToUtcForNpgsqlCompatibility(_connectionStringBuilder);
221211

222-
connection = new NpgsqlConnection(connectionStringBuilder.ToString());
212+
connection = new NpgsqlConnection(_connectionStringBuilder.ToString());
223213
_connectionSetup?.Invoke(connection);
224214
}
225215

@@ -399,9 +389,18 @@ private void InitializeQueueProviders()
399389
QueueProviders = new PersistentJobQueueProviderCollection(defaultQueueProvider);
400390
}
401391

402-
private bool IsConnectionString(string nameOrConnectionString)
392+
private bool TryCreateConnectionStringBuilder(string connectionString, out NpgsqlConnectionStringBuilder builder)
403393
{
404-
return nameOrConnectionString.Contains(";");
394+
try
395+
{
396+
builder = new NpgsqlConnectionStringBuilder(connectionString);
397+
return true;
398+
}
399+
catch (ArgumentException)
400+
{
401+
builder = null;
402+
return false;
403+
}
405404
}
406405
}
407406
}

tests/Hangfire.PostgreSql.Tests/PostgreSqlStorageFacts.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ public void Connection_Timezone_Is_Set_To_UTC_For_Npgsql6_Compatibility()
3232
[Fact]
3333
public void Ctor_ThrowsAnException_WhenConnectionStringIsNull()
3434
{
35-
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(() => new PostgreSqlStorage(nameOrConnectionString: null));
35+
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(() => new PostgreSqlStorage(connectionString: null));
3636

37-
Assert.Equal("nameOrConnectionString", exception.ParamName);
37+
Assert.Equal("connectionString", exception.ParamName);
38+
}
39+
40+
[Fact]
41+
public void Ctor_ThrowsAnException_WhenConnectionStringIsInvalid()
42+
{
43+
ArgumentException exception = Assert.Throws<ArgumentException>(() => new PostgreSqlStorage("hello", new PostgreSqlStorageOptions()));
44+
45+
Assert.Equal("connectionString", exception.ParamName);
3846
}
3947

4048
[Fact]
@@ -90,8 +98,7 @@ public void GetComponents_ReturnsAllNeededComponents()
9098

9199
private PostgreSqlStorage CreateStorage()
92100
{
93-
return new PostgreSqlStorage(ConnectionUtils.GetConnectionString(),
94-
_options);
101+
return new PostgreSqlStorage(ConnectionUtils.GetConnectionString(), _options);
95102
}
96103
}
97104
}

0 commit comments

Comments
 (0)