fix(tasks): recover deleted captures without reviving tombstones
This commit is contained in:
1 parent
43a2d8c5a1
commit
4049acf76e
10 files changed
+594
-28
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// A receipt without a live task or a deletion marker needs repair, not an
|
||||
// implicit create that could silently revive an inconsistent record.
|
||||
var ErrTaskCaptureMissing = errors.New("captured task is missing without a deletion record")
|
||||
|
||||
func TaskRecreationID(request string) string {
|
||||
return uuid.NewSHA1(uuid.NameSpaceURL, []byte("pages:task-recreate:"+request)).String()
|
||||
}
|
||||
|
||||
func deletedCapture(ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}, domain, actor, request string) error {
|
||||
var id string
|
||||
if err := q.QueryRowContext(ctx, `SELECT item_uuid FROM organizer_capture_receipts WHERE domain_uuid=? AND owner_uuid=? AND request_uuid=?`, domain, actor, request).Scan(&id); err != nil {
|
||||
return err
|
||||
}
|
||||
var live, deleted bool
|
||||
if err := q.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=? AND uuid=?), EXISTS(SELECT 1 FROM organizer_task_deletions WHERE domain_uuid=? AND owner_uuid=? AND item_uuid=?)`, domain, id, domain, actor, id).Scan(&live, &deleted); err != nil {
|
||||
return err
|
||||
}
|
||||
if live {
|
||||
return ErrTaskConflict
|
||||
}
|
||||
if !deleted {
|
||||
return ErrTaskCaptureMissing
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecreateCapturedTask never removes old receipts, tombstones or DAV identity
|
||||
// reservations. A deterministic successor ID makes a lost response retry safe.
|
||||
// The caller authorizes read/create/write for target, including retained pages.
|
||||
func (s *Store) RecreateCapturedTask(ctx context.Context, domain, actor, request, hash string, item TaskRecord, target, body, replaces string) (TaskRecord, error) {
|
||||
id, err := uuid.Parse(replaces)
|
||||
if err != nil || id.String() != replaces || request != TaskRecreationID(replaces) {
|
||||
return TaskRecord{}, ErrTaskInvalid
|
||||
}
|
||||
if err = taskActor(ctx, s.DB, domain, actor, true); err != nil {
|
||||
return TaskRecord{}, err
|
||||
}
|
||||
if err = deletedCapture(ctx, s.DB, domain, actor, replaces); err != nil {
|
||||
return TaskRecord{}, err
|
||||
}
|
||||
return s.captureTaskWithWiki(ctx, domain, actor, request, hash, item, target, body, replaces)
|
||||
}
|
||||
Reference in new issue
Block a user