fluentmigrator icon indicating copy to clipboard operation
fluentmigrator copied to clipboard

Centralize creation of audit tables

Open BrunoWouters opened this issue 9 years ago • 1 comments

What is the best way to centralize the creation of audit tables? For example, an audit table has a primary key Id column, audit columns and all the columns of the original table but nullable.

I tried making an extension method on ICreateTableWithColumnSyntax. After casting it to CreateTableExpressionBuilder I could access the columns, table name, schema etc. but the creation of the audit table started to become complex making me wonder if there is a better way to solve this.

Without extension method:

Create.Table("Person").InSchema("mySchema")
    .WithColumn("PersonId").AsGuid().PrimaryKey()
    .WithColumn("FirstName").AsString(50).NotNullable()
    .WithColumn("LastName").AsString(50).NotNullable()
    .WithColumn("Email").AsString(100).Nullable();

Create.Table("PersonAudit").InSchema("mySchema")
    .WithColumn("Id").AsGuid().PrimaryKey()

    .WithColumn("PersonId").AsGuid().Nullable()
    .WithColumn("FirstName").AsString(50).Nullable()
    .WithColumn("LastName").AsString(50).Nullable()
    .WithColumn("Email").AsString(100).Nullable()

    .WithColumn("ChangedBy").AsString(100).NotNullable()
    .WithColumn("ChangedOn").AsDateTime().NotNullable()
    .WithColumn("EntityState").AsInt32().NotNullable();

With extension method:

Create.Table("Person").InSchema("mySchema")
    .WithColumn("PersonId").AsGuid().PrimaryKey()
    .WithColumn("FirstName").AsString(50).NotNullable()
    .WithColumn("LastName").AsString(50).NotNullable()
    .WithColumn("Email").AsString(100).Nullable()
    .WithAuditTable();

Thanks!

BrunoWouters avatar Apr 27 '16 12:04 BrunoWouters

This sounds like an interesting idea. A PR would be welcome 👍

fubar-coder avatar Mar 31 '18 22:03 fubar-coder