Soft delete documents

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-09-08 16:46:36 +02:00
parent 7000ccc246
commit b6254f4758
4 changed files with 35 additions and 17 deletions

View File

@@ -76,6 +76,7 @@ FROM
documents documents
WHERE WHERE
%s %s
AND deleted_at IS NULL
AND id = @document_id AND id = @document_id
LIMIT 1; LIMIT 1;
` `
@@ -114,6 +115,7 @@ FROM
documents documents
WHERE WHERE
%s %s
AND deleted_at IS NULL
AND organization_id = @organization_id AND organization_id = @organization_id
AND %s AND %s
` `
@@ -156,6 +158,7 @@ FROM
documents documents
WHERE WHERE
%s %s
AND deleted_at IS NULL
AND organization_id = @organization_id AND organization_id = @organization_id
AND %s AND %s
AND %s AND %s
@@ -232,18 +235,18 @@ VALUES (
return err return err
} }
func (p Document) Delete( func (p Document) SoftDelete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper, scope Scoper,
) error { ) error {
q := ` 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()) 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()) maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args) _, err := conn.Exec(ctx, q, args)
@@ -284,8 +287,10 @@ SET
document_type = @document_type, document_type = @document_type,
show_on_trust_center = @show_on_trust_center, show_on_trust_center = @show_on_trust_center,
updated_at = @updated_at updated_at = @updated_at
WHERE %s WHERE
AND id = @document_id %s
AND id = @document_id
AND deleted_at IS NULL
` `
q = fmt.Sprintf(q, scope.SQLFragment()) q = fmt.Sprintf(q, scope.SQLFragment())
@@ -321,7 +326,8 @@ WITH plcs AS (
p.id, p.id,
p.tenant_id, p.tenant_id,
p.search_vector, p.search_vector,
p.show_on_trust_center p.show_on_trust_center,
p.deleted_at
FROM FROM
documents p documents p
INNER JOIN INNER JOIN
@@ -333,7 +339,9 @@ SELECT
COUNT(id) COUNT(id)
FROM FROM
plcs plcs
WHERE %s WHERE
%s
AND deleted_at IS NULL
AND %s AND %s
` `
@@ -373,7 +381,8 @@ WITH plcs AS (
p.current_published_version, p.current_published_version,
p.show_on_trust_center, p.show_on_trust_center,
p.created_at, p.created_at,
p.updated_at p.updated_at,
p.deleted_at
FROM FROM
documents p documents p
INNER JOIN INNER JOIN
@@ -393,7 +402,9 @@ SELECT
updated_at updated_at
FROM FROM
plcs plcs
WHERE %s WHERE
%s
AND deleted_at IS NULL
AND %s AND %s
AND %s AND %s
` `
@@ -432,7 +443,8 @@ WITH plcs AS (
p.id, p.id,
p.tenant_id, p.tenant_id,
p.search_vector, p.search_vector,
p.show_on_trust_center p.show_on_trust_center,
p.deleted_at
FROM FROM
documents p documents p
INNER JOIN INNER JOIN
@@ -444,7 +456,9 @@ SELECT
COUNT(id) COUNT(id)
FROM FROM
plcs plcs
WHERE %s WHERE
%s
AND deleted_at IS NULL
AND %s AND %s
` `
@@ -484,7 +498,8 @@ WITH plcs AS (
p.show_on_trust_center, p.show_on_trust_center,
p.created_at, p.created_at,
p.updated_at, p.updated_at,
p.search_vector p.search_vector,
p.deleted_at
FROM FROM
documents p documents p
INNER JOIN INNER JOIN
@@ -504,7 +519,9 @@ SELECT
updated_at updated_at
FROM FROM
plcs plcs
WHERE %s WHERE
%s
AND deleted_at IS NULL
AND %s AND %s
AND %s AND %s
` `

View File

@@ -0,0 +1 @@
ALTER TABLE documents ADD COLUMN deleted_at TIMESTAMP WITH TIME ZONE;

View File

@@ -800,7 +800,7 @@ func (s *DocumentService) DeleteDraft(
) )
} }
func (s *DocumentService) Delete( func (s *DocumentService) SoftDelete(
ctx context.Context, ctx context.Context,
documentID gid.GID, documentID gid.GID,
) error { ) error {
@@ -809,7 +809,7 @@ func (s *DocumentService) Delete(
return s.svc.pg.WithConn( return s.svc.pg.WithConn(
ctx, ctx,
func(conn pg.Conn) error { func(conn pg.Conn) error {
return document.Delete(ctx, conn, s.svc.scope) return document.SoftDelete(ctx, conn, s.svc.scope)
}, },
) )
} }

View File

@@ -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) { func (r *mutationResolver) DeleteDocument(ctx context.Context, input types.DeleteDocumentInput) (*types.DeleteDocumentPayload, error) {
prb := r.ProboService(ctx, input.DocumentID.TenantID()) prb := r.ProboService(ctx, input.DocumentID.TenantID())
err := prb.Documents.Delete(ctx, input.DocumentID) err := prb.Documents.SoftDelete(ctx, input.DocumentID)
if err != nil { if err != nil {
panic(fmt.Errorf("cannot delete document: %w", err)) panic(fmt.Errorf("cannot soft delete document: %w", err))
} }
return &types.DeleteDocumentPayload{ return &types.DeleteDocumentPayload{