From 810058cd55e8a92f031b5ce3c0e5b7918911b3f3 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 1 Jun 2021 18:34:38 +0800 Subject: [PATCH] Fix soft delete with Update --- soft_delete.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/soft_delete.go b/soft_delete.go index b16041f1..af02f8fd 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -84,6 +84,32 @@ func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) { } } +func (DeletedAt) UpdateClauses(f *schema.Field) []clause.Interface { + return []clause.Interface{SoftDeleteUpdateClause{Field: f}} +} + +type SoftDeleteUpdateClause struct { + Field *schema.Field +} + +func (sd SoftDeleteUpdateClause) Name() string { + return "" +} + +func (sd SoftDeleteUpdateClause) Build(clause.Builder) { +} + +func (sd SoftDeleteUpdateClause) MergeClause(*clause.Clause) { +} + +func (sd SoftDeleteUpdateClause) ModifyStatement(stmt *Statement) { + if stmt.SQL.String() == "" { + if _, ok := stmt.Clauses["WHERE"]; stmt.DB.AllowGlobalUpdate || ok { + SoftDeleteQueryClause(sd).ModifyStatement(stmt) + } + } +} + func (DeletedAt) DeleteClauses(f *schema.Field) []clause.Interface { return []clause.Interface{SoftDeleteDeleteClause{Field: f}} }