Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create PropertyChanges for soft deleted entities. #9735

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,17 @@ public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
{
try
{
ApplyAbpConcepts();

var eventReport = CreateEventReport();

var auditLog = AuditingManager?.Current?.Log;
List<EntityChangeInfo> entityChangeList = null;
if (auditLog != null)
{
entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList());
}

ApplyAbpConcepts();

var eventReport = CreateEventReport();

var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);

PublishEntityEvents(eventReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected virtual List<EntityPropertyChangeInfo> GetPropertyChanges(EntityEntry
foreach (var property in properties)
{
var propertyEntry = entityEntry.Property(property.Name);
if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted))
if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted) && !IsSoftDeleted(entityEntry))
{
propertyChanges.Add(new EntityPropertyChangeInfo
{
Expand All @@ -188,11 +188,11 @@ protected virtual bool IsCreated(EntityEntry entityEntry)

protected virtual bool IsDeleted(EntityEntry entityEntry)
{
if (entityEntry.State == EntityState.Deleted)
{
return true;
}
return entityEntry.State == EntityState.Deleted || IsSoftDeleted(entityEntry);
}

protected virtual bool IsSoftDeleted(EntityEntry entityEntry)
{
var entity = entityEntry.Entity;
return entity is ISoftDelete && entity.As<ISoftDelete>().IsDeleted;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public override void ConfigureServices(ServiceConfigurationContext context)
"AppEntityWithSelector",
type => type == typeof(AppEntityWithSelector))
);

options.EntityHistorySelectors.Add(
new NamedTypeSelector(
"AppEntityWithSoftDelete",
type => type == typeof(AppEntityWithSoftDelete))
);
});

context.Services.AddType<Auditing_Tests.MyAuditedObject1>();
Expand All @@ -62,4 +68,4 @@ private static SqliteConnection CreateDatabaseAndGetConnection()
return connection;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Volo.Abp.Domain.Entities;

namespace Volo.Abp.Auditing.App.Entities
{
public class AppEntityWithSoftDelete : AggregateRoot<Guid>, IHasDeletionTime
{
public AppEntityWithSoftDelete(Guid id, string name)
: base(id)
{
Name = name;
}

public string Name { get; set; }

public bool IsDeleted { get; set; }

public DateTime? DeletionTime { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ public class AbpAuditingTestDbContext : AbpDbContext<AbpAuditingTestDbContext>
public DbSet<AppEntityWithPropertyHasAudited> AppEntityWithPropertyHasAudited { get; set; }

public DbSet<AppEntityWithSelector> AppEntityWithSelector { get; set; }

public DbSet<AppFullAuditedEntityWithAudited> AppFullAuditedEntityWithAudited { get; set; }

public DbSet<AppEntityWithAuditedAndHasCustomAuditingProperties> AppEntityWithAuditedAndHasCustomAuditingProperties { get; set; }

public DbSet<AppEntityWithSoftDelete> AppEntityWithSoftDelete { get; set; }

public AbpAuditingTestDbContext(DbContextOptions<AbpAuditingTestDbContext> options)
: base(options)
{

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,27 @@ public virtual async Task Should_Write_AuditLog_Without_ExtraPropertyDictionary(
&& x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithAudited.Name)));
#pragma warning restore 4014
}


[Fact]
public virtual async Task Should_Write_AuditLog_For_Soft_Deleted_Entity()
{
var entity = new AppEntityWithSoftDelete(Guid.NewGuid(), "test name");
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithSoftDelete, Guid>>();
await repository.InsertAsync(entity);

using (var scope = _auditingManager.BeginScope())
{
await repository.DeleteAsync(entity.Id);
await scope.SaveAsync();
}

#pragma warning disable 4014
_auditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.EntityChanges.Count == 1 &&
x.EntityChanges[0].ChangeType == EntityChangeType.Deleted &&
x.EntityChanges[0].PropertyChanges.Count == 0));
#pragma warning restore 4014

}
}
}