Skip to content

Commit

Permalink
Fixed MassTransit#5137 - Updated SqlTransportOptions so that it allow…
Browse files Browse the repository at this point in the history
…s for additional connection string parameters
  • Loading branch information
phatboyg committed Apr 26, 2024
1 parent c287ee3 commit be70dec
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public class SqlTransportOptions

public string? AdminUsername { get; set; }
public string? AdminPassword { get; set; }

/// <summary>
/// Optional, if specified, will be parsed to capture additional properties on the connection.
/// </summary>
public string? ConnectionString { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@ public static void UsingPostgres(this IBusRegistrationConfigurator configurator,
configure?.Invoke(context, cfg);
}));
}

/// <summary>
/// Configure the bus to use the PostgreSQL database transport
/// </summary>
/// <param name="configurator">The registration configurator (configured via AddMassTransit)</param>
/// <param name="connectionString">
/// Connection string to be used/parsed by the transport. <see cref="SqlTransportOptions" /> are not
/// used with this overload
/// </param>
/// <param name="configure">The configuration callback for the bus factory</param>
public static void UsingPostgres(this IBusRegistrationConfigurator configurator, string connectionString,
Action<IBusRegistrationContext, ISqlBusFactoryConfigurator>? configure = null)
{
configurator.SetBusFactory(new SqlRegistrationBusFactory((context, cfg) =>
{
cfg.UsePostgres(connectionString);

configure?.Invoke(context, cfg);
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public PostgresSqlHostSettings(string connectionString)

public PostgresSqlHostSettings(SqlTransportOptions options)
{
Host = options.Host;
Database = options.Database;
Username = options.Username;
Password = options.Password;
Schema = options.Schema;
var builder = PostgresSqlTransportConnection.CreateBuilder(options);

if (options.Port.HasValue)
Port = options.Port.Value;
}
Host = builder.Host;
if (builder.Port > 0 && builder.Port != NpgsqlConnection.DefaultPort)
Port = options.Port;

public string? Role { get; set; }
Database = builder.Database;
Schema = options.Schema;

public string? AdminUsername { get; set; }
public string? AdminPassword { get; set; }
Username = builder.Username;
Password = builder.Password;

_builder = builder;
}

public string? ConnectionString
{
Expand All @@ -44,14 +44,14 @@ public string? ConnectionString
var builder = new NpgsqlConnectionStringBuilder(value);

Host = builder.Host;
if (builder.Port > 0)
if (builder.Port > 0 && builder.Port != NpgsqlConnection.DefaultPort)
Port = builder.Port;

Database = builder.Database;

Username = builder.Username;
Password = builder.Password;

Database = builder.Database;

_builder = builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,51 @@ public Task Close()

public static PostgresSqlTransportConnection GetSystemDatabaseConnection(SqlTransportOptions options)
{
var builder = new NpgsqlConnectionStringBuilder
{
Host = options.Host,
Database = "postgres",
Username = options.AdminUsername ?? options.Username,
Password = options.AdminPassword ?? options.Password
};
var builder = CreateBuilder(options);

if (options.Port.HasValue)
builder.Port = options.Port.Value;
builder.Database = "postgres";

if (!string.IsNullOrWhiteSpace(options.AdminUsername))
builder.Username = options.AdminUsername;
if (!string.IsNullOrWhiteSpace(options.AdminPassword))
builder.Password = options.AdminPassword;

return new PostgresSqlTransportConnection(builder.ToString());
}

public static PostgresSqlTransportConnection GetDatabaseAdminConnection(SqlTransportOptions options)
{
var builder = new NpgsqlConnectionStringBuilder
{
Host = options.Host,
Database = options.Database,
Username = options.AdminUsername ?? options.Username,
Password = options.AdminPassword ?? options.Password
};
var builder = CreateBuilder(options);

if (options.Port.HasValue)
builder.Port = options.Port.Value;
if (!string.IsNullOrWhiteSpace(options.AdminUsername))
builder.Username = options.AdminUsername;
if (!string.IsNullOrWhiteSpace(options.AdminPassword))
builder.Password = options.AdminPassword;

return new PostgresSqlTransportConnection(builder.ToString());
}

public static PostgresSqlTransportConnection GetDatabaseConnection(SqlTransportOptions options)
{
var builder = new NpgsqlConnectionStringBuilder
{
Host = options.Host,
Database = options.Database,
Username = options.Username,
Password = options.Password
};
return new PostgresSqlTransportConnection(CreateBuilder(options).ToString());
}

public static NpgsqlConnectionStringBuilder CreateBuilder(SqlTransportOptions options)
{
var builder = new NpgsqlConnectionStringBuilder(options.ConnectionString);

if (!string.IsNullOrWhiteSpace(options.Host))
builder.Host = options.Host;
if (!string.IsNullOrWhiteSpace(options.Database))
builder.Database = options.Database;
if (!string.IsNullOrWhiteSpace(options.Username))
builder.Username = options.Username;
if (!string.IsNullOrWhiteSpace(options.Password))
builder.Password = options.Password;
if (options.Port.HasValue)
builder.Port = options.Port.Value;

return new PostgresSqlTransportConnection(builder.ToString());
return builder;
}

public static string? GetAdminMigrationPrincipal(SqlTransportOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@ public static void UsingSqlServer(this IBusRegistrationConfigurator configurator
configure?.Invoke(context, cfg);
}));
}

/// <summary>
/// Configure the bus to use the PostgreSQL database transport
/// </summary>
/// <param name="configurator">The registration configurator (configured via AddMassTransit)</param>
/// <param name="connectionString">
/// Connection string to be used/parsed by the transport. <see cref="SqlTransportOptions" /> are not
/// used with this overload
/// </param>
/// <param name="configure">The configuration callback for the bus factory</param>
public static void UsingSqlServer(this IBusRegistrationConfigurator configurator, string connectionString,
Action<IBusRegistrationContext, ISqlBusFactoryConfigurator>? configure = null)
{
configurator.SetBusFactory(new SqlRegistrationBusFactory((context, cfg) =>
{
cfg.UseSqlServer(connectionString);

configure?.Invoke(context, cfg);
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MassTransit
public static class SqlServerHostConfigurationExtensions
{
/// <summary>
/// Configures the database transport to use PostgreSQL as the storage engine
/// Configures the database transport to use SQL Server as the storage engine
/// </summary>
/// <param name="configurator"></param>
/// <param name="hostAddress">The MassTransit host address of the database</param>
Expand All @@ -24,18 +24,34 @@ public static void UseSqlServer(this ISqlBusFactoryConfigurator configurator, Ur
}

/// <summary>
/// Configures the database transport to use PostgreSQL as the storage engine
/// Configures the database transport to use SQL Server as the storage engine
/// </summary>
/// <param name="configurator"></param>
/// <param name="context">The bus registration context, used to retrieve the DbTransportOptions</param>
/// <param name="configure"></param>
public static void UseSqlServer(this ISqlBusFactoryConfigurator configurator, IBusRegistrationContext context, Action<ISqlHostConfigurator>? configure = null)
public static void UseSqlServer(this ISqlBusFactoryConfigurator configurator, IBusRegistrationContext context,
Action<ISqlHostConfigurator>? configure = null)
{
var hostConfigurator = new SqlServerSqlHostConfigurator(context.GetRequiredService<IOptions<SqlTransportOptions>>().Value);

configure?.Invoke(hostConfigurator);

configurator.Host(hostConfigurator.Settings);
}

/// <summary>
/// Configures the database transport to use SQL Server as the storage engine
/// </summary>
/// <param name="configurator"></param>
/// <param name="connectionString">A valid SQL Server connection string</param>
/// <param name="configure"></param>
public static void UseSqlServer(this ISqlBusFactoryConfigurator configurator, string connectionString, Action<ISqlHostConfigurator>? configure = null)
{
var hostConfigurator = new SqlServerSqlHostConfigurator(connectionString);

configure?.Invoke(hostConfigurator);

configurator.Host(hostConfigurator.Settings);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public SqlServerSqlHostSettings(string connectionString)

public SqlServerSqlHostSettings(SqlTransportOptions options)
{
ParseHost(options.Host);
var builder = SqlServerSqlTransportConnection.CreateBuilder(options);

Database = options.Database;
Username = options.Username;
Password = options.Password;
ParseDataSource(builder.DataSource);

Database = builder.InitialCatalog;
Schema = options.Schema;

if (options.Port.HasValue)
Port = options.Port.Value;
Username = builder.UserID;
Password = builder.Password;

_builder = builder;
}

public string? ConnectionString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,50 @@ public SqlCommand CreateCommand(string commandText)

public static SqlServerSqlTransportConnection GetSystemDatabaseConnection(SqlTransportOptions options)
{
var builder = new SqlConnectionStringBuilder
{
DataSource = options.FormatDataSource(),
InitialCatalog = "master",
UserID = options.AdminUsername ?? options.Username,
Password = options.AdminPassword ?? options.Password,
TrustServerCertificate = true
};
var builder = CreateBuilder(options);

builder.InitialCatalog = "master";

if (!string.IsNullOrWhiteSpace(options.AdminUsername))
builder.UserID = options.AdminUsername;
if (!string.IsNullOrWhiteSpace(options.AdminPassword))
builder.Password = options.AdminPassword;

return new SqlServerSqlTransportConnection(builder.ToString());
}

public static SqlServerSqlTransportConnection GetDatabaseAdminConnection(SqlTransportOptions options)
{
var builder = new SqlConnectionStringBuilder
{
DataSource = options.FormatDataSource(),
InitialCatalog = options.Database,
UserID = options.AdminUsername ?? options.Username,
Password = options.AdminPassword ?? options.Password,
TrustServerCertificate = true
};
var builder = CreateBuilder(options);

if (!string.IsNullOrWhiteSpace(options.AdminUsername))
builder.UserID = options.AdminUsername;
if (!string.IsNullOrWhiteSpace(options.AdminPassword))
builder.Password = options.AdminPassword;

return new SqlServerSqlTransportConnection(builder.ToString());
}

public static SqlServerSqlTransportConnection GetDatabaseConnection(SqlTransportOptions options)
{
var builder = new SqlConnectionStringBuilder
{
DataSource = options.FormatDataSource(),
InitialCatalog = options.Database,
UserID = options.Username,
Password = options.Password,
TrustServerCertificate = true
};
var builder = CreateBuilder(options);

return new SqlServerSqlTransportConnection(builder.ToString());
}

public static SqlConnectionStringBuilder CreateBuilder(SqlTransportOptions options)
{
var builder = new SqlConnectionStringBuilder(options.ConnectionString) { TrustServerCertificate = true };

if (!string.IsNullOrWhiteSpace(options.Host))
builder.DataSource = options.FormatDataSource();
if (!string.IsNullOrWhiteSpace(options.Database))
builder.InitialCatalog = options.Database;
if (!string.IsNullOrWhiteSpace(options.Username))
builder.UserID = options.Username;
if (!string.IsNullOrWhiteSpace(options.Password))
builder.Password = options.Password;

return builder;
}
}

0 comments on commit be70dec

Please sign in to comment.