Give each document command one postgres transaction and only let table owners unwrap the scope, so cross-owner writes can roll back together. Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package docrevision
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"chat-agent-system/internal/app/uow"
|
|
)
|
|
|
|
// Repository is the ledger writer bound to a UnitOfWork scope.
|
|
type Repository struct{}
|
|
|
|
func (Repository) InsertBlob(ctx context.Context, scope uow.TxScope, sha, markdown string) error {
|
|
tx, err := uow.AsPG(scope)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.Exec(ctx, `
|
|
INSERT INTO document_content_blobs (content_sha256, content_markdown, byte_size, created_at)
|
|
VALUES ($1,$2,$3,$4)
|
|
ON CONFLICT (content_sha256) DO NOTHING`,
|
|
sha, markdown, int64(len(markdown)), time.Now().UTC())
|
|
if err != nil {
|
|
return fmt.Errorf("docrevision: insert blob: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (Repository) InsertSnapshot(ctx context.Context, scope uow.TxScope, sn Snapshot) (int64, error) {
|
|
tx, err := uow.AsPG(scope)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return (&Service{}).insertSnapshotTX(ctx, tx, &sn)
|
|
}
|
|
|
|
func (Repository) InsertVersion(ctx context.Context, scope uow.TxScope, v Version) (int64, error) {
|
|
tx, err := uow.AsPG(scope)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return (&Service{}).insertVersionTX(ctx, tx, &v)
|
|
}
|
|
|
|
func (Repository) InsertEvent(ctx context.Context, scope uow.TxScope, ev ChangeEvent) (int64, error) {
|
|
tx, err := uow.AsPG(scope)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return (&Service{}).insertEventTX(ctx, tx, &ev)
|
|
}
|