Skip to content

Commit

Permalink
EF Core: Make it possible to define the db schema for the audit table
Browse files Browse the repository at this point in the history
  • Loading branch information
ownerer authored and phatboyg committed May 22, 2023
1 parent 5be3652 commit 67773f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ namespace MassTransit

public static class EntityFrameworkAuditStoreConfiguratorExtensions
{
/// <summary>
/// Configure Audit Store against Entity Framework Core
/// </summary>
/// <param name="configurator"></param>
/// <param name="dbContextOptions">The DB Context Options configuration builder</param>
/// <param name="auditTableName">Name of the table to store audit records.</param>
/// <param name="auditTableSchema">Name of the audit table's db schema</param>
/// <param name="configureFilter">Configure messages to exclude or include from auditing.</param>
public static void UseEntityFrameworkCoreAuditStore(this IBusFactoryConfigurator configurator, DbContextOptionsBuilder dbContextOptions,
string auditTableName, string auditTableSchema,
Action<IMessageFilterConfigurator> configureFilter)
{
ConfigureAuditStore(configurator, dbContextOptions, auditTableName, auditTableSchema, configureFilter);
}

/// <summary>
/// Configure Audit Store against Entity Framework Core
/// </summary>
Expand All @@ -18,7 +33,7 @@ public static void UseEntityFrameworkCoreAuditStore(this IBusFactoryConfigurator
string auditTableName,
Action<IMessageFilterConfigurator> configureFilter)
{
ConfigureAuditStore(configurator, dbContextOptions, auditTableName, configureFilter);
ConfigureAuditStore(configurator, dbContextOptions, auditTableName, configureFilter: configureFilter);
}

/// <summary>
Expand All @@ -27,16 +42,17 @@ public static void UseEntityFrameworkCoreAuditStore(this IBusFactoryConfigurator
/// <param name="configurator"></param>
/// <param name="dbContextOptions">The DB Context Options configuration builder</param>
/// <param name="auditTableName">Name of the table to store audit records.</param>
/// <param name="auditTableSchema">Name of the audit table's db schema</param>
public static void UseEntityFrameworkCoreAuditStore(this IBusFactoryConfigurator configurator, DbContextOptionsBuilder dbContextOptions,
string auditTableName)
string auditTableName, string auditTableSchema = null)
{
ConfigureAuditStore(configurator, dbContextOptions, auditTableName);
ConfigureAuditStore(configurator, dbContextOptions, auditTableName, auditTableSchema);
}

static void ConfigureAuditStore(IBusFactoryConfigurator configurator, DbContextOptionsBuilder dbContextOptions, string auditTableName,
Action<IMessageFilterConfigurator> configureFilter = default)
string auditTableSchema = null, Action<IMessageFilterConfigurator> configureFilter = default)
{
var auditStore = new EntityFrameworkAuditStore(dbContextOptions.Options, auditTableName);
var auditStore = new EntityFrameworkAuditStore(dbContextOptions.Options, auditTableName, auditTableSchema);

configurator.ConnectSendAuditObservers(auditStore, configureFilter);
configurator.ConnectConsumeAuditObserver(auditStore, configureFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ public class AuditDbContext :
DbContext
{
readonly string _auditTableName;
readonly string _auditTableSchema;

protected AuditDbContext(string auditTableName)
protected AuditDbContext(string auditTableName, string auditTableSchema = null)
{
_auditTableName = auditTableName;
_auditTableSchema = auditTableSchema;
}

public AuditDbContext(DbContextOptions options, string auditTableName)
public AuditDbContext(DbContextOptions options, string auditTableName, string auditTableSchema = null)
: base(options)
{
_auditTableName = auditTableName;
_auditTableSchema = auditTableSchema;
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AuditMapping(_auditTableName));
modelBuilder.ApplyConfiguration(new AuditMapping(_auditTableName, _auditTableSchema));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ public class AuditMapping :
IEntityTypeConfiguration<AuditRecord>
{
readonly string _tableName;
readonly string _schemaName;

public AuditMapping(string tableName)
public AuditMapping(string tableName, string schemaName = null)
{
_tableName = tableName;
_schemaName = schemaName;
}

public void Configure(EntityTypeBuilder<AuditRecord> builder)
{
builder.ToTable(_tableName);
if (string.IsNullOrWhiteSpace(_schemaName))
{
builder.ToTable(_tableName);
}
else
{
builder.ToTable(_tableName, _schemaName);
}

builder.HasKey(x => x.AuditRecordId);
builder.Property(x => x.AuditRecordId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ public class EntityFrameworkAuditStore :
IMessageAuditStore
{
readonly string _auditTableName;
readonly string _auditTableSchema;
readonly DbContextOptions _contextOptions;

public EntityFrameworkAuditStore(DbContextOptions contextOptions, string auditTableName)
public EntityFrameworkAuditStore(DbContextOptions contextOptions, string auditTableName, string auditTableSchema = null)
{
_contextOptions = contextOptions;
_auditTableName = auditTableName;
_auditTableSchema = auditTableSchema;
}

public DbContext AuditContext => new AuditDbContext(_contextOptions, _auditTableName);
public DbContext AuditContext => new AuditDbContext(_contextOptions, _auditTableName, _auditTableSchema);

async Task IMessageAuditStore.StoreMessage<T>(T message, MessageAuditMetadata metadata)
{
Expand Down

0 comments on commit 67773f0

Please sign in to comment.