Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #88 from enriquein/postgres-auth-client-certificate
Browse files Browse the repository at this point in the history
Added configuration overload to provide custom Npgsql connections.
  • Loading branch information
mookid8000 authored Jun 15, 2017
2 parents 7d47774 + 8a5c2cb commit 2961e5d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using d60.Cirqus.Config.Configurers;
using d60.Cirqus.Events;
using d60.Cirqus.PostgreSql.Events;
using Npgsql;

namespace d60.Cirqus.PostgreSql.Config
{
Expand All @@ -10,13 +11,13 @@ public static class PostgreSqlConfigurationExtensions
/// <summary>
/// Configures Cirqus to use Postgres as the event store
/// </summary>
public static void UsePostgreSql(this EventStoreConfigurationBuilder builder, string connectionStringOrConnectionStringName, string tableName, bool automaticallyCreateSchema = true)
public static void UsePostgreSql(this EventStoreConfigurationBuilder builder, string connectionStringOrConnectionStringName, string tableName, bool automaticallyCreateSchema = true, Action<NpgsqlConnection> additionalConnectionSetup = null)
{
if (builder == null) throw new ArgumentNullException("builder");
if (connectionStringOrConnectionStringName == null) throw new ArgumentNullException("connectionStringOrConnectionStringName");
if (tableName == null) throw new ArgumentNullException("tableName");

builder.Register<IEventStore>(context => new PostgreSqlEventStore(connectionStringOrConnectionStringName, tableName, automaticallyCreateSchema: automaticallyCreateSchema));
builder.Register<IEventStore>(context => new PostgreSqlEventStore(connectionStringOrConnectionStringName, tableName, automaticallyCreateSchema: automaticallyCreateSchema, additionalConnectionSetup: additionalConnectionSetup));
}
}
}
9 changes: 7 additions & 2 deletions d60.Cirqus.PostgreSql/Events/PostgreSqlEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ namespace d60.Cirqus.PostgreSql.Events
{
public class PostgreSqlEventStore : IEventStore
{
readonly Action<NpgsqlConnection> _additionalConnectionSetup;
readonly string _connectionString;
readonly string _tableName;
readonly MetadataSerializer _metadataSerializer = new MetadataSerializer();

public PostgreSqlEventStore(string connectionStringOrConnectionStringName, string tableName, bool automaticallyCreateSchema = true)
public PostgreSqlEventStore(string connectionStringOrConnectionStringName, string tableName, bool automaticallyCreateSchema = true, Action<NpgsqlConnection> additionalConnectionSetup = null)
{
_tableName = tableName;
_connectionString = SqlHelper.GetConnectionString(connectionStringOrConnectionStringName);
_additionalConnectionSetup = additionalConnectionSetup;

if (automaticallyCreateSchema)
{
Expand Down Expand Up @@ -159,16 +161,19 @@ long GetNextGlobalSequenceNumber(NpgsqlConnection conn, NpgsqlTransaction tx)
}
}


NpgsqlConnection GetConnection()
{
var connection = new NpgsqlConnection(_connectionString);

if (_additionalConnectionSetup != null)
_additionalConnectionSetup.Invoke(connection);

connection.Open();

return connection;
}


public IEnumerable<EventData> Load(string aggregateRootId, long firstSeq = 0)
{
using (var connection = GetConnection())
Expand Down
10 changes: 9 additions & 1 deletion d60.Cirqus.PostgreSql/Views/PostgreSqlViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace d60.Cirqus.PostgreSql.Views
public class PostgreSqlViewManager<TViewInstance> : AbstractViewManager<TViewInstance> where TViewInstance : class, IViewInstance, ISubscribeTo, new()
{
readonly string _tableName;
private readonly Action<NpgsqlConnection> _additionalConnectionSetup;
readonly string _positionTableName;
const int PrimaryKeySize = 255;
const int DefaultPosition = -1;
Expand All @@ -27,9 +28,10 @@ namespace d60.Cirqus.PostgreSql.Views
readonly string _connectionString;
readonly GenericSerializer _serializer = new GenericSerializer();

public PostgreSqlViewManager(string connectionStringOrConnectionStringName, string tableName, string positionTableName = null, bool automaticallyCreateSchema = true)
public PostgreSqlViewManager(string connectionStringOrConnectionStringName, string tableName, string positionTableName = null, bool automaticallyCreateSchema = true, Action<NpgsqlConnection> additionalConnectionSetup = null)
{
_tableName = tableName;
_additionalConnectionSetup = additionalConnectionSetup;
_positionTableName = positionTableName ?? _tableName + "_Position";
_connectionString = SqlHelper.GetConnectionString(connectionStringOrConnectionStringName);

Expand Down Expand Up @@ -71,6 +73,9 @@ NpgsqlConnection GetConnection()
{
var connection = new NpgsqlConnection(_connectionString);

if (_additionalConnectionSetup != null)
_additionalConnectionSetup.Invoke(connection);

connection.Open();

return connection;
Expand All @@ -80,6 +85,9 @@ async Task<NpgsqlConnection> GetConnectionAsync()
{
var connection = new NpgsqlConnection(_connectionString);

if (_additionalConnectionSetup != null)
_additionalConnectionSetup.Invoke(connection);

await connection.OpenAsync();

return connection;
Expand Down

0 comments on commit 2961e5d

Please sign in to comment.