Add audit table.

This commit is contained in:
2020-06-09 22:23:17 +01:00
parent af07f790fd
commit c6941d1450
7 changed files with 4279 additions and 1 deletions

View File

@@ -494,4 +494,10 @@ namespace Marechai.Database
{
Normal = 0, Low = 1, High = 2
}
public enum AuditType : byte
{
None = 0, Created = 1, Updated = 2,
Deleted = 3
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,42 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Marechai.Database.Migrations
{
public partial class AddAuditTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable("Audit", table => new
{
Id = table.Column<long>(nullable: false).
Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
CreatedOn = table.Column<DateTime>(nullable: false).
Annotation("MySql:ValueGenerationStrategy",
MySqlValueGenerationStrategy.IdentityColumn),
UpdatedOn = table.Column<DateTime>(nullable: false).
Annotation("MySql:ValueGenerationStrategy",
MySqlValueGenerationStrategy.ComputedColumn),
Type = table.Column<byte>(nullable: false), UserId = table.Column<string>(nullable: false),
Table = table.Column<string>(nullable: true), Keys = table.Column<string>(nullable: true),
OldValues = table.Column<string>(nullable: true), NewValues = table.Column<string>(nullable: true),
AffectedColumns = table.Column<string>(nullable: true)
}, constraints: table =>
{
table.PrimaryKey("PK_Audit", x => x.Id);
table.ForeignKey("FK_Audit_AspNetUsers_UserId", x => x.UserId, "AspNetUsers", "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex("IX_Audit_Table", "Audit", "Table");
migrationBuilder.CreateIndex("IX_Audit_Type", "Audit", "Type");
migrationBuilder.CreateIndex("IX_Audit_UserId", "Audit", "UserId");
}
protected override void Down(MigrationBuilder migrationBuilder) => migrationBuilder.DropTable("Audit");
}
}

View File

@@ -82,6 +82,39 @@ namespace Marechai.Database.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Marechai.Database.Models.Audit", b =>
{
b.Property<long>("Id").ValueGeneratedOnAdd().HasColumnType("bigint");
b.Property<string>("AffectedColumns").HasColumnType("json");
b.Property<DateTime>("CreatedOn").ValueGeneratedOnAdd().HasColumnType("datetime(6)");
b.Property<string>("Keys").HasColumnType("json");
b.Property<string>("NewValues").HasColumnType("json");
b.Property<string>("OldValues").HasColumnType("json");
b.Property<string>("Table").HasColumnType("varchar(255) CHARACTER SET utf8mb4");
b.Property<byte>("Type").HasColumnType("tinyint unsigned");
b.Property<DateTime>("UpdatedOn").ValueGeneratedOnAddOrUpdate().HasColumnType("datetime(6)");
b.Property<string>("UserId").IsRequired().HasColumnType("varchar(255) CHARACTER SET utf8mb4");
b.HasKey("Id");
b.HasIndex("Table");
b.HasIndex("Type");
b.HasIndex("UserId");
b.ToTable("Audit");
});
modelBuilder.Entity("Marechai.Database.Models.Book", b =>
{
b.Property<long>("Id").ValueGeneratedOnAdd().HasColumnType("bigint");
@@ -2291,6 +2324,12 @@ namespace Marechai.Database.Migrations
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("Marechai.Database.Models.Audit", b =>
{
b.HasOne("Marechai.Database.Models.ApplicationUser", "User").WithMany().HasForeignKey("UserId").
OnDelete(DeleteBehavior.Cascade).IsRequired();
});
modelBuilder.Entity("Marechai.Database.Models.Book", b =>
{
b.HasOne("Marechai.Database.Models.Iso31661Numeric", "Country").WithMany("Books").

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Marechai.Database.Models
{
public class Audit : BaseModel<long>
{
public AuditType Type { get; set; }
[Required]
public string UserId { get; set; }
public string Table { get; set; }
public JsonObject<Dictionary<string, object>> Keys { get; set; }
public JsonObject<Dictionary<string, object>> OldValues { get; set; }
public JsonObject<Dictionary<string, object>> NewValues { get; set; }
public JsonObject<List<string>> AffectedColumns { get; set; }
[Required]
public virtual ApplicationUser User { get; set; }
}
}

View File

@@ -93,6 +93,8 @@ namespace Marechai.Database.Models
public virtual DbSet<StorageByMachine> StorageByMachine { get; set; }
public virtual DbSet<StorageByOwnedMachine> StorageByOwnedMachine { get; set; }
public virtual DbSet<Audit> Audit { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if(optionsBuilder.IsConfigured)
@@ -1463,6 +1465,13 @@ namespace Marechai.Database.Models
entity.HasIndex(e => e.FsfApproved);
entity.HasIndex(e => e.OsiApproved);
});
modelBuilder.Entity<Audit>(entity =>
{
entity.HasIndex(d => d.UserId);
entity.HasIndex(d => d.Table);
entity.HasIndex(d => d.Type);
});
}
}
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>4.0.0.1585</Version>
<Version>4.0.0.1586</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>