From b6254f4758a93ad00133e14e625411198011b881 Mon Sep 17 00:00:00 2001 From: Sacha Al Himdani Date: Mon, 8 Sep 2025 16:46:36 +0200 Subject: [PATCH] Soft delete documents Signed-off-by: Sacha Al Himdani --- pkg/coredata/document.go | 43 ++++++++++++++------ pkg/coredata/migrations/20250908T142945Z.sql | 1 + pkg/probo/document_service.go | 4 +- pkg/server/api/console/v1/v1_resolver.go | 4 +- 4 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 pkg/coredata/migrations/20250908T142945Z.sql diff --git a/pkg/coredata/document.go b/pkg/coredata/document.go index 535bd33b0..f90bfe4bd 100644 --- a/pkg/coredata/document.go +++ b/pkg/coredata/document.go @@ -76,6 +76,7 @@ FROM documents WHERE %s + AND deleted_at IS NULL AND id = @document_id LIMIT 1; ` @@ -114,6 +115,7 @@ FROM documents WHERE %s + AND deleted_at IS NULL AND organization_id = @organization_id AND %s ` @@ -156,6 +158,7 @@ FROM documents WHERE %s + AND deleted_at IS NULL AND organization_id = @organization_id AND %s AND %s @@ -232,18 +235,18 @@ VALUES ( return err } -func (p Document) Delete( +func (p Document) SoftDelete( ctx context.Context, conn pg.Conn, scope Scoper, ) error { q := ` -DELETE FROM documents WHERE %s AND id = @document_id +UPDATE documents SET deleted_at = @deleted_at WHERE %s AND id = @document_id ` q = fmt.Sprintf(q, scope.SQLFragment()) - args := pgx.StrictNamedArgs{"document_id": p.ID} + args := pgx.StrictNamedArgs{"document_id": p.ID, "deleted_at": time.Now()} maps.Copy(args, scope.SQLArguments()) _, err := conn.Exec(ctx, q, args) @@ -284,8 +287,10 @@ SET document_type = @document_type, show_on_trust_center = @show_on_trust_center, updated_at = @updated_at -WHERE %s - AND id = @document_id +WHERE + %s + AND id = @document_id + AND deleted_at IS NULL ` q = fmt.Sprintf(q, scope.SQLFragment()) @@ -321,7 +326,8 @@ WITH plcs AS ( p.id, p.tenant_id, p.search_vector, - p.show_on_trust_center + p.show_on_trust_center, + p.deleted_at FROM documents p INNER JOIN @@ -333,7 +339,9 @@ SELECT COUNT(id) FROM plcs -WHERE %s +WHERE + %s + AND deleted_at IS NULL AND %s ` @@ -373,7 +381,8 @@ WITH plcs AS ( p.current_published_version, p.show_on_trust_center, p.created_at, - p.updated_at + p.updated_at, + p.deleted_at FROM documents p INNER JOIN @@ -393,7 +402,9 @@ SELECT updated_at FROM plcs -WHERE %s +WHERE + %s + AND deleted_at IS NULL AND %s AND %s ` @@ -432,7 +443,8 @@ WITH plcs AS ( p.id, p.tenant_id, p.search_vector, - p.show_on_trust_center + p.show_on_trust_center, + p.deleted_at FROM documents p INNER JOIN @@ -444,7 +456,9 @@ SELECT COUNT(id) FROM plcs -WHERE %s +WHERE + %s + AND deleted_at IS NULL AND %s ` @@ -484,7 +498,8 @@ WITH plcs AS ( p.show_on_trust_center, p.created_at, p.updated_at, - p.search_vector + p.search_vector, + p.deleted_at FROM documents p INNER JOIN @@ -504,7 +519,9 @@ SELECT updated_at FROM plcs -WHERE %s +WHERE + %s + AND deleted_at IS NULL AND %s AND %s ` diff --git a/pkg/coredata/migrations/20250908T142945Z.sql b/pkg/coredata/migrations/20250908T142945Z.sql new file mode 100644 index 000000000..4f2cced4a --- /dev/null +++ b/pkg/coredata/migrations/20250908T142945Z.sql @@ -0,0 +1 @@ +ALTER TABLE documents ADD COLUMN deleted_at TIMESTAMP WITH TIME ZONE; diff --git a/pkg/probo/document_service.go b/pkg/probo/document_service.go index 36a0239ce..351ec9e90 100644 --- a/pkg/probo/document_service.go +++ b/pkg/probo/document_service.go @@ -800,7 +800,7 @@ func (s *DocumentService) DeleteDraft( ) } -func (s *DocumentService) Delete( +func (s *DocumentService) SoftDelete( ctx context.Context, documentID gid.GID, ) error { @@ -809,7 +809,7 @@ func (s *DocumentService) Delete( return s.svc.pg.WithConn( ctx, func(conn pg.Conn) error { - return document.Delete(ctx, conn, s.svc.scope) + return document.SoftDelete(ctx, conn, s.svc.scope) }, ) } diff --git a/pkg/server/api/console/v1/v1_resolver.go b/pkg/server/api/console/v1/v1_resolver.go index c05bbc9d3..f459caab2 100644 --- a/pkg/server/api/console/v1/v1_resolver.go +++ b/pkg/server/api/console/v1/v1_resolver.go @@ -2484,9 +2484,9 @@ func (r *mutationResolver) UpdateDocument(ctx context.Context, input types.Updat func (r *mutationResolver) DeleteDocument(ctx context.Context, input types.DeleteDocumentInput) (*types.DeleteDocumentPayload, error) { prb := r.ProboService(ctx, input.DocumentID.TenantID()) - err := prb.Documents.Delete(ctx, input.DocumentID) + err := prb.Documents.SoftDelete(ctx, input.DocumentID) if err != nil { - panic(fmt.Errorf("cannot delete document: %w", err)) + panic(fmt.Errorf("cannot soft delete document: %w", err)) } return &types.DeleteDocumentPayload{