CreateCommentWithActivity writes the activity row a comment links to AND the comment in ONE transaction, so both commit or neither does (BUG-2716). The order inside is forced — comments.activity_id carries a foreign key, so the activity must exist before the comment can reference it — and that is e
(workspaceID, itemID, userID string, activity models.Activity, input models.CommentCreate)
| 62 | // activity records a write that has already committed and must survive a |
| 63 | // comment failure, so its current two-step order is the correct one there. |
| 64 | func (s *Store) CreateCommentWithActivity(workspaceID, itemID, userID string, activity models.Activity, input models.CommentCreate) (*models.Comment, error) { |
| 65 | tx, err := s.db.Begin() |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("begin comment tx: %w", err) |
| 68 | } |
| 69 | defer tx.Rollback() |
| 70 | |
| 71 | activityID, err := s.createActivityQ(tx, activity) |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("insert activity for comment: %w", err) |
| 74 | } |
| 75 | input.ActivityID = activityID |
| 76 | |
| 77 | id, err := s.createCommentTx(tx, workspaceID, itemID, userID, input) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | // Read back INSIDE the transaction, before the commit (TASK-3406, the |
| 82 | // BUG-3405 shape): a deletion landing after the commit (an account |
| 83 | // deletion, a revoke, a cascade) cannot turn a successful write into |
| 84 | // a nil result. |
| 85 | out, err := s.getCommentQ(tx, id) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | // TASK-3585: the caller's last word inside this transaction. |
| 90 | if input.AfterWriteTx != nil { |
| 91 | if err := input.AfterWriteTx(tx, out); err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | } |
| 95 | if err := tx.Commit(); err != nil { |
| 96 | return nil, fmt.Errorf("commit comment: %w", err) |
| 97 | } |
| 98 | afterCommitReadback("comment", id) |
| 99 | return out, nil |
| 100 | } |
| 101 | |
| 102 | // createCommentTx is the body of CreateComment against a caller's |
| 103 | // transaction: stamp, INSERT, read back, emit. It returns the new id and |