fix(events): harden hosted mutation journal (bd-et7)
Adapt the contributor journal for per-project activation, replay-complete issue/dependency/comment payloads, transactional label and derived-readiness coverage, and additive PostgreSQL upgrades required by Hosted. Preserves the contributor implementation and tests through the preceding two-parent merge of PR #4916. Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot
This commit is contained in:
parent
50b7cfcd58
commit
3bfed2431b
59 changed files with 2059 additions and 299 deletions
9
beads.go
9
beads.go
|
|
@ -19,6 +19,15 @@ import (
|
|||
// Storage is the interface for beads storage operations
|
||||
type Storage = beads.Storage
|
||||
|
||||
// EventsJournalAccessor is the stable public capability exposed by stores that
|
||||
// support the durable events journal. Hosted callers can type-assert the
|
||||
// Storage returned by OpenServerPostgres to this interface without importing an
|
||||
// internal package.
|
||||
type EventsJournalAccessor = storage.EventsJournalAccessor
|
||||
|
||||
// EventsJournalRow is one committed, ordered journal record.
|
||||
type EventsJournalRow = storage.EventsJournalRow
|
||||
|
||||
// Transaction provides atomic multi-operation support within a database transaction.
|
||||
// Use Storage.RunInTransaction() to obtain a Transaction instance.
|
||||
type Transaction = beads.Transaction
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
)
|
||||
|
||||
// eventFollowPollInterval is how often `bd events tail --follow` polls the
|
||||
|
|
@ -54,17 +55,18 @@ var eventsTailCmd = &cobra.Command{
|
|||
Long: `Print events journal records with seq greater than --since, in order.
|
||||
|
||||
Each line is a JSON record:
|
||||
{"seq":N,"ts":"...","op":"create|update|close|delete|dep_add|dep_remove",
|
||||
"issue_id":"...","issue":{...|null},"dep":{"kind":..,"target":..}}
|
||||
{"seq":N,"ts":"...","op":"create|update|close|delete|dep_add|dep_remove|comment",
|
||||
"issue_id":"...","issue":{...|null},"dep":{"kind":..,"target":..,"metadata":..},"comment":{...}}
|
||||
|
||||
Record contract (stable for external consumers):
|
||||
seq int64 counter-assigned inside the mutation's transaction; gapless,
|
||||
strictly increasing in commit order, never reused or reset
|
||||
ts string UTC insert time, stamped inside the committing transaction
|
||||
op string one of the six ops above
|
||||
op string one of the seven ops above
|
||||
issue_id string the mutated issue's id
|
||||
issue object full issue state AFTER the mutation; null on delete
|
||||
dep object {"kind","target"} for dep_add / dep_remove; omitted otherwise
|
||||
dep object {"kind","target","metadata"} for dep_add / dep_remove; omitted otherwise
|
||||
comment object {"id","author","text","created_at","source"} for comment; omitted otherwise
|
||||
|
||||
Poll with the highest seq seen to consume new mutations incrementally, or pass
|
||||
--follow to keep printing new records as they are committed (Ctrl-C to stop).`,
|
||||
|
|
@ -147,12 +149,13 @@ type eventRecord struct {
|
|||
IssueID string `json:"issue_id"`
|
||||
Issue json.RawMessage `json:"issue"`
|
||||
Dep json.RawMessage `json:"dep,omitempty"`
|
||||
Comment json.RawMessage `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
// tailSelect builds the read query. CAST(ts AS CHAR) normalizes the DATETIME to
|
||||
// a string across drivers.
|
||||
func tailSelect(limit int) string {
|
||||
q := `SELECT seq, CAST(ts AS CHAR), op, issue_id, issue_json, dep_json
|
||||
q := `SELECT seq, CAST(ts AS CHAR), op, issue_id, issue_json, dep_json, comment_json
|
||||
FROM bd_events_journal WHERE seq > ? ORDER BY seq ASC`
|
||||
if limit > 0 {
|
||||
q += " LIMIT " + strconv.Itoa(limit)
|
||||
|
|
@ -234,25 +237,37 @@ func journalAccessor() (storage.EventsJournalAccessor, error) {
|
|||
return acc, nil
|
||||
}
|
||||
|
||||
// readJournal reads records with seq greater than since from the active store's
|
||||
// journal capability. The older proxied UOW surface intentionally has no raw
|
||||
// journal capability; its mutation paths remain supported, but journal tailing
|
||||
// requires the direct store path until that public seam is widened.
|
||||
// readJournal reads records with seq greater than since from the active
|
||||
// storage seam. Proxied-server mode uses its transaction-bound UOW journal
|
||||
// capability; direct stores use EventsJournalAccessor.
|
||||
func readJournal(ctx context.Context, since int64, limit int) ([]eventRecord, error) {
|
||||
var rows []storage.EventsJournalRow
|
||||
if usesProxiedServer() {
|
||||
return nil, fmt.Errorf("events journal is unavailable through the proxied-server UOW backend")
|
||||
}
|
||||
acc, err := journalAccessor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := acc.ReadEventsJournal(ctx, since, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if uowProvider == nil {
|
||||
return nil, fmt.Errorf("no proxied-server unit-of-work provider available")
|
||||
}
|
||||
uw, err := uowProvider.NewUOW(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer uw.Close(ctx)
|
||||
rows, err = uw.EventsJournalUseCase().Read(ctx, since, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
acc, err := journalAccessor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err = acc.ReadEventsJournal(ctx, since, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
out := make([]eventRecord, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, buildRecord(r.Seq, r.TS, r.Op, r.IssueID, r.IssueJSON, r.DepJSON))
|
||||
out = append(out, buildRecord(r.Seq, r.TS, r.Op, r.IssueID, r.IssueJSON, r.DepJSON, r.CommentJSON))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
@ -260,7 +275,16 @@ func readJournal(ctx context.Context, since int64, limit int) ([]eventRecord, er
|
|||
// pruneJournal deletes records below before honoring the retain floors.
|
||||
func pruneJournal(ctx context.Context, before int64, retainDays, retainRows int) (int64, error) {
|
||||
if usesProxiedServer() {
|
||||
return 0, fmt.Errorf("events journal is unavailable through the proxied-server UOW backend")
|
||||
if uowProvider == nil {
|
||||
return 0, fmt.Errorf("no proxied-server unit-of-work provider available")
|
||||
}
|
||||
var n int64
|
||||
err := uow.RunInTx(ctx, uowProvider, fmt.Sprintf("bd: prune events journal below %d", before), func(uw uow.UnitOfWork) error {
|
||||
var pruneErr error
|
||||
n, pruneErr = uw.EventsJournalUseCase().Prune(ctx, before, retainDays, retainRows)
|
||||
return pruneErr
|
||||
})
|
||||
return n, err
|
||||
}
|
||||
acc, err := journalAccessor()
|
||||
if err != nil {
|
||||
|
|
@ -269,7 +293,7 @@ func pruneJournal(ctx context.Context, before int64, retainDays, retainRows int)
|
|||
return acc.PruneEventsJournal(ctx, before, retainDays, retainRows)
|
||||
}
|
||||
|
||||
func buildRecord(seq int64, ts, op, issueID, issueJS, depJS string) eventRecord {
|
||||
func buildRecord(seq int64, ts, op, issueID, issueJS, depJS, commentJS string) eventRecord {
|
||||
rec := eventRecord{Seq: seq, TS: ts, Op: op, IssueID: issueID, Issue: json.RawMessage("null")}
|
||||
if issueJS != "" {
|
||||
rec.Issue = json.RawMessage(issueJS)
|
||||
|
|
@ -277,5 +301,8 @@ func buildRecord(seq int64, ts, op, issueID, issueJS, depJS string) eventRecord
|
|||
if depJS != "" {
|
||||
rec.Dep = json.RawMessage(depJS)
|
||||
}
|
||||
if commentJS != "" {
|
||||
rec.Comment = json.RawMessage(commentJS)
|
||||
}
|
||||
return rec
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,15 +98,16 @@ func renderGoldenLines(t *testing.T) []byte {
|
|||
}
|
||||
|
||||
records := []eventRecord{
|
||||
buildRecord(1, ts, string(issueops.EventCreate), minimal.ID, mustJSON(t, minimal), ""),
|
||||
buildRecord(2, ts, string(issueops.EventCreate), full.ID, mustJSON(t, full), ""),
|
||||
buildRecord(1, ts, string(issueops.EventCreate), minimal.ID, mustJSON(t, minimal), "", ""),
|
||||
buildRecord(2, ts, string(issueops.EventCreate), full.ID, mustJSON(t, full), "", ""),
|
||||
buildRecord(3, ts, string(issueops.EventDepAdd), "bd-101", mustJSON(t, full),
|
||||
mustJSON(t, &issueops.EventDep{Kind: string(types.DepBlocks), Target: "bd-100"})),
|
||||
mustJSON(t, &issueops.EventDep{Kind: string(types.DepBlocks), Target: "bd-100", Metadata: `{}`}), ""),
|
||||
buildRecord(4, ts, string(issueops.EventDepRemove), "bd-101", mustJSON(t, full),
|
||||
mustJSON(t, &issueops.EventDep{Kind: string(types.DepBlocks), Target: "bd-100"})),
|
||||
buildRecord(5, ts, string(issueops.EventClose), closedIssue.ID, mustJSON(t, closedIssue), ""),
|
||||
buildRecord(6, ts, string(issueops.EventCreate), wisp.ID, mustJSON(t, wisp), ""),
|
||||
buildRecord(7, ts, string(issueops.EventDelete), "bd-100", "", ""), // null issue on delete
|
||||
mustJSON(t, &issueops.EventDep{Kind: string(types.DepBlocks), Target: "bd-100", Metadata: `{}`}), ""),
|
||||
buildRecord(5, ts, string(issueops.EventCommentWrite), full.ID, mustJSON(t, full), "", mustJSON(t, &issueops.EventComment{ID: "019c0000-0000-7000-8000-000000000000", Author: "worker-1", Text: "journal me", CreatedAt: updated, Source: "structured"})),
|
||||
buildRecord(6, ts, string(issueops.EventClose), closedIssue.ID, mustJSON(t, closedIssue), "", ""),
|
||||
buildRecord(7, ts, string(issueops.EventCreate), wisp.ID, mustJSON(t, wisp), "", ""),
|
||||
buildRecord(8, ts, string(issueops.EventDelete), "bd-100", "", "", ""), // null issue on delete
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
|
|
|||
|
|
@ -1,16 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
)
|
||||
|
||||
// applyEventsJournalConfig turns the durable events journal on or off for
|
||||
// this process from the events-journal config knob (or the
|
||||
// BD_EVENTS_JOURNAL environment override). It must run before any mutation on
|
||||
// either write plumbing, since emission lives at the shared issueops seam. OFF
|
||||
// by default: when off, the seam's emit helpers are a cheap no-op and no journal
|
||||
// rows are written.
|
||||
func applyEventsJournalConfig() {
|
||||
issueops.SetJournalEnabled(config.GetBool("events-journal"))
|
||||
// validateEventsJournalBackend rejects activation on SQL-family backends that
|
||||
// have not implemented the journal contract. Disabled configurations remain
|
||||
// accepted so existing MySQL and SQLite workspaces continue to open normally.
|
||||
func validateEventsJournalBackend(backend string, enabled bool) error {
|
||||
if !enabled {
|
||||
return nil
|
||||
}
|
||||
switch backend {
|
||||
case "", configfile.BackendDolt, configfile.BackendPostgres:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("events journal is not supported by the %s backend", backend)
|
||||
}
|
||||
}
|
||||
|
||||
func eventsJournalEnabled() bool {
|
||||
return config.GetBool("events-journal")
|
||||
}
|
||||
|
||||
// withConfiguredEventsJournal scopes proxied UOW operations to this command's
|
||||
// selected workspace. The value follows rootCtx into every repository call and
|
||||
// cannot affect another store or project in the process.
|
||||
func withConfiguredEventsJournal(ctx context.Context, enabled bool) context.Context {
|
||||
return issueops.WithEventsJournal(ctx, enabled)
|
||||
}
|
||||
|
||||
// configureEventsJournalStore applies activation to one concrete direct store.
|
||||
// Enabled backends fail closed if they do not expose the capability.
|
||||
func configureEventsJournalStore(s storage.DoltStorage, enabled bool) error {
|
||||
configurer, ok := storage.UnwrapStore(s).(storage.EventsJournalConfigurer)
|
||||
if !ok {
|
||||
if enabled {
|
||||
return fmt.Errorf("storage backend does not support the events journal")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
configurer.SetEventsJournalEnabled(enabled)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
144
cmd/bd/events_journal_test.go
Normal file
144
cmd/bd/events_journal_test.go
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
)
|
||||
|
||||
func TestValidateEventsJournalBackend(t *testing.T) {
|
||||
for _, backend := range []string{"", configfile.BackendPostgres} {
|
||||
if err := validateEventsJournalBackend(backend, true); err != nil {
|
||||
t.Errorf("supported backend %q rejected: %v", backend, err)
|
||||
}
|
||||
}
|
||||
for _, backend := range []string{configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
if err := validateEventsJournalBackend(backend, true); err == nil {
|
||||
t.Errorf("unsupported backend %q accepted journal activation", backend)
|
||||
}
|
||||
if err := validateEventsJournalBackend(backend, false); err != nil {
|
||||
t.Errorf("disabled journal should not reject backend %q: %v", backend, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type recordingEventsJournalUseCase struct {
|
||||
rows []storage.EventsJournalRow
|
||||
|
||||
readSince int64
|
||||
readLimit int
|
||||
|
||||
pruneBefore int64
|
||||
pruneRetainDays int
|
||||
pruneRetainRows int
|
||||
pruned int64
|
||||
}
|
||||
|
||||
func (u *recordingEventsJournalUseCase) Read(_ context.Context, since int64, limit int) ([]storage.EventsJournalRow, error) {
|
||||
u.readSince = since
|
||||
u.readLimit = limit
|
||||
return u.rows, nil
|
||||
}
|
||||
|
||||
func (u *recordingEventsJournalUseCase) Prune(_ context.Context, before int64, retainDays, retainRows int) (int64, error) {
|
||||
u.pruneBefore = before
|
||||
u.pruneRetainDays = retainDays
|
||||
u.pruneRetainRows = retainRows
|
||||
return u.pruned, nil
|
||||
}
|
||||
|
||||
type journalTestUOW struct {
|
||||
journal domain.EventsJournalUseCase
|
||||
commitCount int
|
||||
}
|
||||
|
||||
func (u *journalTestUOW) Close(context.Context) {}
|
||||
func (u *journalTestUOW) Commit(context.Context, string) error { u.commitCount++; return nil }
|
||||
func (u *journalTestUOW) ConfigUseCase() domain.ConfigUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) DoltRemoteUseCase() domain.DoltRemoteUseCase {
|
||||
panic("not used")
|
||||
}
|
||||
func (u *journalTestUOW) BootstrapUseCase() domain.BootstrapUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) IssueUseCase() domain.IssueUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) DependencyUseCase() domain.DependencyUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) LabelUseCase() domain.LabelUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) CommentUseCase() domain.CommentUseCase { panic("not used") }
|
||||
func (u *journalTestUOW) EventsJournalUseCase() domain.EventsJournalUseCase {
|
||||
return u.journal
|
||||
}
|
||||
|
||||
type journalTestProvider struct {
|
||||
uw *journalTestUOW
|
||||
}
|
||||
|
||||
func (p *journalTestProvider) NewUOW(context.Context) (uow.UnitOfWork, error) { return p.uw, nil }
|
||||
func (p *journalTestProvider) Close(context.Context) error { return nil }
|
||||
|
||||
func installProxiedJournalTestProvider(t *testing.T, p uow.UnitOfWorkProvider) {
|
||||
t.Helper()
|
||||
oldProvider := uowProvider
|
||||
oldProxied := proxiedServerMode
|
||||
oldCmdCtx := cmdCtx
|
||||
oldTestMode := testModeUseGlobals
|
||||
t.Cleanup(func() {
|
||||
uowProvider = oldProvider
|
||||
proxiedServerMode = oldProxied
|
||||
cmdCtx = oldCmdCtx
|
||||
testModeUseGlobals = oldTestMode
|
||||
})
|
||||
t.Setenv("BD_SPIKE_UOWSTORE", "")
|
||||
testModeUseGlobals = true
|
||||
cmdCtx = nil
|
||||
proxiedServerMode = true
|
||||
uowProvider = p
|
||||
}
|
||||
|
||||
func TestReadJournalUsesProxiedUOWCursor(t *testing.T) {
|
||||
journal := &recordingEventsJournalUseCase{
|
||||
rows: []storage.EventsJournalRow{{
|
||||
Seq: 12,
|
||||
TS: "2026-07-29T10:00:00Z",
|
||||
Op: "update",
|
||||
IssueID: "bd-12",
|
||||
IssueJSON: `{"id":"bd-12"}`,
|
||||
}},
|
||||
}
|
||||
provider := &journalTestProvider{uw: &journalTestUOW{journal: journal}}
|
||||
installProxiedJournalTestProvider(t, provider)
|
||||
|
||||
rows, err := readJournal(context.Background(), 11, 25)
|
||||
if err != nil {
|
||||
t.Fatalf("readJournal: %v", err)
|
||||
}
|
||||
if journal.readSince != 11 || journal.readLimit != 25 {
|
||||
t.Fatalf("proxied cursor = (since=%d, limit=%d), want (11, 25)", journal.readSince, journal.readLimit)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].Seq != 12 || rows[0].IssueID != "bd-12" {
|
||||
t.Fatalf("readJournal rows = %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneJournalUsesCommittedProxiedUOW(t *testing.T) {
|
||||
journal := &recordingEventsJournalUseCase{pruned: 7}
|
||||
uw := &journalTestUOW{journal: journal}
|
||||
installProxiedJournalTestProvider(t, &journalTestProvider{uw: uw})
|
||||
|
||||
n, err := pruneJournal(context.Background(), 100, 14, 500)
|
||||
if err != nil {
|
||||
t.Fatalf("pruneJournal: %v", err)
|
||||
}
|
||||
if n != 7 {
|
||||
t.Fatalf("pruned = %d, want 7", n)
|
||||
}
|
||||
if journal.pruneBefore != 100 || journal.pruneRetainDays != 14 || journal.pruneRetainRows != 500 {
|
||||
t.Fatalf("proxied prune args = (before=%d, days=%d, rows=%d), want (100, 14, 500)",
|
||||
journal.pruneBefore, journal.pruneRetainDays, journal.pruneRetainRows)
|
||||
}
|
||||
if uw.commitCount != 1 {
|
||||
t.Fatalf("proxied prune commits = %d, want 1", uw.commitCount)
|
||||
}
|
||||
}
|
||||
|
|
@ -1105,6 +1105,15 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
doltCfg.SyncRemote = resolveSyncRemote()
|
||||
|
||||
journalEnabled := eventsJournalEnabled()
|
||||
backend := configfile.BackendDolt
|
||||
if cfg != nil {
|
||||
backend = cfg.GetBackend()
|
||||
}
|
||||
if err := validateEventsJournalBackend(backend, journalEnabled); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
// --global flag: switch to the global shared-server database.
|
||||
// Must be in shared-server mode; errors otherwise.
|
||||
if globalFlag {
|
||||
|
|
@ -1118,11 +1127,6 @@ var rootCmd = &cobra.Command{
|
|||
// other helper paths stay in lockstep with the main command path.
|
||||
dolt.ApplyCLIAutoStart(beadsDir, doltCfg)
|
||||
|
||||
// Enable the shared transaction-seam journal before either the Dolt or
|
||||
// SQL-family store can serve a mutation. The setting is process-local by
|
||||
// design; BD_EVENTS_JOURNAL is the Hosted writer activation switch.
|
||||
applyEventsJournalConfig()
|
||||
|
||||
// BD_SPIKE_UOWSTORE (issue #4547 Route A derisk): in proxied mode the CLI
|
||||
// normally short-circuits to the uowProvider path and dispatches through
|
||||
// the *_proxied_server.go duals. With the spike flag set we instead fall
|
||||
|
|
@ -1130,6 +1134,7 @@ var rootCmd = &cobra.Command{
|
|||
// travel the ordinary store path — exercising exactly the adapter this
|
||||
// spike is proving. Default (unset) keeps the original short-circuit.
|
||||
if proxiedServerMode && !spikeUOWStore() {
|
||||
rootCtx = withConfiguredEventsJournal(rootCtx, journalEnabled)
|
||||
p, err := newProxiedServerUOWProvider(rootCtx, beadsDir)
|
||||
if err != nil {
|
||||
return HandleError("failed to open uow provider: %v", err)
|
||||
|
|
@ -1209,6 +1214,11 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
return HandleError("failed to open database: %v", err)
|
||||
}
|
||||
if err := configureEventsJournalStore(store, journalEnabled); err != nil {
|
||||
_ = store.Close()
|
||||
store = nil
|
||||
return HandleError("configuring events journal: %v", err)
|
||||
}
|
||||
|
||||
// Mark store as active for flush goroutine safety
|
||||
storeMutex.Lock()
|
||||
|
|
|
|||
15
cmd/bd/testdata/events_journal_records.jsonl
vendored
15
cmd/bd/testdata/events_journal_records.jsonl
vendored
|
|
@ -1,7 +1,8 @@
|
|||
{"seq":1,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-100","issue":{"id":"bd-100","title":"wire the seam","status":"open","priority":1,"issue_type":"task","created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:04:05Z"}}
|
||||
{"seq":2,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]}}
|
||||
{"seq":3,"ts":"2026-01-02 03:04:05","op":"dep_add","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]},"dep":{"kind":"blocks","target":"bd-100"}}
|
||||
{"seq":4,"ts":"2026-01-02 03:04:05","op":"dep_remove","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]},"dep":{"kind":"blocks","target":"bd-100"}}
|
||||
{"seq":5,"ts":"2026-01-02 03:04:05","op":"close","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","status":"closed","priority":0,"issue_type":"feature","created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:30:00Z","closed_at":"2026-01-02T03:30:00Z","close_reason":"shipped"}}
|
||||
{"seq":6,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-wisp-1","issue":{"id":"bd-wisp-1","title":"convoy member","status":"open","priority":2,"issue_type":"task","created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:04:05Z","ephemeral":true,"wisp_type":"convoy"}}
|
||||
{"seq":7,"ts":"2026-01-02 03:04:05","op":"delete","issue_id":"bd-100","issue":null}
|
||||
{"seq":1,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-100","issue":{"id":"bd-100","title":"wire the seam","status":"open","priority":1,"issue_type":"task","is_blocked":false,"created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:04:05Z"}}
|
||||
{"seq":2,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","is_blocked":false,"assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]}}
|
||||
{"seq":3,"ts":"2026-01-02 03:04:05","op":"dep_add","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","is_blocked":false,"assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]},"dep":{"kind":"blocks","target":"bd-100","metadata":"{}"}}
|
||||
{"seq":4,"ts":"2026-01-02 03:04:05","op":"dep_remove","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","is_blocked":false,"assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]},"dep":{"kind":"blocks","target":"bd-100","metadata":"{}"}}
|
||||
{"seq":5,"ts":"2026-01-02 03:04:05","op":"comment","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","description":"append-only record","acceptance_criteria":"replayable","status":"in_progress","priority":0,"issue_type":"feature","is_blocked":false,"assignee":"worker-1","owner":"dev@example.com","estimated_minutes":90,"created_at":"2026-01-02T03:00:00Z","created_by":"author","updated_at":"2026-01-02T03:04:05Z","external_ref":"gh-9","source_system":"github","metadata":{"k":"v"},"labels":["infra","urgent"]},"comment":{"id":"019c0000-0000-7000-8000-000000000000","author":"worker-1","text":"journal me","created_at":"2026-01-02T03:04:05Z","source":"structured"}}
|
||||
{"seq":6,"ts":"2026-01-02 03:04:05","op":"close","issue_id":"bd-101","issue":{"id":"bd-101","title":"durable journal","status":"closed","priority":0,"issue_type":"feature","is_blocked":false,"created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:30:00Z","closed_at":"2026-01-02T03:30:00Z","close_reason":"shipped"}}
|
||||
{"seq":7,"ts":"2026-01-02 03:04:05","op":"create","issue_id":"bd-wisp-1","issue":{"id":"bd-wisp-1","title":"convoy member","status":"open","priority":2,"issue_type":"task","is_blocked":false,"created_at":"2026-01-02T03:00:00Z","updated_at":"2026-01-02T03:04:05Z","ephemeral":true,"wisp_type":"convoy"}}
|
||||
{"seq":8,"ts":"2026-01-02 03:04:05","op":"delete","issue_id":"bd-100","issue":null}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,11 @@ import (
|
|||
// at startup, not from the database).
|
||||
var YamlOnlyKeys = map[string]bool{
|
||||
// Bootstrap flags (affect how bd starts)
|
||||
"no-db": true,
|
||||
"json": true,
|
||||
"no-db": true,
|
||||
"json": true,
|
||||
"events-journal": true,
|
||||
"events-journal-retain-days": true,
|
||||
"events-journal-retain-rows": true,
|
||||
|
||||
// Database and identity
|
||||
"db": true,
|
||||
|
|
|
|||
|
|
@ -69,6 +69,18 @@ func TestIsYamlOnlyKey(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestEventsJournalConfigIsStartupScoped(t *testing.T) {
|
||||
for _, key := range []string{
|
||||
"events-journal",
|
||||
"events-journal-retain-days",
|
||||
"events-journal-retain-rows",
|
||||
} {
|
||||
if !IsYamlOnlyKey(key) {
|
||||
t.Errorf("%s must be stored in config.yaml so startup activation and retention read the value that config set writes", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateYamlKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -64,6 +64,8 @@ func (s *DoltStore) RemoveDependency(ctx context.Context, issueID, dependsOnID s
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if err := issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID); err != nil {
|
||||
return err
|
||||
|
|
@ -75,6 +77,8 @@ func (s *DoltStore) RemoveDependency(ctx context.Context, issueID, dependsOnID s
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if err := issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID); err != nil {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ func (s *DoltStore) AddComment(ctx context.Context, issueID, actor, comment stri
|
|||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if err := issueops.AddCommentEventInTx(ctx, tx, issueID, actor, comment); err != nil {
|
||||
|
|
|
|||
|
|
@ -2,41 +2,22 @@ package dolt
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
var (
|
||||
journalRefMu sync.Mutex
|
||||
journalRefs int
|
||||
)
|
||||
|
||||
// enableJournalForTest turns the process-global events journal on for a test
|
||||
// and keeps it on until the last concurrent journal test finishes. The dolt
|
||||
// suite runs tests in parallel (setupTestStore calls t.Parallel), so a plain
|
||||
// enable/disable pair would let one journal test's cleanup flip the flag off
|
||||
// underneath a still-running sibling. Reference counting keeps the flag stable
|
||||
// across the overlapping journal tests; non-journal tests are unaffected because
|
||||
// the journal table is dolt_ignored and separate from versioned data.
|
||||
func enableJournalForTest(t *testing.T) {
|
||||
// enableJournalForTest activates only this store. Journal tests run in
|
||||
// parallel, so a process-global switch would leak across unrelated projects.
|
||||
func enableJournalForTest(t *testing.T, store *DoltStore) {
|
||||
t.Helper()
|
||||
journalRefMu.Lock()
|
||||
journalRefs++
|
||||
issueops.SetJournalEnabled(true)
|
||||
journalRefMu.Unlock()
|
||||
t.Cleanup(func() {
|
||||
journalRefMu.Lock()
|
||||
journalRefs--
|
||||
if journalRefs == 0 {
|
||||
issueops.SetJournalEnabled(false)
|
||||
}
|
||||
journalRefMu.Unlock()
|
||||
})
|
||||
store.SetEventsJournalEnabled(true)
|
||||
t.Cleanup(func() { store.SetEventsJournalEnabled(false) })
|
||||
}
|
||||
|
||||
// jrow is one journal row read back in seq order.
|
||||
|
|
@ -93,7 +74,7 @@ func TestEventsJournal_SeamEntryPoints(t *testing.T) {
|
|||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t)
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
|
|
@ -111,8 +92,9 @@ func TestEventsJournal_SeamEntryPoints(t *testing.T) {
|
|||
if err := store.UpdateIssueID(ctx, "bd-rn-1", "bd-rn-2", iss, "actor"); err != nil {
|
||||
t.Fatalf("rename: %v", err)
|
||||
}
|
||||
if rows := readJournalRows(t, store); !hasOpFor(rows, "update", "bd-rn-2") {
|
||||
t.Fatalf("rename must journal an update for the new id; got %+v", rows)
|
||||
rows := readJournalRows(t, store)
|
||||
if !hasOpFor(rows, "delete", "bd-rn-1") || !hasOpFor(rows, "create", "bd-rn-2") {
|
||||
t.Fatalf("rename must journal an old-node delete and new-node create; got %+v", rows)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -136,6 +118,25 @@ func TestEventsJournal_SeamEntryPoints(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("transaction label writes", func(t *testing.T) {
|
||||
if err := store.CreateIssue(ctx, mk("bd-tx-label-1"), "actor"); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
clearJournal(t, store)
|
||||
if err := store.RunInTransaction(ctx, "journal transaction labels", func(tx storage.Transaction) error {
|
||||
if err := tx.AddLabel(ctx, "bd-tx-label-1", "demo", "actor"); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.RemoveLabel(ctx, "bd-tx-label-1", "demo", "actor")
|
||||
}); err != nil {
|
||||
t.Fatalf("transaction labels: %v", err)
|
||||
}
|
||||
rows := readJournalRows(t, store)
|
||||
if len(rows) != 2 || !hasOpFor(rows, "update", "bd-tx-label-1") {
|
||||
t.Fatalf("transaction add/remove label must each journal an update; got %+v", rows)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reopen", func(t *testing.T) {
|
||||
if err := store.CreateIssue(ctx, mk("bd-ro-1"), "actor"); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
|
|
@ -240,6 +241,149 @@ func TestEventsJournal_SeamEntryPoints(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// TestEventsJournal_RenameRelationshipDeltas proves a cursor consumer can
|
||||
// replay a rename without querying current graph state. Both source and target
|
||||
// endpoints are explicit in old-edge removals and new-edge additions.
|
||||
func TestEventsJournal_RenameRelationshipDeltas(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
}
|
||||
for _, id := range []string{"bd-rn-old", "bd-rn-out", "bd-rn-in"} {
|
||||
if err := store.CreateIssue(ctx, mk(id), "actor"); err != nil {
|
||||
t.Fatalf("create %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
for _, dep := range []*types.Dependency{
|
||||
{IssueID: "bd-rn-old", DependsOnID: "bd-rn-out", Type: types.DepBlocks},
|
||||
{IssueID: "bd-rn-in", DependsOnID: "bd-rn-old", Type: types.DepRelated},
|
||||
} {
|
||||
if err := store.AddDependency(ctx, dep, "actor"); err != nil {
|
||||
t.Fatalf("add dependency %+v: %v", dep, err)
|
||||
}
|
||||
}
|
||||
clearJournal(t, store)
|
||||
issue, err := store.GetIssue(ctx, "bd-rn-old")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.UpdateIssueID(ctx, "bd-rn-old", "bd-rn-new", issue, "actor"); err != nil {
|
||||
t.Fatalf("rename: %v", err)
|
||||
}
|
||||
|
||||
rows, err := store.db.QueryContext(ctx,
|
||||
`SELECT op, issue_id, dep_json FROM bd_events_journal ORDER BY seq ASC`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var got []string
|
||||
for rows.Next() {
|
||||
var op, id string
|
||||
var depJSON []byte
|
||||
if err := rows.Scan(&op, &id, &depJSON); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(depJSON) == 0 {
|
||||
got = append(got, op+":"+id)
|
||||
continue
|
||||
}
|
||||
var dep issueops.EventDep
|
||||
if err := json.Unmarshal(depJSON, &dep); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = append(got, op+":"+id+"->"+dep.Target+":"+dep.Kind)
|
||||
}
|
||||
want := []string{
|
||||
"dep_remove:bd-rn-in->bd-rn-old:related",
|
||||
"dep_remove:bd-rn-old->bd-rn-out:blocks",
|
||||
"delete:bd-rn-old",
|
||||
"create:bd-rn-new",
|
||||
"dep_add:bd-rn-in->bd-rn-new:related",
|
||||
"dep_add:bd-rn-new->bd-rn-out:blocks",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("rename journal = %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("rename journal[%d] = %q, want %q (all=%v)", i, got[i], want[i], got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournal_DeleteRelationshipDeltas guards the direct-store path:
|
||||
// both incoming and outgoing edges are explicit dep_remove rows before the
|
||||
// deleted node, so replay never relies on implicit database cascades.
|
||||
func TestEventsJournal_DeleteRelationshipDeltas(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
}
|
||||
for _, id := range []string{"bd-del-center", "bd-del-in", "bd-del-out"} {
|
||||
if err := store.CreateIssue(ctx, mk(id), "actor"); err != nil {
|
||||
t.Fatalf("create %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
for _, dep := range []*types.Dependency{
|
||||
{IssueID: "bd-del-center", DependsOnID: "bd-del-out", Type: types.DepBlocks},
|
||||
{IssueID: "bd-del-in", DependsOnID: "bd-del-center", Type: types.DepRelated},
|
||||
} {
|
||||
if err := store.AddDependency(ctx, dep, "actor"); err != nil {
|
||||
t.Fatalf("add dependency %+v: %v", dep, err)
|
||||
}
|
||||
}
|
||||
clearJournal(t, store)
|
||||
if err := store.DeleteIssue(ctx, "bd-del-center"); err != nil {
|
||||
t.Fatalf("delete: %v", err)
|
||||
}
|
||||
|
||||
rows, err := store.db.QueryContext(ctx,
|
||||
`SELECT op, issue_id, dep_json FROM bd_events_journal ORDER BY seq ASC`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var got []string
|
||||
for rows.Next() {
|
||||
var op, id string
|
||||
var depJSON []byte
|
||||
if err := rows.Scan(&op, &id, &depJSON); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(depJSON) == 0 {
|
||||
got = append(got, op+":"+id)
|
||||
continue
|
||||
}
|
||||
var dep issueops.EventDep
|
||||
if err := json.Unmarshal(depJSON, &dep); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = append(got, op+":"+id+"->"+dep.Target+":"+dep.Kind)
|
||||
}
|
||||
want := []string{
|
||||
"dep_remove:bd-del-center->bd-del-out:blocks",
|
||||
"dep_remove:bd-del-in->bd-del-center:related",
|
||||
"delete:bd-del-center",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("delete journal = %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("delete journal[%d] = %q, want %q (all=%v)", i, got[i], want[i], got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournalAccessorServerStore guards the server-mode DoltStore's
|
||||
// EventsJournalAccessor (the read/prune capability the `bd events` CLI uses
|
||||
// against a Dolt SQL server), mirroring the embedded-store guard in embeddeddolt.
|
||||
|
|
@ -247,7 +391,7 @@ func TestEventsJournalAccessorServerStore(t *testing.T) {
|
|||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t)
|
||||
enableJournalForTest(t, store)
|
||||
clearJournal(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
|
|
@ -297,7 +441,7 @@ func TestEventsJournal_ReplayFromZero(t *testing.T) {
|
|||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t)
|
||||
enableJournalForTest(t, store)
|
||||
clearJournal(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
|
|
@ -354,3 +498,239 @@ func TestEventsJournal_ReplayFromZero(t *testing.T) {
|
|||
t.Fatalf("bd-rp-3 was deleted; replay must not resurrect it")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournal_DependencySnapshotsFollowBlockedState proves that a
|
||||
// consumer never observes a dependency delta whose issue snapshot predates the
|
||||
// derived is_blocked state committed with that graph change. It also pins the
|
||||
// exact metadata replacement payload used to replay same-type refreshes.
|
||||
func TestEventsJournal_DependencySnapshotsFollowBlockedState(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
}
|
||||
for _, id := range []string{"bd-js-source", "bd-js-target"} {
|
||||
if err := store.CreateIssue(ctx, mk(id), "actor"); err != nil {
|
||||
t.Fatalf("create %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
clearJournal(t, store)
|
||||
|
||||
add := func(metadata string) {
|
||||
t.Helper()
|
||||
if err := store.AddDependency(ctx, &types.Dependency{IssueID: "bd-js-source", DependsOnID: "bd-js-target", Type: types.DepBlocks, Metadata: metadata}, "actor"); err != nil {
|
||||
t.Fatalf("add dependency: %v", err)
|
||||
}
|
||||
}
|
||||
readLast := func() (types.Issue, issueops.EventDep) {
|
||||
t.Helper()
|
||||
var issueJSON, depJSON []byte
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT issue_json, dep_json FROM bd_events_journal ORDER BY seq DESC LIMIT 1`).Scan(&issueJSON, &depJSON); err != nil {
|
||||
t.Fatalf("read journal: %v", err)
|
||||
}
|
||||
var issue types.Issue
|
||||
var dep issueops.EventDep
|
||||
if err := json.Unmarshal(issueJSON, &issue); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(depJSON, &dep); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return issue, dep
|
||||
}
|
||||
|
||||
add(`{"revision":"A"}`)
|
||||
issue, dep := readLast()
|
||||
if !issue.IsBlocked || dep.Metadata != `{"revision":"A"}` {
|
||||
t.Fatalf("initial dep_add = issue(blocked=%v), dep=%+v; want blocked snapshot and metadata A", issue.IsBlocked, dep)
|
||||
}
|
||||
add(`{"revision":"B"}`)
|
||||
issue, dep = readLast()
|
||||
if !issue.IsBlocked || dep.Metadata != `{"revision":"B"}` {
|
||||
t.Fatalf("metadata refresh = issue(blocked=%v), dep=%+v; want blocked snapshot and metadata B", issue.IsBlocked, dep)
|
||||
}
|
||||
if err := store.RemoveDependency(ctx, "bd-js-source", "bd-js-target", "actor"); err != nil {
|
||||
t.Fatalf("remove dependency: %v", err)
|
||||
}
|
||||
issue, dep = readLast()
|
||||
if issue.IsBlocked || dep.Metadata != `{"revision":"B"}` {
|
||||
t.Fatalf("dep_remove = issue(blocked=%v), dep=%+v; want unblocked snapshot and metadata B", issue.IsBlocked, dep)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournal_DerivedBlockedStateChanges proves that mutations affecting
|
||||
// another bead's persisted readiness emit a post-recompute update for that
|
||||
// bead. Without these rows, cursor consumers retain stale is_blocked values
|
||||
// until a later full reconciliation.
|
||||
func TestEventsJournal_DerivedBlockedStateChanges(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
}
|
||||
mustCreate := func(tb testing.TB, ids ...string) {
|
||||
tb.Helper()
|
||||
for _, id := range ids {
|
||||
if err := store.CreateIssue(ctx, mk(id), "actor"); err != nil {
|
||||
tb.Fatalf("create %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
mustAdd := func(tb testing.TB, source, target string, kind types.DependencyType) {
|
||||
tb.Helper()
|
||||
if err := store.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: source, DependsOnID: target, Type: kind,
|
||||
}, "actor"); err != nil {
|
||||
tb.Fatalf("add dependency %s -> %s (%s): %v", source, target, kind, err)
|
||||
}
|
||||
}
|
||||
assertBlocked := func(tb testing.TB, id string, want bool) {
|
||||
tb.Helper()
|
||||
var got int
|
||||
if err := store.db.QueryRowContext(ctx, "SELECT is_blocked FROM issues WHERE id = ?", id).Scan(&got); err != nil {
|
||||
tb.Fatalf("read is_blocked for %s: %v", id, err)
|
||||
}
|
||||
if (got != 0) != want {
|
||||
tb.Fatalf("%s is_blocked = %d, want %v", id, got, want)
|
||||
}
|
||||
}
|
||||
assertJournalUpdate := func(tb testing.TB, id string, wantBlocked bool) {
|
||||
tb.Helper()
|
||||
rows, err := store.db.QueryContext(ctx,
|
||||
`SELECT issue_json FROM bd_events_journal WHERE op = 'update' AND issue_id = ? ORDER BY seq`, id)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var snapshots []types.Issue
|
||||
for rows.Next() {
|
||||
var raw []byte
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
var issue types.Issue
|
||||
if err := json.Unmarshal(raw, &issue); err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
snapshots = append(snapshots, issue)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
if len(snapshots) != 1 || snapshots[0].IsBlocked != wantBlocked {
|
||||
tb.Fatalf("journal updates for %s = %+v, want one snapshot with is_blocked=%v", id, snapshots, wantBlocked)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("closing blocker updates depender", func(t *testing.T) {
|
||||
mustCreate(t, "bd-derived-close-source", "bd-derived-close-target")
|
||||
mustAdd(t, "bd-derived-close-source", "bd-derived-close-target", types.DepBlocks)
|
||||
assertBlocked(t, "bd-derived-close-source", true)
|
||||
clearJournal(t, store)
|
||||
|
||||
if err := store.CloseIssue(ctx, "bd-derived-close-target", "done", "actor", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertBlocked(t, "bd-derived-close-source", false)
|
||||
assertJournalUpdate(t, "bd-derived-close-source", false)
|
||||
})
|
||||
|
||||
t.Run("deleting blocker updates depender", func(t *testing.T) {
|
||||
mustCreate(t, "bd-derived-delete-source", "bd-derived-delete-target")
|
||||
mustAdd(t, "bd-derived-delete-source", "bd-derived-delete-target", types.DepBlocks)
|
||||
assertBlocked(t, "bd-derived-delete-source", true)
|
||||
clearJournal(t, store)
|
||||
|
||||
if err := store.DeleteIssue(ctx, "bd-derived-delete-target"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertBlocked(t, "bd-derived-delete-source", false)
|
||||
assertJournalUpdate(t, "bd-derived-delete-source", false)
|
||||
})
|
||||
|
||||
t.Run("removing parent blocker updates descendant", func(t *testing.T) {
|
||||
mustCreate(t, "bd-derived-parent", "bd-derived-child", "bd-derived-parent-target")
|
||||
mustAdd(t, "bd-derived-child", "bd-derived-parent", types.DepParentChild)
|
||||
mustAdd(t, "bd-derived-parent", "bd-derived-parent-target", types.DepBlocks)
|
||||
assertBlocked(t, "bd-derived-parent", true)
|
||||
assertBlocked(t, "bd-derived-child", true)
|
||||
clearJournal(t, store)
|
||||
|
||||
if err := store.RemoveDependency(ctx, "bd-derived-parent", "bd-derived-parent-target", "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertBlocked(t, "bd-derived-parent", false)
|
||||
assertBlocked(t, "bd-derived-child", false)
|
||||
assertJournalUpdate(t, "bd-derived-child", false)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEventsJournal_CommentPayloads pins the exact comment record returned by
|
||||
// structured, imported, and audit-comment write paths.
|
||||
func TestEventsJournal_CommentPayloads(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
enableJournalForTest(t, store)
|
||||
issue := &types.Issue{ID: "bd-jc", Title: "comments", IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
if err := store.CreateIssue(ctx, issue, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clearJournal(t, store)
|
||||
|
||||
first, err := store.AddIssueComment(ctx, issue.ID, "alice", "structured")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
importedAt := time.Date(2026, 2, 3, 4, 5, 6, 0, time.UTC)
|
||||
second, err := store.ImportIssueComment(ctx, issue.ID, "bob", "imported", importedAt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.AddComment(ctx, issue.ID, "carol", "audit"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rows, err := store.db.QueryContext(ctx, `SELECT op, comment_json FROM bd_events_journal ORDER BY seq ASC`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var got []issueops.EventComment
|
||||
for rows.Next() {
|
||||
var op string
|
||||
var raw []byte
|
||||
if err := rows.Scan(&op, &raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if op != string(issueops.EventCommentWrite) {
|
||||
t.Fatalf("op = %q, want comment", op)
|
||||
}
|
||||
var comment issueops.EventComment
|
||||
if err := json.Unmarshal(raw, &comment); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = append(got, comment)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("comment rows = %+v, want three", got)
|
||||
}
|
||||
if got[0].ID != first.ID || got[0].Author != "alice" || got[0].Text != "structured" || got[0].Source != "structured" {
|
||||
t.Fatalf("structured comment = %+v", got[0])
|
||||
}
|
||||
if got[1].ID != second.ID || !got[1].CreatedAt.Equal(importedAt) || got[1].Source != "structured" {
|
||||
t.Fatalf("imported comment = %+v, want id %s timestamp %s", got[1], second.ID, importedAt)
|
||||
}
|
||||
if got[2].ID == "" || got[2].Author != "carol" || got[2].Text != "audit" || got[2].Source != "audit" || got[2].CreatedAt.IsZero() {
|
||||
t.Fatalf("audit comment = %+v", got[2])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package dolt
|
|||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -17,7 +19,7 @@ func TestEventsJournal_EmbeddedPlumbing(t *testing.T) {
|
|||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
enableJournalForTest(t)
|
||||
enableJournalForTest(t, store)
|
||||
if _, err := store.db.ExecContext(ctx, "DELETE FROM bd_events_journal"); err != nil {
|
||||
t.Fatalf("clear journal: %v", err)
|
||||
}
|
||||
|
|
@ -60,7 +62,11 @@ func TestEventsJournal_EmbeddedPlumbing(t *testing.T) {
|
|||
}
|
||||
must(rows.Err(), "rows err")
|
||||
|
||||
wantOps := []string{"create", "create", "update", "update", "update", "dep_add", "dep_remove", "close", "delete"}
|
||||
wantOps := []string{
|
||||
"create", "create", "update", "update", "update", "dep_add",
|
||||
"update", // derived is_blocked flip after dependency removal
|
||||
"dep_remove", "close", "delete",
|
||||
}
|
||||
if len(got) != len(wantOps) {
|
||||
t.Fatalf("expected %d journal rows, got %d: %+v", len(wantOps), len(got), got)
|
||||
}
|
||||
|
|
@ -84,7 +90,7 @@ func TestEventsJournal_NoPhantomDeletes(t *testing.T) {
|
|||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
enableJournalForTest(t)
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
|
|
@ -127,3 +133,67 @@ func TestEventsJournal_NoPhantomDeletes(t *testing.T) {
|
|||
t.Fatalf("journal must record deletes only for present ids, got %v", deleted)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournal_RunInTransactionMixedBuckets proves one public
|
||||
// RunInTransaction callback can journal both a durable issue and a wisp. That
|
||||
// plumbing uses separate regular and ignored SQL transactions internally, so
|
||||
// both mutations must still share one ordered journal without contending with
|
||||
// each other on bd_events_seq.
|
||||
func TestEventsJournal_RunInTransactionMixedBuckets(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
regular := &types.Issue{
|
||||
ID: "bd-jtx-regular", Title: "regular", IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
}
|
||||
wisp := &types.Issue{
|
||||
ID: "bd-jtx-wisp", Title: "wisp", IssueType: types.TypeTask, Status: types.StatusOpen, Ephemeral: true,
|
||||
}
|
||||
if err := store.RunInTransaction(ctx, "test: journal mixed buckets", func(tx storage.Transaction) error {
|
||||
return tx.CreateIssues(ctx, []*types.Issue{regular, wisp}, "actor")
|
||||
}); err != nil {
|
||||
t.Fatalf("RunInTransaction mixed journaled create: %v", err)
|
||||
}
|
||||
|
||||
rows := readJournalRows(t, store)
|
||||
if len(rows) != 2 {
|
||||
t.Fatalf("mixed journal rows = %+v, want two creates", rows)
|
||||
}
|
||||
if rows[0].seq != 1 || rows[0].op != "create" || rows[0].id != regular.ID {
|
||||
t.Fatalf("mixed journal row 0 = %+v, want create for %s at seq 1", rows[0], regular.ID)
|
||||
}
|
||||
if rows[1].seq != 2 || rows[1].op != "create" || rows[1].id != wisp.ID {
|
||||
t.Fatalf("mixed journal row 1 = %+v, want create for %s at seq 2", rows[1], wisp.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventsJournal_RunInTransactionWispOnly guards the no-versioned-tables
|
||||
// case. The journal-enabled transaction still has to persist its ignored wisp
|
||||
// and journal row when there is nothing for the following Dolt commit to stage.
|
||||
func TestEventsJournal_RunInTransactionWispOnly(t *testing.T) {
|
||||
store, cleanup := setupTestStore(t)
|
||||
defer cleanup()
|
||||
enableJournalForTest(t, store)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
wisp := &types.Issue{
|
||||
ID: "bd-jtx-wisp-only", Title: "wisp", IssueType: types.TypeTask, Status: types.StatusOpen, Ephemeral: true,
|
||||
}
|
||||
if err := store.RunInTransaction(ctx, "test: journal wisp only", func(tx storage.Transaction) error {
|
||||
return tx.CreateIssue(ctx, wisp, "actor")
|
||||
}); err != nil {
|
||||
t.Fatalf("RunInTransaction journaled wisp create: %v", err)
|
||||
}
|
||||
|
||||
if got, err := store.GetIssue(ctx, wisp.ID); err != nil || !got.Ephemeral {
|
||||
t.Fatalf("journaled wisp persisted = (%+v, %v), want active wisp", got, err)
|
||||
}
|
||||
rows := readJournalRows(t, store)
|
||||
if len(rows) != 1 || rows[0].seq != 1 || rows[0].op != "create" || rows[0].id != wisp.ID {
|
||||
t.Fatalf("wisp-only journal rows = %+v, want one create at seq 1", rows)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -156,15 +156,16 @@ var _ storage.SchemaMigrator = (*DoltStore)(nil)
|
|||
|
||||
// DoltStore implements the Storage interface using Dolt
|
||||
type DoltStore struct {
|
||||
db *sql.DB
|
||||
dbPath string // Path to Dolt data directory (server root, e.g. .beads/dolt/)
|
||||
beadsDir string // Path to .beads directory (parent of dbPath)
|
||||
database string // Database name (subdirectory under dbPath)
|
||||
closed atomic.Bool // Tracks whether Close() has been called
|
||||
connStr string // Connection string for reconnection
|
||||
mu sync.RWMutex // Protects concurrent access
|
||||
readOnly bool // True if opened in read-only mode
|
||||
credentialKey []byte // Random encryption key for federation credentials
|
||||
db *sql.DB
|
||||
dbPath string // Path to Dolt data directory (server root, e.g. .beads/dolt/)
|
||||
beadsDir string // Path to .beads directory (parent of dbPath)
|
||||
database string // Database name (subdirectory under dbPath)
|
||||
closed atomic.Bool // Tracks whether Close() has been called
|
||||
eventsJournalEnabled atomic.Bool
|
||||
connStr string // Connection string for reconnection
|
||||
mu sync.RWMutex // Protects concurrent access
|
||||
readOnly bool // True if opened in read-only mode
|
||||
credentialKey []byte // Random encryption key for federation credentials
|
||||
|
||||
customStatusDetailedCache []types.CustomStatus
|
||||
customStatusCache []string
|
||||
|
|
@ -706,6 +707,8 @@ func (s *DoltStore) withWriteTx(ctx context.Context, fn func(tx *sql.Tx) error)
|
|||
if err != nil {
|
||||
return fmt.Errorf("begin write tx: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
if err := fn(tx); err != nil {
|
||||
return errors.Join(err, tx.Rollback())
|
||||
}
|
||||
|
|
@ -717,6 +720,15 @@ func (s *DoltStore) withWriteTx(ctx context.Context, fn func(tx *sql.Tx) error)
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetEventsJournalEnabled activates the journal for this store instance only.
|
||||
func (s *DoltStore) SetEventsJournalEnabled(enabled bool) {
|
||||
s.eventsJournalEnabled.Store(enabled)
|
||||
}
|
||||
|
||||
func (s *DoltStore) scopeEventsJournalTransaction(tx *sql.Tx) func() {
|
||||
return issueops.ScopeEventsJournalTransaction(tx, s.eventsJournalEnabled.Load())
|
||||
}
|
||||
|
||||
// uncommitted implicit transaction that Dolt rolls back on connection close,
|
||||
// causing silent data loss for callers that do not use db.BeginTx themselves.
|
||||
func (s *DoltStore) execContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
|
|
|
|||
|
|
@ -98,42 +98,67 @@ func (s *DoltStore) runDoltTransaction(ctx context.Context, commitMsg string, fn
|
|||
return fmt.Errorf("failed to begin regular tx: %w", err)
|
||||
}
|
||||
|
||||
ignoredDB, ignoredConn, ignoredTx, err := s.beginIgnoredTxOnBranch(ctx, currentBranch)
|
||||
if err != nil {
|
||||
_ = regularTx.Rollback()
|
||||
return err
|
||||
// The journal counter and rows must commit in the same SQL transaction as
|
||||
// every mutation they describe. In journal mode, use the pinned transaction
|
||||
// for both regular and dolt-ignored tables; otherwise a mixed durable+wisp
|
||||
// callback makes the two transactions contend with each other on
|
||||
// bd_events_seq, and the ignored commit can fail after the regular side has
|
||||
// already committed. The default journal-off path keeps the established
|
||||
// split transactions.
|
||||
journalEnabled := s.eventsJournalEnabled.Load()
|
||||
ignoredTx := regularTx
|
||||
var ignoredDB *sql.DB
|
||||
var ignoredConn *sql.Conn
|
||||
if !journalEnabled {
|
||||
ignoredDB, ignoredConn, ignoredTx, err = s.beginIgnoredTxOnBranch(ctx, currentBranch)
|
||||
if err != nil {
|
||||
_ = regularTx.Rollback()
|
||||
return err
|
||||
}
|
||||
defer ignoredDB.Close()
|
||||
defer ignoredConn.Close()
|
||||
}
|
||||
defer ignoredDB.Close()
|
||||
defer ignoredConn.Close()
|
||||
clearRegularJournalScope := issueops.ScopeEventsJournalTransaction(regularTx, journalEnabled)
|
||||
defer clearRegularJournalScope()
|
||||
|
||||
tx := &doltTransaction{regularTx: regularTx, ignoredTx: ignoredTx, store: s}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
_ = regularTx.Rollback()
|
||||
_ = ignoredTx.Rollback()
|
||||
if !journalEnabled {
|
||||
_ = ignoredTx.Rollback()
|
||||
}
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
_ = regularTx.Rollback()
|
||||
_ = ignoredTx.Rollback()
|
||||
if !journalEnabled {
|
||||
_ = ignoredTx.Rollback()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := regularTx.Commit(); err != nil {
|
||||
_ = ignoredTx.Rollback()
|
||||
if !journalEnabled {
|
||||
_ = ignoredTx.Rollback()
|
||||
}
|
||||
return fmt.Errorf("sql commit (regular): %w", err)
|
||||
}
|
||||
|
||||
if err := versioncontrolops.StageAndCommit(ctx, conn, tx.dirty.DirtyTables(), commitMsg, s.commitAuthorString()); err != nil {
|
||||
_ = ignoredTx.Rollback()
|
||||
if !journalEnabled {
|
||||
_ = ignoredTx.Rollback()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ignoredTx.Commit(); err != nil {
|
||||
return fmt.Errorf("sql commit (ignored, regular already committed): %w", err)
|
||||
if !journalEnabled {
|
||||
if err := ignoredTx.Commit(); err != nil {
|
||||
return fmt.Errorf("sql commit (ignored, regular already committed): %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -784,10 +809,14 @@ func (t *doltTransaction) AddLabel(ctx context.Context, issueID, label, actor st
|
|||
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf(`
|
||||
INSERT IGNORE INTO %s (issue_id, label) VALUES (?, ?)
|
||||
`, table), issueID, label)
|
||||
if err == nil {
|
||||
t.dirty.MarkDirty(table)
|
||||
if err != nil {
|
||||
return wrapExecError("add label in tx", err)
|
||||
}
|
||||
return wrapExecError("add label in tx", err)
|
||||
t.dirty.MarkDirty(table)
|
||||
if err := issueops.RecordEventInTx(ctx, t.txFor(table), issueops.EventUpdate, issueID); err != nil {
|
||||
return wrapExecError("journal label add in tx", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *doltTransaction) GetLabels(ctx context.Context, issueID string) ([]string, error) {
|
||||
|
|
@ -824,10 +853,14 @@ func (t *doltTransaction) RemoveLabel(ctx context.Context, issueID, label, actor
|
|||
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf(`
|
||||
DELETE FROM %s WHERE issue_id = ? AND label = ?
|
||||
`, table), issueID, label)
|
||||
if err == nil {
|
||||
t.dirty.MarkDirty(table)
|
||||
if err != nil {
|
||||
return wrapExecError("remove label in tx", err)
|
||||
}
|
||||
return wrapExecError("remove label in tx", err)
|
||||
t.dirty.MarkDirty(table)
|
||||
if err := issueops.RecordEventInTx(ctx, t.txFor(table), issueops.EventUpdate, issueID); err != nil {
|
||||
return wrapExecError("journal label remove in tx", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetConfig sets a config value within the transaction
|
||||
|
|
|
|||
|
|
@ -191,6 +191,8 @@ func (s *DoltStore) updateWisp(ctx context.Context, id string, updates map[strin
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := issueops.UpdateIssueInTx(ctx, tx, id, updates, actor); err != nil {
|
||||
|
|
@ -208,6 +210,8 @@ func (s *DoltStore) closeWisp(ctx context.Context, id string, reason string, act
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := issueops.CloseIssueInTx(ctx, tx, id, reason, actor, session); err != nil {
|
||||
|
|
@ -223,12 +227,17 @@ func (s *DoltStore) deleteWisp(ctx context.Context, id string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
affectedIssues, affectedWisps, aerr := issueops.AffectedByDeletionInTx(ctx, tx, nil, []string{id})
|
||||
if aerr != nil {
|
||||
return fmt.Errorf("affected by wisp delete for %s: %w", id, aerr)
|
||||
}
|
||||
if err := issueops.RecordDependencyRemovalsForIssuesInTx(ctx, tx, []string{id}); err != nil {
|
||||
return fmt.Errorf("journal dependency removals for wisp %s: %w", id, err)
|
||||
}
|
||||
|
||||
result, err := tx.ExecContext(ctx, "DELETE FROM wisps WHERE id = ?", id)
|
||||
if err != nil {
|
||||
|
|
@ -242,6 +251,9 @@ func (s *DoltStore) deleteWisp(ctx context.Context, id string) error {
|
|||
if rows == 0 {
|
||||
return fmt.Errorf("wisp not found: %s", id)
|
||||
}
|
||||
if err := issueops.RecordDeleteInTx(ctx, tx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := issueops.DeleteWispFromDependenciesInTx(ctx, tx, id); err != nil {
|
||||
return err
|
||||
|
|
@ -296,12 +308,25 @@ func (s *DoltStore) deleteWispBatchTx(ctx context.Context, ids []string) (int, e
|
|||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
actualIDs, err := issueops.ExistingIssueIDsInTableInTx(ctx, tx, "wisps", ids)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("resolve existing wisps for batch delete: %w", err)
|
||||
}
|
||||
if len(actualIDs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
ids = actualIDs
|
||||
affectedIssues, affectedWisps, aerr := issueops.AffectedByDeletionInTx(ctx, tx, nil, ids)
|
||||
if aerr != nil {
|
||||
return 0, fmt.Errorf("affected by batched wisp delete: %w", aerr)
|
||||
}
|
||||
if err := issueops.RecordDependencyRemovalsForIssuesInTx(ctx, tx, ids); err != nil {
|
||||
return 0, fmt.Errorf("journal dependency removals for batched wisp delete: %w", err)
|
||||
}
|
||||
|
||||
inClause, args := doltBuildSQLInClause(ids)
|
||||
|
||||
|
|
@ -313,6 +338,11 @@ func (s *DoltStore) deleteWispBatchTx(ctx context.Context, ids []string) (int, e
|
|||
return 0, fmt.Errorf("failed to batch delete wisps: %w", err)
|
||||
}
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
for _, id := range ids {
|
||||
if err := issueops.RecordDeleteInTx(ctx, tx, id); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := issueops.DeleteWispsFromDependenciesInTx(ctx, tx, ids); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -337,6 +367,8 @@ func (s *DoltStore) claimWisp(ctx context.Context, id string, actor string) erro
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := issueops.ClaimIssueInTx(ctx, tx, id, actor); err != nil {
|
||||
|
|
@ -518,6 +550,8 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
clearJournalScope := s.scopeEventsJournalTransaction(tx)
|
||||
defer clearJournalScope()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
// Cycle detection for blocking dependency types: check if adding this edge
|
||||
|
|
@ -553,6 +587,9 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
|
|||
`, targetCol), metadata, dep.IssueID, dep.DependsOnID); err != nil {
|
||||
return fmt.Errorf("failed to update wisp dependency metadata: %w", err)
|
||||
}
|
||||
if err := issueops.RecordDepEventInTx(ctx, tx, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
return wrapTransactionError("commit add wisp dependency", tx.Commit())
|
||||
}
|
||||
return fmt.Errorf("dependency %s -> %s already exists with type %q (requested %q); remove it first with 'bd dep remove' then re-add",
|
||||
|
|
@ -571,7 +608,6 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
|
|||
`, targetCol), depid.New(dep.IssueID, dep.DependsOnID), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
|
||||
return fmt.Errorf("failed to add wisp dependency: %w", err)
|
||||
}
|
||||
|
||||
affectedIssues, affectedWisps, aerr := issueops.AffectedByDepChangeForWispInTx(ctx, tx, dep.IssueID, dep.DependsOnID, dep.Type)
|
||||
if aerr != nil {
|
||||
return fmt.Errorf("affected by add wisp dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, aerr)
|
||||
|
|
@ -579,6 +615,9 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
|
|||
if err := issueops.RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("recompute is_blocked after add wisp dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
|
||||
}
|
||||
if err := issueops.RecordDepEventInTx(ctx, tx, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wrapTransactionError("commit add wisp dependency", tx.Commit())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ func (r *dependencySQLRepositoryImpl) Insert(ctx context.Context, dep *types.Dep
|
|||
); err != nil {
|
||||
return fmt.Errorf("db: DependencySQLRepository.Insert: refresh metadata: %w", err)
|
||||
}
|
||||
return nil
|
||||
return issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
return &domain.DependencyTypeConflictError{
|
||||
IssueID: dep.IssueID,
|
||||
|
|
@ -125,12 +125,6 @@ func (r *dependencySQLRepositoryImpl) Insert(ctx context.Context, dep *types.Dep
|
|||
return fmt.Errorf("db: DependencySQLRepository.Insert: %w", err)
|
||||
}
|
||||
|
||||
// Journal the dependency add in the same transaction (new-edge path only;
|
||||
// the same-type metadata refresh above returns earlier without a new edge).
|
||||
if err := issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// is_blocked maintenance mirrors the classic AddDependencyInTx flow
|
||||
// (issueops/dependencies.go): the affected set expands the source by its
|
||||
// parent-child descendants (plus, for parent-child edges, waiters on the
|
||||
|
|
@ -160,12 +154,12 @@ func (r *dependencySQLRepositoryImpl) Insert(ctx context.Context, dep *types.Dep
|
|||
if err := issueops.RecomputeIsBlockedInTx(ctx, r.runner, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("db: DependencySQLRepository.Insert: recompute is_blocked: %w", err)
|
||||
}
|
||||
return nil
|
||||
return issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
if err := issueops.MarkIsBlockedInTx(ctx, r.runner, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("db: DependencySQLRepository.Insert: mark is_blocked (affected): %w", err)
|
||||
}
|
||||
return nil
|
||||
return issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
|
||||
// markDirectBlockedSource mirrors issueops.markDirectBlockingDependencySourceInTx:
|
||||
|
|
@ -210,12 +204,12 @@ func (r *dependencySQLRepositoryImpl) Delete(ctx context.Context, issueID, depen
|
|||
}
|
||||
table := pickDepTable(opts.UseWispsTable)
|
||||
|
||||
var depType string
|
||||
var depType, metadata string
|
||||
//nolint:gosec // G201: table and depTargetExpr are hardcoded constants
|
||||
err := r.runner.QueryRowContext(ctx,
|
||||
fmt.Sprintf("SELECT type FROM %s WHERE issue_id = ? AND %s = ?", table, depTargetExpr),
|
||||
fmt.Sprintf("SELECT type, metadata FROM %s WHERE issue_id = ? AND %s = ?", table, depTargetExpr),
|
||||
issueID, dependsOnID,
|
||||
).Scan(&depType)
|
||||
).Scan(&depType, &metadata)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return domain.DepDeleteResult{Found: false}, nil
|
||||
|
|
@ -231,12 +225,6 @@ func (r *dependencySQLRepositoryImpl) Delete(ctx context.Context, issueID, depen
|
|||
return domain.DepDeleteResult{}, fmt.Errorf("db: DependencySQLRepository.Delete: %s -> %s: %w", issueID, dependsOnID, err)
|
||||
}
|
||||
|
||||
// Journal the dependency remove in the same transaction (only reached when
|
||||
// the edge existed — the no-match path returned earlier).
|
||||
if err := issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepRemove, issueID, depType, dependsOnID); err != nil {
|
||||
return domain.DepDeleteResult{}, err
|
||||
}
|
||||
|
||||
dt := types.DependencyType(depType)
|
||||
var affectedIssues, affectedWisps []string
|
||||
var aerr error
|
||||
|
|
@ -252,6 +240,9 @@ func (r *dependencySQLRepositoryImpl) Delete(ctx context.Context, issueID, depen
|
|||
return domain.DepDeleteResult{}, fmt.Errorf("db: DependencySQLRepository.Delete: recompute is_blocked: %w", err)
|
||||
}
|
||||
|
||||
if err := issueops.RecordDepEventInTx(ctx, r.runner, issueops.EventDepRemove, issueID, depType, dependsOnID, metadata); err != nil {
|
||||
return domain.DepDeleteResult{}, err
|
||||
}
|
||||
return domain.DepDeleteResult{Found: true, Type: dt, DependsOnID: dependsOnID}, nil
|
||||
}
|
||||
|
||||
|
|
@ -659,6 +650,9 @@ func (r *dependencySQLRepositoryImpl) DeleteAllForIDs(ctx context.Context, ids [
|
|||
args = append(args, id)
|
||||
}
|
||||
ph := strings.Join(placeholders, ",")
|
||||
if err := issueops.RecordDependencyRemovalsForTableInTx(ctx, r.runner, table, batch); err != nil {
|
||||
return total, fmt.Errorf("db: DependencySQLRepository.DeleteAllForIDs journal from %s: %w", table, err)
|
||||
}
|
||||
//nolint:gosec // G201: table is one of two hardcoded constants; ? placeholders only.
|
||||
res, err := r.runner.ExecContext(ctx,
|
||||
fmt.Sprintf("DELETE FROM %s WHERE issue_id IN (%s) OR %s IN (%s)", table, ph, issueops.DepTargetExpr, ph),
|
||||
|
|
|
|||
|
|
@ -67,11 +67,14 @@ func (r *issueSQLRepositoryImpl) Insert(ctx context.Context, issue *types.Issue,
|
|||
if err := insertIssueRow(ctx, r.runner, table, issue); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.events.Record(ctx, domain.Event{
|
||||
if err := r.events.Record(ctx, domain.Event{
|
||||
IssueID: issue.ID,
|
||||
Type: types.EventCreated,
|
||||
Actor: actor,
|
||||
}, domain.RecordEventOpts{UseWispsTable: opts.UseWispsTable})
|
||||
}, domain.RecordEventOpts{UseWispsTable: opts.UseWispsTable}); err != nil {
|
||||
return err
|
||||
}
|
||||
return issueops.RecordEventInTx(ctx, r.runner, issueops.EventCreate, issue.ID)
|
||||
}
|
||||
|
||||
func (r *issueSQLRepositoryImpl) InsertBatch(ctx context.Context, issues []*types.Issue, actor string, opts domain.InsertIssueOpts) error {
|
||||
|
|
@ -190,7 +193,7 @@ func (r *issueSQLRepositoryImpl) Update(ctx context.Context, id string, updates
|
|||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return issueops.RecordEventInTx(ctx, r.runner, issueops.EventUpdate, id)
|
||||
}
|
||||
|
||||
func coerceStatus(v any) types.Status {
|
||||
|
|
@ -275,6 +278,9 @@ func (r *issueSQLRepositoryImpl) Claim(ctx context.Context, id, actor string, op
|
|||
}, domain.RecordEventOpts{UseWispsTable: opts.UseWispsTable}); err != nil {
|
||||
return domain.ClaimRowResult{}, fmt.Errorf("db: Claim %s: record event: %w", id, err)
|
||||
}
|
||||
if err := issueops.RecordEventInTx(ctx, r.runner, issueops.EventUpdate, id); err != nil {
|
||||
return domain.ClaimRowResult{}, fmt.Errorf("db: Claim %s: record journal: %w", id, err)
|
||||
}
|
||||
|
||||
return domain.ClaimRowResult{
|
||||
Updated: true,
|
||||
|
|
@ -654,6 +660,9 @@ func (r *issueSQLRepositoryImpl) Delete(ctx context.Context, id string, opts dom
|
|||
if opts.UseWispsTable {
|
||||
table = "wisps"
|
||||
}
|
||||
if err := issueops.RecordDependencyRemovalsForIssuesInTx(ctx, r.runner, []string{id}); err != nil {
|
||||
return fmt.Errorf("db: IssueSQLRepository.Delete journal dependencies for %s: %w", id, err)
|
||||
}
|
||||
//nolint:gosec // G201: table is a hardcoded constant.
|
||||
res, err := r.runner.ExecContext(ctx, fmt.Sprintf("DELETE FROM %s WHERE id = ?", table), id)
|
||||
if err != nil {
|
||||
|
|
@ -666,7 +675,7 @@ func (r *issueSQLRepositoryImpl) Delete(ctx context.Context, id string, opts dom
|
|||
if rows == 0 {
|
||||
return fmt.Errorf("issue not found: %s", id)
|
||||
}
|
||||
return nil
|
||||
return issueops.RecordDeleteInTx(ctx, r.runner, id)
|
||||
}
|
||||
|
||||
func (r *issueSQLRepositoryImpl) DeleteByIDs(ctx context.Context, ids []string, opts domain.IssueTableOpts) (int, error) {
|
||||
|
|
@ -677,13 +686,23 @@ func (r *issueSQLRepositoryImpl) DeleteByIDs(ctx context.Context, ids []string,
|
|||
if opts.UseWispsTable {
|
||||
table = "wisps"
|
||||
}
|
||||
actualIDs, err := issueops.ExistingIssueIDsInTableInTx(ctx, r.runner, table, ids)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("db: IssueSQLRepository.DeleteByIDs resolve existing rows from %s: %w", table, err)
|
||||
}
|
||||
if len(actualIDs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
if err := issueops.RecordDependencyRemovalsForIssuesInTx(ctx, r.runner, actualIDs); err != nil {
|
||||
return 0, fmt.Errorf("db: IssueSQLRepository.DeleteByIDs journal dependencies from %s: %w", table, err)
|
||||
}
|
||||
total := 0
|
||||
for start := 0; start < len(ids); start += deleteBatchSize {
|
||||
for start := 0; start < len(actualIDs); start += deleteBatchSize {
|
||||
end := start + deleteBatchSize
|
||||
if end > len(ids) {
|
||||
end = len(ids)
|
||||
if end > len(actualIDs) {
|
||||
end = len(actualIDs)
|
||||
}
|
||||
batch := ids[start:end]
|
||||
batch := actualIDs[start:end]
|
||||
placeholders := make([]string, len(batch))
|
||||
args := make([]any, len(batch))
|
||||
for i, id := range batch {
|
||||
|
|
@ -703,6 +722,11 @@ func (r *issueSQLRepositoryImpl) DeleteByIDs(ctx context.Context, ids []string,
|
|||
}
|
||||
total += int(n)
|
||||
}
|
||||
for _, id := range actualIDs {
|
||||
if err := issueops.RecordDeleteInTx(ctx, r.runner, id); err != nil {
|
||||
return total, fmt.Errorf("db: IssueSQLRepository.DeleteByIDs journal delete %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
28
internal/storage/domain/db/journal.go
Normal file
28
internal/storage/domain/db/journal.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
)
|
||||
|
||||
func NewEventsJournalSQLRepository(runner Runner) domain.EventsJournalSQLRepository {
|
||||
return &eventsJournalSQLRepository{runner: runner}
|
||||
}
|
||||
|
||||
type eventsJournalSQLRepository struct {
|
||||
runner Runner
|
||||
}
|
||||
|
||||
var _ domain.EventsJournalSQLRepository = (*eventsJournalSQLRepository)(nil)
|
||||
|
||||
func (r *eventsJournalSQLRepository) Read(ctx context.Context, since int64, limit int) ([]storage.EventsJournalRow, error) {
|
||||
return issueops.ReadEventsInTx(ctx, r.runner, since, limit)
|
||||
}
|
||||
|
||||
func (r *eventsJournalSQLRepository) Prune(ctx context.Context, before int64, retainDays, retainRows int) (int64, error) {
|
||||
return issueops.PruneEventsInTx(ctx, r.runner, before, retainDays, retainRows, time.Now().UTC())
|
||||
}
|
||||
|
|
@ -22,10 +22,10 @@ type journalRow struct {
|
|||
// tests (the table is dolt-ignored, so DOLT_RESET does not clear it), and turns
|
||||
// it back off on cleanup.
|
||||
func (s *testSuite) enableJournalForTest() {
|
||||
issueops.SetJournalEnabled(true)
|
||||
s.journalEnabled = true
|
||||
_, err := s.Runner().ExecContext(s.Ctx(), "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
s.T().Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
s.T().Cleanup(func() { s.journalEnabled = false })
|
||||
}
|
||||
|
||||
func (s *testSuite) readJournal() []journalRow {
|
||||
|
|
@ -84,7 +84,11 @@ func (s *testSuite) TestEventsJournal_UOWPlumbing() {
|
|||
s.Require().NoError(ir.Delete(ctx, "bd-mj-2", domain.IssueTableOpts{}))
|
||||
|
||||
got := s.readJournal()
|
||||
wantOps := []string{"create", "create", "update", "update", "update", "dep_add", "dep_remove", "close", "delete"}
|
||||
wantOps := []string{
|
||||
"create", "create", "update", "update", "update", "dep_add",
|
||||
"update", // derived is_blocked flip after dependency removal
|
||||
"dep_remove", "close", "delete",
|
||||
}
|
||||
s.Require().Len(got, len(wantOps), "journal rows: %+v", got)
|
||||
|
||||
var prev int64
|
||||
|
|
@ -100,12 +104,16 @@ func (s *testSuite) TestEventsJournal_UOWPlumbing() {
|
|||
s.Require().NotNil(got[5].Dep)
|
||||
s.Equal(string(types.DepBlocks), got[5].Dep.Kind)
|
||||
s.Equal("bd-mj-2", got[5].Dep.Target)
|
||||
// Dependency removal changes the persisted readiness projection before its
|
||||
// edge delta, so cursor consumers receive the derived source update too.
|
||||
s.Require().True(got[6].HasIssue)
|
||||
s.False(got[6].Issue.IsBlocked)
|
||||
// dep_remove carries the edge details.
|
||||
s.Require().NotNil(got[6].Dep)
|
||||
s.Equal("bd-mj-2", got[6].Dep.Target)
|
||||
s.Require().NotNil(got[7].Dep)
|
||||
s.Equal("bd-mj-2", got[7].Dep.Target)
|
||||
// delete carries a null issue payload.
|
||||
s.Equal("delete", got[8].Op)
|
||||
s.False(got[8].HasIssue, "delete row must have null issue")
|
||||
s.Equal("delete", got[9].Op)
|
||||
s.False(got[9].HasIssue, "delete row must have null issue")
|
||||
}
|
||||
|
||||
// TestEventsJournal_CascadeDelete asserts a cascade delete journals every
|
||||
|
|
@ -140,6 +148,49 @@ func (s *testSuite) TestEventsJournal_CascadeDelete() {
|
|||
s.True(deleted["bd-cd-child"], "cascade-deleted child must be journaled, got %v", deleted)
|
||||
}
|
||||
|
||||
// TestEventsJournal_DeleteRelationshipDeltas proves a cursor consumer sees
|
||||
// every edge removal before the nodes disappear, including both incoming and
|
||||
// outgoing relationships in a multi-node UOW delete.
|
||||
func (s *testSuite) TestEventsJournal_DeleteRelationshipDeltas() {
|
||||
s.enableJournalForTest()
|
||||
ctx := s.Ctx()
|
||||
ir := s.issueRepo()
|
||||
dr := s.depRepo()
|
||||
|
||||
for _, id := range []string{"bd-del-center", "bd-del-in", "bd-del-out"} {
|
||||
s.Require().NoError(ir.Insert(ctx, newTestIssue(id, "t"), "actor", domain.InsertIssueOpts{}))
|
||||
}
|
||||
s.Require().NoError(dr.Insert(ctx, &types.Dependency{
|
||||
IssueID: "bd-del-center", DependsOnID: "bd-del-out", Type: types.DepBlocks,
|
||||
}, "actor", domain.DepInsertOpts{}))
|
||||
s.Require().NoError(dr.Insert(ctx, &types.Dependency{
|
||||
IssueID: "bd-del-in", DependsOnID: "bd-del-center", Type: types.DepRelated,
|
||||
}, "actor", domain.DepInsertOpts{}))
|
||||
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = s.issueUseCase().DeleteIssues(ctx, domain.DeleteIssuesParams{
|
||||
IDs: []string{"bd-del-center", "bd-del-in", "bd-del-out"},
|
||||
}, "actor")
|
||||
s.Require().NoError(err)
|
||||
|
||||
got := s.readJournal()
|
||||
s.Require().Len(got, 5, "two edge removals followed by three node deletes: %+v", got)
|
||||
for i := 0; i < 2; i++ {
|
||||
s.Equal("dep_remove", got[i].Op, "edge delta %d must precede node deletes", i)
|
||||
}
|
||||
for i := 2; i < len(got); i++ {
|
||||
s.Equal("delete", got[i].Op, "row %d", i)
|
||||
}
|
||||
s.Require().NotNil(got[0].Dep)
|
||||
s.Require().NotNil(got[1].Dep)
|
||||
s.Equal("bd-del-center", got[0].IssueID)
|
||||
s.Equal("bd-del-out", got[0].Dep.Target)
|
||||
s.Equal("bd-del-in", got[1].IssueID)
|
||||
s.Equal("bd-del-center", got[1].Dep.Target)
|
||||
}
|
||||
|
||||
// TestEventsJournal_NoPhantomDeletes asserts DeleteByIDs journals a delete
|
||||
// only for ids that actually removed a row — never a phantom delete for an id
|
||||
// that matched nothing (the batched DELETE reports only a per-batch total).
|
||||
|
|
@ -171,7 +222,7 @@ func (s *testSuite) TestEventsJournal_NoPhantomDeletes() {
|
|||
// TestEventsJournal_DisabledWritesNothing asserts the default-off knob writes
|
||||
// no rows.
|
||||
func (s *testSuite) TestEventsJournal_DisabledWritesNothing() {
|
||||
issueops.SetJournalEnabled(false)
|
||||
s.journalEnabled = false
|
||||
ctx := s.Ctx()
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
|
@ -182,3 +233,50 @@ func (s *testSuite) TestEventsJournal_DisabledWritesNothing() {
|
|||
s.Require().NoError(s.Runner().QueryRowContext(ctx, "SELECT COUNT(*) FROM bd_events_journal").Scan(&n))
|
||||
s.Equal(0, n, "disabled journal must write nothing")
|
||||
}
|
||||
|
||||
// TestEventsJournal_UOWDependencySnapshotsFollowBlockedState mirrors the
|
||||
// direct-store guard at the UOW repository seam. The event snapshot must be
|
||||
// taken after is_blocked maintenance, including same-type metadata refreshes.
|
||||
func (s *testSuite) TestEventsJournal_UOWDependencySnapshotsFollowBlockedState() {
|
||||
s.enableJournalForTest()
|
||||
ctx := s.Ctx()
|
||||
ir := s.issueRepo()
|
||||
dr := s.depRepo()
|
||||
for _, id := range []string{"bd-uj-source", "bd-uj-target"} {
|
||||
s.Require().NoError(ir.Insert(ctx, newTestIssue(id, id), "actor", domain.InsertIssueOpts{}))
|
||||
}
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
||||
add := func(metadata string) {
|
||||
s.T().Helper()
|
||||
s.Require().NoError(dr.Insert(ctx, &types.Dependency{
|
||||
IssueID: "bd-uj-source", DependsOnID: "bd-uj-target", Type: types.DepBlocks, Metadata: metadata,
|
||||
}, "actor", domain.DepInsertOpts{}))
|
||||
}
|
||||
last := func() journalRow {
|
||||
s.T().Helper()
|
||||
rows := s.readJournal()
|
||||
s.Require().NotEmpty(rows)
|
||||
return rows[len(rows)-1]
|
||||
}
|
||||
|
||||
add(`{"revision":"A"}`)
|
||||
row := last()
|
||||
s.Require().True(row.Issue.IsBlocked)
|
||||
s.Require().NotNil(row.Dep)
|
||||
s.Equal(`{"revision":"A"}`, row.Dep.Metadata)
|
||||
|
||||
add(`{"revision":"B"}`)
|
||||
row = last()
|
||||
s.Require().True(row.Issue.IsBlocked)
|
||||
s.Require().NotNil(row.Dep)
|
||||
s.Equal(`{"revision":"B"}`, row.Dep.Metadata)
|
||||
|
||||
_, err = dr.Delete(ctx, "bd-uj-source", "bd-uj-target", "actor", domain.DepInsertOpts{})
|
||||
s.Require().NoError(err)
|
||||
row = last()
|
||||
s.Require().False(row.Issue.IsBlocked)
|
||||
s.Require().NotNil(row.Dep)
|
||||
s.Equal(`{"revision":"B"}`, row.Dep.Metadata)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,19 +34,20 @@ func TestEveryRepositoryMutatorJournals(t *testing.T) {
|
|||
// The staleness check below fails if any of these stops being a bead mutator,
|
||||
// so a rename or refactor cannot silently strand an exemption.
|
||||
exempt := map[string]string{
|
||||
"DeleteAllForIDs": "bulk edge/label cleanup runs under a parent issue delete that already journals each removed bead",
|
||||
"markDirectBlockedSource": "maintains the derived is_blocked column as a side effect of a journaled dependency insert",
|
||||
"labelSQLRepositoryImpl.DeleteAllForIDs": "bulk label cleanup runs under a parent issue delete; the surviving journal record is the node delete",
|
||||
"dependencySQLRepositoryImpl.markDirectBlockedSource": "maintains the derived is_blocked column as a side effect of a journaled dependency insert",
|
||||
}
|
||||
|
||||
// Direct emit helpers and the issueops functions that journal internally.
|
||||
emitCalls := map[string]bool{
|
||||
"RecordEventInTx": true,
|
||||
"RecordDeleteInTx": true,
|
||||
"RecordDepEventInTx": true,
|
||||
"CloseIssueInTx": true,
|
||||
"ReopenIssueInTx": true,
|
||||
"ClaimReadyIssueInTx": true,
|
||||
"ReclaimExpiredLeasesInTx": true,
|
||||
"RecordEventInTx": true,
|
||||
"RecordDeleteInTx": true,
|
||||
"RecordDepEventInTx": true,
|
||||
"RecordDependencyRemovalsForTableInTx": true,
|
||||
"CloseIssueInTx": true,
|
||||
"ReopenIssueInTx": true,
|
||||
"ClaimReadyIssueInTx": true,
|
||||
"ReclaimExpiredLeasesInTx": true,
|
||||
}
|
||||
|
||||
fns, err := journalscan.ParsePackage(".")
|
||||
|
|
@ -74,11 +75,12 @@ func TestEveryRepositoryMutatorJournals(t *testing.T) {
|
|||
if !beadReceivers[f.Recv] || !beadDML[name] {
|
||||
continue
|
||||
}
|
||||
if reason, ok := exempt[f.Name]; ok {
|
||||
exemptKey := f.Recv + "." + f.Name
|
||||
if reason, ok := exempt[exemptKey]; ok {
|
||||
if reason == "" {
|
||||
t.Errorf("%s has an empty exemption reason", f.Name)
|
||||
t.Errorf("%s has an empty exemption reason", exemptKey)
|
||||
}
|
||||
seenExempt[f.Name] = true
|
||||
seenExempt[exemptKey] = true
|
||||
continue
|
||||
}
|
||||
checked++
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import (
|
|||
// up in dolt_status — the signal bd's auto-commit path uses to decide what to
|
||||
// stage/commit.
|
||||
func (s *testSuite) TestEventsJournal_IsDoltIgnored() {
|
||||
s.journalEnabled = true
|
||||
defer func() { s.journalEnabled = false }()
|
||||
ctx := s.Ctx()
|
||||
|
||||
var ignored bool
|
||||
|
|
@ -20,8 +22,6 @@ func (s *testSuite) TestEventsJournal_IsDoltIgnored() {
|
|||
|
||||
// Write a journal row, then confirm the ignored table never surfaces in
|
||||
// dolt_status (which is what the auto-commit/add path consults).
|
||||
issueops.SetJournalEnabled(true)
|
||||
defer issueops.SetJournalEnabled(false)
|
||||
s.Require().NoError(s.issueRepo().Insert(ctx, newTestIssue("bd-di-1", "t"), "actor", domain.InsertIssueOpts{}))
|
||||
|
||||
var statusCount int
|
||||
|
|
@ -35,9 +35,9 @@ func (s *testSuite) TestEventsJournal_IsDoltIgnored() {
|
|||
// git-untracked state), because the table is dolt-ignored. If reset destroyed
|
||||
// them, the outbox design would be unsound — this is the guard for that.
|
||||
func (s *testSuite) TestEventsJournal_SurvivesCommitAndReset() {
|
||||
s.journalEnabled = true
|
||||
defer func() { s.journalEnabled = false }()
|
||||
ctx := s.Ctx()
|
||||
issueops.SetJournalEnabled(true)
|
||||
defer issueops.SetJournalEnabled(false)
|
||||
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
|
@ -69,9 +69,9 @@ func (s *testSuite) TestEventsJournal_SurvivesCommitAndReset() {
|
|||
// mutation share the transaction even though the journal table is ignored: a
|
||||
// rollback drops both, a commit keeps both.
|
||||
func (s *testSuite) TestEventsJournal_TxAtomicity() {
|
||||
s.journalEnabled = true
|
||||
defer func() { s.journalEnabled = false }()
|
||||
ctx := s.Ctx()
|
||||
issueops.SetJournalEnabled(true)
|
||||
defer issueops.SetJournalEnabled(false)
|
||||
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
)
|
||||
|
||||
|
|
@ -39,9 +38,9 @@ func isSerializationFailure(err error) bool {
|
|||
// consumer would skip the lower seq. The counter converts that silent skip into
|
||||
// a detected conflict + retry that preserves commit order.
|
||||
func (s *testSuite) TestEventsJournal_CommitOrderedGaplessSeq() {
|
||||
s.journalEnabled = true
|
||||
s.T().Cleanup(func() { s.journalEnabled = false })
|
||||
ctx := s.Ctx()
|
||||
issueops.SetJournalEnabled(true)
|
||||
s.T().Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
||||
|
|
@ -102,9 +101,9 @@ func (s *testSuite) TestEventsJournal_CommitOrderedGaplessSeq() {
|
|||
// seq<N is already committed) through concurrent writers, each retrying its own
|
||||
// serialization losses — mirroring what withRetryTx / uow.RunTx do in production.
|
||||
func (s *testSuite) TestEventsJournal_ConcurrentWritersGaplessNoDup() {
|
||||
s.journalEnabled = true
|
||||
s.T().Cleanup(func() { s.journalEnabled = false })
|
||||
ctx := s.Ctx()
|
||||
issueops.SetJournalEnabled(true)
|
||||
s.T().Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
_, err := s.Runner().ExecContext(ctx, "DELETE FROM bd_events_journal")
|
||||
s.Require().NoError(err)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/storage/schema"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
)
|
||||
|
|
@ -19,6 +20,7 @@ type testSuite struct {
|
|||
db *sql.DB
|
||||
dbName string
|
||||
baselineCommit string
|
||||
journalEnabled bool
|
||||
}
|
||||
|
||||
func (s *testSuite) SetupSuite() {
|
||||
|
|
@ -79,6 +81,7 @@ func (s *testSuite) TearDownSuite() {
|
|||
}
|
||||
|
||||
func (s *testSuite) SetupTest() {
|
||||
s.journalEnabled = false
|
||||
_, err := s.db.ExecContext(context.Background(), "CALL DOLT_RESET('--hard', ?)", s.baselineCommit)
|
||||
s.Require().NoError(err, "reset to baseline %s", s.baselineCommit)
|
||||
}
|
||||
|
|
@ -88,7 +91,7 @@ func (s *testSuite) Runner() Runner {
|
|||
}
|
||||
|
||||
func (s *testSuite) Ctx() context.Context {
|
||||
return context.Background()
|
||||
return issueops.WithEventsJournal(context.Background(), s.journalEnabled)
|
||||
}
|
||||
|
||||
var suffixLetters = []rune("abcdefghijklmnopqrstuvwxyz")
|
||||
|
|
|
|||
39
internal/storage/domain/journal.go
Normal file
39
internal/storage/domain/journal.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// EventsJournalSQLRepository is the transaction-bound persistence seam for
|
||||
// reading and pruning the durable mutation journal.
|
||||
type EventsJournalSQLRepository interface {
|
||||
Read(ctx context.Context, since int64, limit int) ([]storage.EventsJournalRow, error)
|
||||
Prune(ctx context.Context, before int64, retainDays, retainRows int) (int64, error)
|
||||
}
|
||||
|
||||
// EventsJournalUseCase exposes the bounded cursor operations needed by
|
||||
// proxied-server callers without leaking a raw SQL connection.
|
||||
type EventsJournalUseCase interface {
|
||||
Read(ctx context.Context, since int64, limit int) ([]storage.EventsJournalRow, error)
|
||||
Prune(ctx context.Context, before int64, retainDays, retainRows int) (int64, error)
|
||||
}
|
||||
|
||||
func NewEventsJournalUseCase(repo EventsJournalSQLRepository) EventsJournalUseCase {
|
||||
return &eventsJournalUseCase{repo: repo}
|
||||
}
|
||||
|
||||
type eventsJournalUseCase struct {
|
||||
repo EventsJournalSQLRepository
|
||||
}
|
||||
|
||||
var _ EventsJournalUseCase = (*eventsJournalUseCase)(nil)
|
||||
|
||||
func (u *eventsJournalUseCase) Read(ctx context.Context, since int64, limit int) ([]storage.EventsJournalRow, error) {
|
||||
return u.repo.Read(ctx, since, limit)
|
||||
}
|
||||
|
||||
func (u *eventsJournalUseCase) Prune(ctx context.Context, before int64, retainDays, retainRows int) (int64, error) {
|
||||
return u.repo.Prune(ctx, before, retainDays, retainRows)
|
||||
}
|
||||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -19,10 +18,8 @@ import (
|
|||
func TestEventsJournalAccessor(t *testing.T) {
|
||||
env := newTestEnv(t, "jrn")
|
||||
ctx := context.Background()
|
||||
issueops.SetJournalEnabled(true)
|
||||
t.Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
|
||||
store := env.store
|
||||
store.SetEventsJournalEnabled(true)
|
||||
mk := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -24,9 +23,8 @@ import (
|
|||
// contiguous seq per create.
|
||||
func TestEventsJournal_EmbeddedConcurrentGaplessNoDup(t *testing.T) {
|
||||
env := newTestEnv(t, "ecw")
|
||||
issueops.SetJournalEnabled(true)
|
||||
t.Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
store := env.store
|
||||
store.SetEventsJournalEnabled(true)
|
||||
|
||||
const writers = 12
|
||||
var wg sync.WaitGroup
|
||||
|
|
|
|||
|
|
@ -39,12 +39,13 @@ var _ storage.SchemaMigrator = (*EmbeddedDoltStore)(nil)
|
|||
// The dolthub/driver/v2 handles its own concurrency internally. File-level locking
|
||||
// is only used during bd init to protect one-time initialization steps.
|
||||
type EmbeddedDoltStore struct {
|
||||
dataDir string
|
||||
beadsDir string
|
||||
database string
|
||||
branch string
|
||||
credentialKey []byte
|
||||
closed atomic.Bool
|
||||
dataDir string
|
||||
beadsDir string
|
||||
database string
|
||||
branch string
|
||||
credentialKey []byte
|
||||
closed atomic.Bool
|
||||
eventsJournalEnabled atomic.Bool
|
||||
// readOnly marks a store opened via OpenReadOnly: open-time mutations
|
||||
// (CREATE DATABASE, schema migrations) were skipped and write
|
||||
// transactions are refused (bd-6dnrw.32).
|
||||
|
|
@ -221,6 +222,8 @@ func (s *EmbeddedDoltStore) withConn(ctx context.Context, commit bool, fn func(t
|
|||
err = fmt.Errorf("embeddeddolt: begin tx: %w", err)
|
||||
return
|
||||
}
|
||||
clearJournalScope := issueops.ScopeEventsJournalTransaction(tx, s.eventsJournalEnabled.Load())
|
||||
defer clearJournalScope()
|
||||
|
||||
if fnErr := fn(tx); fnErr != nil {
|
||||
err = errors.Join(fnErr, tx.Rollback())
|
||||
|
|
@ -239,6 +242,11 @@ func (s *EmbeddedDoltStore) withConn(ctx context.Context, commit bool, fn func(t
|
|||
return
|
||||
}
|
||||
|
||||
// SetEventsJournalEnabled activates the journal for this store instance only.
|
||||
func (s *EmbeddedDoltStore) SetEventsJournalEnabled(enabled bool) {
|
||||
s.eventsJournalEnabled.Store(enabled)
|
||||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) ApplySchemaMigrations(ctx context.Context) (int, error) {
|
||||
if s.closed.Load() {
|
||||
return 0, errClosed
|
||||
|
|
|
|||
|
|
@ -100,6 +100,10 @@ func recomputeIsBlockedCounting(ctx context.Context, tx DBTX, issueIDs, wispIDs
|
|||
if len(issueIDs) == 0 && len(wispIDs) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
before, err := captureBlockedJournalSnapshot(ctx, tx, issueIDs, wispIDs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var total int64
|
||||
for {
|
||||
var changed int64
|
||||
|
|
@ -115,6 +119,9 @@ func recomputeIsBlockedCounting(ctx context.Context, tx DBTX, issueIDs, wispIDs
|
|||
changed += n
|
||||
total += changed
|
||||
if changed == 0 {
|
||||
if err := recordBlockedJournalChanges(ctx, tx, before, issueIDs, wispIDs); err != nil {
|
||||
return total, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@ func RecomputeIsBlockedInTx(ctx context.Context, tx DBTX, issueIDs, wispIDs []st
|
|||
if len(issueIDs) == 0 && len(wispIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
before, err := captureBlockedJournalSnapshot(ctx, tx, issueIDs, wispIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
var changed int64
|
||||
|
||||
|
|
@ -77,7 +81,7 @@ func RecomputeIsBlockedInTx(ctx context.Context, tx DBTX, issueIDs, wispIDs []st
|
|||
changed += n
|
||||
|
||||
if changed == 0 {
|
||||
return nil
|
||||
return recordBlockedJournalChanges(ctx, tx, before, issueIDs, wispIDs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -86,6 +90,10 @@ func MarkIsBlockedInTx(ctx context.Context, tx DBTX, issueIDs, wispIDs []string)
|
|||
if len(issueIDs) == 0 && len(wispIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
before, err := captureBlockedJournalSnapshot(ctx, tx, issueIDs, wispIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
var changed int64
|
||||
|
||||
|
|
@ -102,7 +110,7 @@ func MarkIsBlockedInTx(ctx context.Context, tx DBTX, issueIDs, wispIDs []string)
|
|||
changed += n
|
||||
|
||||
if changed == 0 {
|
||||
return nil
|
||||
return recordBlockedJournalChanges(ctx, tx, before, issueIDs, wispIDs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -197,6 +197,9 @@ func DeleteIssuesBySourceRepoInTx(ctx context.Context, tx *sql.Tx, sourceRepo st
|
|||
return 0, fmt.Errorf("affected by source-repo delete: %w", aerr)
|
||||
}
|
||||
|
||||
if err := RecordDependencyRemovalsForIssuesInTx(ctx, tx, issueIDs); err != nil {
|
||||
return 0, fmt.Errorf("journal dependency removals for source-repo delete: %w", err)
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `DELETE FROM issues WHERE source_repo = ?`, sourceRepo)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("delete issues: %w", err)
|
||||
|
|
@ -224,6 +227,14 @@ func DeleteIssuesBySourceRepoInTx(ctx context.Context, tx *sql.Tx, sourceRepo st
|
|||
|
||||
//nolint:gosec // G201: table names are hardcoded
|
||||
func UpdateIssueIDInTx(ctx context.Context, tx *sql.Tx, oldID, newID string, issue *types.Issue, actor string) error {
|
||||
var renameEdges []journalDependencyEdge
|
||||
if journalEnabled(ctx, tx) {
|
||||
var err error
|
||||
renameEdges, err = dependencyEdgesForIssueIDsInTx(ctx, tx, []string{oldID})
|
||||
if err != nil {
|
||||
return fmt.Errorf("capture dependency edges for rename %s -> %s: %w", oldID, newID, err)
|
||||
}
|
||||
}
|
||||
if IsActiveWispInTx(ctx, tx, oldID) {
|
||||
if err := updateWispIDInTx(ctx, tx, oldID, newID, issue, actor); err != nil {
|
||||
return err
|
||||
|
|
@ -231,9 +242,34 @@ func UpdateIssueIDInTx(ctx context.Context, tx *sql.Tx, oldID, newID string, iss
|
|||
} else if err := updateIssueIDInTx(ctx, tx, oldID, newID, issue, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
// Journal the rename as an update keyed on the new id (the post-rename
|
||||
// snapshot). The 'renamed' history event carries the old->new link.
|
||||
return RecordEventInTx(ctx, tx, EventUpdate, newID)
|
||||
return recordRenameInJournal(ctx, tx, oldID, newID, renameEdges)
|
||||
}
|
||||
|
||||
func recordRenameInJournal(ctx context.Context, tx DBTX, oldID, newID string, edges []journalDependencyEdge) error {
|
||||
for _, edge := range edges {
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepRemove, edge.source, edge.kind, edge.target, edge.metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := RecordDeleteInTx(ctx, tx, oldID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := RecordEventInTx(ctx, tx, EventCreate, newID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, edge := range edges {
|
||||
source, target := edge.source, edge.target
|
||||
if source == oldID {
|
||||
source = newID
|
||||
}
|
||||
if target == oldID {
|
||||
target = newID
|
||||
}
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepAdd, source, edge.kind, target, edge.metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateIssueIDInTx(ctx context.Context, tx *sql.Tx, oldID, newID string, issue *types.Issue, actor string) error {
|
||||
|
|
|
|||
|
|
@ -144,19 +144,19 @@ func ImportIssueCommentInTx(ctx context.Context, tx *sql.Tx, issueID, author, te
|
|||
return nil, fmt.Errorf("add comment to %s: %w", commentTable, err)
|
||||
}
|
||||
|
||||
// Journal the comment as an update to the issue in the same transaction — a
|
||||
// comment mutates the issue's bead state, recorded like a label change.
|
||||
if err := RecordEventInTx(ctx, tx, EventUpdate, issueID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.Comment{
|
||||
comment := &types.Comment{
|
||||
ID: id,
|
||||
IssueID: issueID,
|
||||
Author: author,
|
||||
Text: text,
|
||||
CreatedAt: createdAt,
|
||||
}, nil
|
||||
}
|
||||
if err := RecordCommentEventInTx(ctx, tx, issueID, &EventComment{
|
||||
ID: id, Author: author, Text: text, CreatedAt: createdAt, Source: "structured",
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// AddCommentEventInTx adds a comment as an event to an issue within a transaction.
|
||||
|
|
@ -166,12 +166,16 @@ func ImportIssueCommentInTx(ctx context.Context, tx *sql.Tx, issueID, author, te
|
|||
func AddCommentEventInTx(ctx context.Context, tx DBTX, issueID, actor, comment string) error {
|
||||
isWisp := IsActiveWispInTx(ctx, tx, issueID)
|
||||
_, _, eventTable, _ := WispTableRouting(isWisp)
|
||||
id := NewEventID()
|
||||
createdAt := time.Now().UTC().Truncate(time.Second)
|
||||
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
INSERT INTO %s (id, issue_id, event_type, actor, comment)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`, eventTable), NewEventID(), issueID, types.EventCommented, actor, comment); err != nil {
|
||||
INSERT INTO %s (id, issue_id, event_type, actor, comment, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`, eventTable), id, issueID, types.EventCommented, actor, comment, createdAt); err != nil {
|
||||
return fmt.Errorf("add comment event to %s: %w", eventTable, err)
|
||||
}
|
||||
return nil
|
||||
return RecordCommentEventInTx(ctx, tx, issueID, &EventComment{
|
||||
ID: id, Author: actor, Text: comment, CreatedAt: createdAt, Source: "audit",
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -740,7 +740,7 @@ func PersistDependenciesWithOptionsResult(ctx context.Context, tx *sql.Tx, issue
|
|||
result.markChanged(depTable)
|
||||
// Creation-time edges are independently replayable operations; do
|
||||
// not rely on the issue create payload's inline dependencies.
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID); err != nil {
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, dep.Metadata); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,9 @@ func DeleteIssueInTx(ctx context.Context, tx *sql.Tx, id string) error {
|
|||
return fmt.Errorf("affected by delete for %s: %w", id, aerr)
|
||||
}
|
||||
|
||||
if err := RecordDependencyRemovalsForIssuesInTx(ctx, tx, []string{id}); err != nil {
|
||||
return fmt.Errorf("journal dependency removals for %s: %w", id, err)
|
||||
}
|
||||
if err := deleteIssueRowInTx(ctx, tx, id, isWisp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -159,6 +162,13 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("partition expanded delete IDs: %w", err)
|
||||
}
|
||||
// A bulk DELETE reports only a batch total. Resolve the actual regular rows
|
||||
// while they are still present so result counts and journal deletes describe
|
||||
// committed mutations, never caller-requested phantom IDs.
|
||||
finalRegularIDs, err = ExistingIssueIDsInTableInTx(ctx, tx, "issues", finalRegularIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve existing issues for delete: %w", err)
|
||||
}
|
||||
|
||||
allWispIDs := append(append([]string{}, initialWispIDs...), cascadeWispIDs...)
|
||||
allDeletedSet := make(map[string]bool, len(finalRegularIDs)+len(allWispIDs))
|
||||
|
|
@ -246,6 +256,11 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
return nil, fmt.Errorf("affected by batch delete: %w", aerr)
|
||||
}
|
||||
|
||||
allDeletedIDs := append(append([]string{}, finalRegularIDs...), allWispIDs...)
|
||||
if err := RecordDependencyRemovalsForIssuesInTx(ctx, tx, allDeletedIDs); err != nil {
|
||||
return nil, fmt.Errorf("journal dependency removals for batch delete: %w", err)
|
||||
}
|
||||
|
||||
for _, id := range allWispIDs {
|
||||
if err := deleteIssueRowInTx(ctx, tx, id, true); err != nil {
|
||||
return nil, fmt.Errorf("delete wisp %s: %w", id, err)
|
||||
|
|
@ -289,6 +304,51 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// ExistingIssueIDsInTableInTx returns the requested IDs that currently exist
|
||||
// in the selected issue table. It preserves caller ordering so delete and
|
||||
// journal records are deterministic across batches.
|
||||
func ExistingIssueIDsInTableInTx(ctx context.Context, tx DBTX, table string, ids []string) ([]string, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
switch table {
|
||||
case "issues", "wisps":
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported issue table %q", table)
|
||||
}
|
||||
exists := make(map[string]struct{}, len(ids))
|
||||
for i := 0; i < len(ids); i += deleteBatchSize {
|
||||
end := i + deleteBatchSize
|
||||
if end > len(ids) {
|
||||
end = len(ids)
|
||||
}
|
||||
inClause, args := buildSQLInClause(ids[i:end])
|
||||
//nolint:gosec // table is validated above and inClause contains only placeholders.
|
||||
rows, err := tx.QueryContext(ctx, "SELECT id FROM "+table+" WHERE id IN ("+inClause+")", args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
exists[id] = struct{}{}
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
actual := make([]string, 0, len(exists))
|
||||
for _, id := range ids {
|
||||
if _, ok := exists[id]; ok {
|
||||
actual = append(actual, id)
|
||||
}
|
||||
}
|
||||
return actual, nil
|
||||
}
|
||||
|
||||
// findAllDependentsRecursiveInTx finds all issues that depend on the given
|
||||
// issues, recursively. Uses batched IN-clause queries. Traversal is capped
|
||||
// at maxRecursiveResults total discovered IDs.
|
||||
|
|
|
|||
|
|
@ -200,7 +200,9 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
|
|||
metadata, dep.IssueID, dep.DependsOnID); err != nil {
|
||||
return fmt.Errorf("failed to update dependency metadata: %w", err)
|
||||
}
|
||||
return nil
|
||||
// A same-type add refreshes edge metadata. It is an observable graph
|
||||
// mutation, so emit the complete replacement edge for replay.
|
||||
return RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
return fmt.Errorf("dependency %s -> %s already exists with type %q (requested %q); remove it first with 'bd dep remove' then re-add",
|
||||
dep.IssueID, dep.DependsOnID, existingType, dep.Type)
|
||||
|
|
@ -220,12 +222,6 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
|
|||
return fmt.Errorf("failed to add dependency: %w", err)
|
||||
}
|
||||
|
||||
// Journal the dependency add in the same transaction (both is_blocked
|
||||
// branches below return from here, so emitting once here covers them).
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcIsWisp := writeTable == "wisp_dependencies"
|
||||
var affectedIssues, affectedWisps []string
|
||||
var aerr error
|
||||
|
|
@ -249,12 +245,13 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
|
|||
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("recompute is_blocked after add dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
|
||||
}
|
||||
return nil
|
||||
return RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
if err := MarkIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("mark is_blocked after add dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
|
||||
}
|
||||
return nil
|
||||
// Snapshot only after all derived blocked-state maintenance has completed.
|
||||
return RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata)
|
||||
}
|
||||
|
||||
// RemoveSourceFromAffected drops the dep source from the affected-ID sets
|
||||
|
|
@ -714,11 +711,11 @@ func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID
|
|||
|
||||
// Capture the row's type before deleting so we can dispatch the right
|
||||
// affected-set helper. If no row matches, treat as a no-op.
|
||||
var depType string
|
||||
var depType, metadata string
|
||||
row := tx.QueryRowContext(ctx, fmt.Sprintf(
|
||||
`SELECT type FROM %s WHERE issue_id = ? AND %s = ?`, depTable, DepTargetExpr),
|
||||
`SELECT type, metadata FROM %s WHERE issue_id = ? AND %s = ?`, depTable, DepTargetExpr),
|
||||
issueID, dependsOnID)
|
||||
if err := row.Scan(&depType); err != nil {
|
||||
if err := row.Scan(&depType, &metadata); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -731,12 +728,6 @@ func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID
|
|||
return fmt.Errorf("remove dependency: %w", err)
|
||||
}
|
||||
|
||||
// Journal the dependency remove in the same transaction (the no-match path
|
||||
// returned earlier, so reaching here means an edge was deleted).
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepRemove, issueID, depType, dependsOnID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var affectedIssues, affectedWisps []string
|
||||
var aerr error
|
||||
if isWisp {
|
||||
|
|
@ -750,7 +741,8 @@ func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID
|
|||
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
|
||||
return fmt.Errorf("recompute is_blocked after remove dependency %s -> %s: %w", issueID, dependsOnID, err)
|
||||
}
|
||||
return nil
|
||||
// Snapshot only after all derived blocked-state maintenance has completed.
|
||||
return RecordDepEventInTx(ctx, tx, EventDepRemove, issueID, depType, dependsOnID, metadata)
|
||||
}
|
||||
|
||||
// GetIssuesByIDsInTx retrieves multiple issues by ID within an existing
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
|
|
@ -49,31 +50,168 @@ type EventOp string
|
|||
|
||||
// The closed set of journalled mutation kinds.
|
||||
const (
|
||||
EventCreate EventOp = "create"
|
||||
EventUpdate EventOp = "update"
|
||||
EventClose EventOp = "close"
|
||||
EventDelete EventOp = "delete"
|
||||
EventDepAdd EventOp = "dep_add"
|
||||
EventDepRemove EventOp = "dep_remove"
|
||||
EventCreate EventOp = "create"
|
||||
EventUpdate EventOp = "update"
|
||||
EventClose EventOp = "close"
|
||||
EventDelete EventOp = "delete"
|
||||
EventDepAdd EventOp = "dep_add"
|
||||
EventDepRemove EventOp = "dep_remove"
|
||||
EventCommentWrite EventOp = "comment"
|
||||
)
|
||||
|
||||
// EventDep is the edge payload recorded for dependency operations.
|
||||
type EventDep struct {
|
||||
Kind string `json:"kind"`
|
||||
Target string `json:"target"`
|
||||
Kind string `json:"kind"`
|
||||
Target string `json:"target"`
|
||||
Metadata string `json:"metadata"`
|
||||
}
|
||||
|
||||
// journalOn gates every emission. It is process-global because a bd process
|
||||
// serves a single workspace; cmd/bd sets it from the events-journal config
|
||||
// knob, and tests set it directly. Default OFF: when off, the *InTx emit helpers
|
||||
// are a cheap no-op and no rows are written.
|
||||
var journalOn atomic.Bool
|
||||
// EventComment is the stable, replayable payload for a comment write. Source
|
||||
// distinguishes a structured comment row from an audit-trail comment event.
|
||||
type EventComment struct {
|
||||
ID string `json:"id"`
|
||||
Author string `json:"author"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
// SetJournalEnabled turns events journaling on or off for this process.
|
||||
func SetJournalEnabled(on bool) { journalOn.Store(on) }
|
||||
// Journal activation is carried by the operation context. Store instances add
|
||||
// this value when opening an explicitly enabled project, so opening one Hosted
|
||||
// project cannot turn journaling on for any other project in the same process.
|
||||
// A missing value is deliberately safe-off.
|
||||
type journalContextKey struct{}
|
||||
|
||||
// JournalEnabled reports whether events journaling is currently on.
|
||||
func JournalEnabled() bool { return journalOn.Load() }
|
||||
var journalTransactions sync.Map // map[DBTX]bool; entries live for one transaction
|
||||
|
||||
// WithEventsJournal returns ctx scoped to one storage operation with the
|
||||
// durable events journal enabled or disabled.
|
||||
func WithEventsJournal(ctx context.Context, enabled bool) context.Context {
|
||||
return context.WithValue(ctx, journalContextKey{}, enabled)
|
||||
}
|
||||
|
||||
// ScopeEventsJournalTransaction associates activation with one concrete
|
||||
// transaction and returns a cleanup function. Store implementations call it
|
||||
// immediately after BeginTx. This is instance/project scoped even when many
|
||||
// stores share a process; there is no process-wide activation switch.
|
||||
func ScopeEventsJournalTransaction(tx DBTX, enabled bool) func() {
|
||||
if tx == nil {
|
||||
return func() {}
|
||||
}
|
||||
journalTransactions.Store(tx, enabled)
|
||||
return func() { journalTransactions.Delete(tx) }
|
||||
}
|
||||
|
||||
func journalEnabled(ctx context.Context, tx DBTX) bool {
|
||||
if enabled, ok := ctx.Value(journalContextKey{}).(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
enabled, _ := journalTransactions.Load(tx)
|
||||
on, _ := enabled.(bool)
|
||||
return on
|
||||
}
|
||||
|
||||
type blockedJournalKey struct {
|
||||
table string
|
||||
id string
|
||||
}
|
||||
|
||||
type blockedJournalSnapshot map[blockedJournalKey]bool
|
||||
|
||||
// captureBlockedJournalSnapshot records the pre-maintenance readiness state
|
||||
// for the exact affected set. It is a no-op unless this transaction's durable
|
||||
// journal is enabled, so ordinary local operations pay no extra reads.
|
||||
func captureBlockedJournalSnapshot(
|
||||
ctx context.Context,
|
||||
tx DBTX,
|
||||
issueIDs, wispIDs []string,
|
||||
) (blockedJournalSnapshot, error) {
|
||||
if !journalEnabled(ctx, tx) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
snapshot := make(blockedJournalSnapshot, len(issueIDs)+len(wispIDs))
|
||||
for _, target := range []struct {
|
||||
table string
|
||||
ids []string
|
||||
}{
|
||||
{table: "issues", ids: issueIDs},
|
||||
{table: "wisps", ids: wispIDs},
|
||||
} {
|
||||
for start := 0; start < len(target.ids); start += queryBatchSize {
|
||||
end := start + queryBatchSize
|
||||
if end > len(target.ids) {
|
||||
end = len(target.ids)
|
||||
}
|
||||
inClause, args := buildSQLInClause(target.ids[start:end])
|
||||
//nolint:gosec // table is one of the two hardcoded values above.
|
||||
rows, err := tx.QueryContext(ctx,
|
||||
fmt.Sprintf("SELECT id, is_blocked FROM %s WHERE id IN (%s)", target.table, inClause),
|
||||
args...)
|
||||
if err != nil {
|
||||
if optionalBlockedTable(target.table) && isTableNotExistError(err) {
|
||||
break
|
||||
}
|
||||
return nil, fmt.Errorf("journal: snapshot derived is_blocked from %s: %w", target.table, err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var blocked int
|
||||
if err := rows.Scan(&id, &blocked); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, fmt.Errorf("journal: scan derived is_blocked from %s: %w", target.table, err)
|
||||
}
|
||||
snapshot[blockedJournalKey{table: target.table, id: id}] = blocked != 0
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, fmt.Errorf("journal: iterate derived is_blocked from %s: %w", target.table, err)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, fmt.Errorf("journal: close derived is_blocked from %s: %w", target.table, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
// recordBlockedJournalChanges compares the stable post-maintenance state with
|
||||
// the captured state and journals only beads whose derived is_blocked value
|
||||
// actually changed. The emitted update carries the complete post-mutation
|
||||
// snapshot, allowing a cursor consumer to stay correct without graph queries.
|
||||
func recordBlockedJournalChanges(
|
||||
ctx context.Context,
|
||||
tx DBTX,
|
||||
before blockedJournalSnapshot,
|
||||
issueIDs, wispIDs []string,
|
||||
) error {
|
||||
if before == nil {
|
||||
return nil
|
||||
}
|
||||
after, err := captureBlockedJournalSnapshot(ctx, tx, issueIDs, wispIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var changed []blockedJournalKey
|
||||
for key, afterBlocked := range after {
|
||||
if beforeBlocked, existed := before[key]; existed && beforeBlocked != afterBlocked {
|
||||
changed = append(changed, key)
|
||||
}
|
||||
}
|
||||
sort.Slice(changed, func(i, j int) bool {
|
||||
if changed[i].table != changed[j].table {
|
||||
return changed[i].table < changed[j].table
|
||||
}
|
||||
return changed[i].id < changed[j].id
|
||||
})
|
||||
for _, key := range changed {
|
||||
if err := RecordEventInTx(ctx, tx, EventUpdate, key.id); err != nil {
|
||||
return fmt.Errorf("journal: record derived is_blocked update for %s: %w", key.id, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecordEventInTx records op for issueID, snapshotting the issue's
|
||||
// post-mutation state as of tx (read-your-writes within the same transaction).
|
||||
|
|
@ -81,45 +219,86 @@ func JournalEnabled() bool { return journalOn.Load() }
|
|||
// RecordDeleteInTx) and dependency ops (use RecordDepEventInTx). A no-op when
|
||||
// journaling is disabled.
|
||||
func RecordEventInTx(ctx context.Context, tx DBTX, op EventOp, issueID string) error {
|
||||
if !journalOn.Load() {
|
||||
if !journalEnabled(ctx, tx) {
|
||||
return nil
|
||||
}
|
||||
issue, err := GetIssueInTx(ctx, tx, issueID)
|
||||
issue, err := getJournalIssueInTx(ctx, tx, issueID)
|
||||
if err != nil {
|
||||
// The row should exist for a non-delete op; a missing row means the
|
||||
// mutation and the journal disagree, so fail the transaction rather than
|
||||
// record a hole.
|
||||
return fmt.Errorf("journal: snapshot %s for %s: %w", op, issueID, err)
|
||||
}
|
||||
return insertEventRow(ctx, tx, op, issueID, issue, nil)
|
||||
return insertEventRow(ctx, tx, op, issueID, issue, nil, nil)
|
||||
}
|
||||
|
||||
// RecordDeleteInTx records a delete for issueID with a null issue payload (the
|
||||
// row no longer exists). A no-op when journaling is disabled.
|
||||
func RecordDeleteInTx(ctx context.Context, tx DBTX, issueID string) error {
|
||||
if !journalOn.Load() {
|
||||
if !journalEnabled(ctx, tx) {
|
||||
return nil
|
||||
}
|
||||
return insertEventRow(ctx, tx, EventDelete, issueID, nil, nil)
|
||||
return insertEventRow(ctx, tx, EventDelete, issueID, nil, nil, nil)
|
||||
}
|
||||
|
||||
// RecordDepEventInTx records a dependency add or remove for issueID, carrying
|
||||
// the edge kind and target. The issue snapshot is the post-mutation state as of
|
||||
// tx. A no-op when journaling is disabled.
|
||||
func RecordDepEventInTx(ctx context.Context, tx DBTX, op EventOp, issueID, kind, target string) error {
|
||||
if !journalOn.Load() {
|
||||
func RecordDepEventInTx(ctx context.Context, tx DBTX, op EventOp, issueID, kind, target, metadata string) error {
|
||||
if !journalEnabled(ctx, tx) {
|
||||
return nil
|
||||
}
|
||||
issue, err := GetIssueInTx(ctx, tx, issueID)
|
||||
issue, err := getJournalIssueInTx(ctx, tx, issueID)
|
||||
if err != nil {
|
||||
// The dependency source may itself have been deleted (cascade); record
|
||||
// the edge change with a null snapshot rather than failing.
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
return insertEventRow(ctx, tx, op, issueID, nil, &EventDep{Kind: kind, Target: target})
|
||||
return insertEventRow(ctx, tx, op, issueID, nil, &EventDep{Kind: kind, Target: target, Metadata: metadata}, nil)
|
||||
}
|
||||
return fmt.Errorf("journal: snapshot %s for %s: %w", op, issueID, err)
|
||||
}
|
||||
return insertEventRow(ctx, tx, op, issueID, issue, &EventDep{Kind: kind, Target: target})
|
||||
return insertEventRow(ctx, tx, op, issueID, issue, &EventDep{Kind: kind, Target: target, Metadata: metadata}, nil)
|
||||
}
|
||||
|
||||
// RecordCommentEventInTx records a replayable structured or audit comment.
|
||||
func RecordCommentEventInTx(ctx context.Context, tx DBTX, issueID string, comment *EventComment) error {
|
||||
if !journalEnabled(ctx, tx) {
|
||||
return nil
|
||||
}
|
||||
issue, err := getJournalIssueInTx(ctx, tx, issueID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("journal: snapshot comment for %s: %w", issueID, err)
|
||||
}
|
||||
return insertEventRow(ctx, tx, EventCommentWrite, issueID, issue, nil, comment)
|
||||
}
|
||||
|
||||
// getJournalIssueInTx augments the normal issue snapshot with the persisted
|
||||
// readiness projection. is_blocked is deliberately not part of ordinary issue
|
||||
// hydration, but it is required in a journal snapshot because a dependency
|
||||
// delta must be replayable without re-running graph maintenance.
|
||||
func getJournalIssueInTx(ctx context.Context, tx DBTX, issueID string) (*types.Issue, error) {
|
||||
for _, candidate := range []struct {
|
||||
issueTable string
|
||||
labelTable string
|
||||
}{
|
||||
{"issues", "labels"},
|
||||
{"wisps", "wisp_labels"},
|
||||
} {
|
||||
issue, err := getIssueFromTableInTx(ctx, tx, candidate.issueTable, candidate.labelTable, issueID)
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var blocked int
|
||||
if err := tx.QueryRowContext(ctx, fmt.Sprintf("SELECT is_blocked FROM %s WHERE id = ?", candidate.issueTable), issueID).Scan(&blocked); err != nil {
|
||||
return nil, fmt.Errorf("journal: read is_blocked for %s: %w", issueID, err)
|
||||
}
|
||||
issue.IsBlocked = blocked != 0
|
||||
return issue, nil
|
||||
}
|
||||
return nil, fmt.Errorf("%w: issue %s", storage.ErrNotFound, issueID)
|
||||
}
|
||||
|
||||
// insertEventRow performs the actual INSERT. It is the ONE seam both write
|
||||
|
|
@ -127,7 +306,7 @@ func RecordDepEventInTx(ctx context.Context, tx DBTX, op EventOp, issueID, kind,
|
|||
// nil issue is stored as SQL NULL (deletes); a nil dep is stored as SQL NULL
|
||||
// (non-dependency ops). ts is the insert time, stamped inside the committing
|
||||
// transaction.
|
||||
func insertEventRow(ctx context.Context, tx DBTX, op EventOp, issueID string, issue *types.Issue, dep *EventDep) error {
|
||||
func insertEventRow(ctx context.Context, tx DBTX, op EventOp, issueID string, issue *types.Issue, dep *EventDep, comment *EventComment) error {
|
||||
var issueJSON any
|
||||
if issue != nil {
|
||||
b, err := json.Marshal(issue)
|
||||
|
|
@ -144,14 +323,22 @@ func insertEventRow(ctx context.Context, tx DBTX, op EventOp, issueID string, is
|
|||
}
|
||||
depJSON = string(b)
|
||||
}
|
||||
var commentJSON any
|
||||
if comment != nil {
|
||||
b, err := json.Marshal(comment)
|
||||
if err != nil {
|
||||
return fmt.Errorf("journal: marshal comment for %s: %w", issueID, err)
|
||||
}
|
||||
commentJSON = string(b)
|
||||
}
|
||||
seq, err := nextEventSeq(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO bd_events_journal (seq, ts, op, issue_id, issue_json, dep_json)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`, seq, time.Now().UTC(), string(op), issueID, issueJSON, depJSON)
|
||||
INSERT INTO bd_events_journal (seq, ts, op, issue_id, issue_json, dep_json, comment_json)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`, seq, time.Now().UTC(), string(op), issueID, issueJSON, depJSON, commentJSON)
|
||||
if err != nil {
|
||||
return fmt.Errorf("journal: record %s for %s: %w", op, issueID, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,22 +49,18 @@ var mutationEntryPoints = []string{
|
|||
|
||||
// beadDMLExemptions are exported functions the DML detector flags as writing a
|
||||
// work-bead table but which legitimately do NOT journal, each with a reason.
|
||||
// They fall into four buckets: (1) derived is_blocked maintenance; (2) aux-table
|
||||
// writers the templated-%s heuristic can't distinguish from a bead table (events,
|
||||
// child counters); (3) constituent sub-helpers of a create/rename/promote/delete
|
||||
// whose top-level entry point journals the whole mutation once; (4) compaction
|
||||
// They fall into four buckets: (1) derived child-counter maintenance; (2)
|
||||
// aux-table writers the templated-%s heuristic can't distinguish from a bead
|
||||
// table (events, child counters); (3) constituent sub-helpers of a
|
||||
// create/rename/promote/delete whose top-level entry point journals the whole
|
||||
// mutation once; (4) compaction
|
||||
// maintenance outside the create/update/close/delete/dep/label op vocabulary. The
|
||||
// staleness check fails if any stops being flagged, so an exemption cannot rot.
|
||||
var beadDMLExemptions = map[string]string{
|
||||
// (1) is_blocked is a denormalized/derived column recomputed as a side effect
|
||||
// of the graph mutation that changed it (which journals).
|
||||
"MarkIsBlockedInTx": "maintains the derived is_blocked column, not a bead mutation",
|
||||
"RecomputeIsBlockedInTx": "recomputes the derived is_blocked column, not a bead mutation",
|
||||
"RecomputeAllIsBlockedInTx": "recomputes the derived is_blocked column, not a bead mutation",
|
||||
"RecomputeIsBlockedForIDsInTx": "recomputes the derived is_blocked column, not a bead mutation",
|
||||
"RecomputeIsBlockedForWispIDsInTx": "recomputes the derived is_blocked column, not a bead mutation",
|
||||
"RecomputeIsBlockedAfterMergeInTx": "recomputes the derived is_blocked column, not a bead mutation",
|
||||
"ReconcileChildCounters": "recomputes denormalized child-counter state, not a bead mutation",
|
||||
// (1) Child counters are derived CLI acceleration state. In contrast,
|
||||
// is_blocked is part of the exported bead snapshot and its recompute helpers
|
||||
// structurally journal every value that actually changes.
|
||||
"ReconcileChildCounters": "recomputes denormalized child-counter state, not a bead mutation",
|
||||
|
||||
// (2) aux tables matched via templated %s, not work-bead state.
|
||||
"RecordEventInTable": "writes the events audit table (templated %s), not work-bead state",
|
||||
|
|
|
|||
131
internal/storage/issueops/journal_dependencies.go
Normal file
131
internal/storage/issueops/journal_dependencies.go
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
package issueops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type journalDependencyEdge struct {
|
||||
source string
|
||||
target string
|
||||
kind string
|
||||
metadata string
|
||||
}
|
||||
|
||||
// RecordDependencyRemovalsForIssuesInTx emits one deterministic dep_remove
|
||||
// record for every edge whose source or target is in ids. Callers invoke this
|
||||
// before deleting the edges or nodes so source snapshots are still available.
|
||||
func RecordDependencyRemovalsForIssuesInTx(ctx context.Context, tx DBTX, ids []string) error {
|
||||
if !journalEnabled(ctx, tx) || len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
edges, err := dependencyEdgesForIssueIDsInTx(ctx, tx, ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return recordDependencyRemovalsInTx(ctx, tx, edges)
|
||||
}
|
||||
|
||||
// RecordDependencyRemovalsForTableInTx is the table-scoped variant used by
|
||||
// the UOW dependency repository immediately before its bulk edge DELETE.
|
||||
func RecordDependencyRemovalsForTableInTx(ctx context.Context, tx DBTX, table string, ids []string) error {
|
||||
if !journalEnabled(ctx, tx) || len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
edges, err := dependencyEdgesInTableForIssueIDsInTx(ctx, tx, table, ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return recordDependencyRemovalsInTx(ctx, tx, edges)
|
||||
}
|
||||
|
||||
func recordDependencyRemovalsInTx(ctx context.Context, tx DBTX, edges []journalDependencyEdge) error {
|
||||
for _, edge := range edges {
|
||||
if err := RecordDepEventInTx(ctx, tx, EventDepRemove, edge.source, edge.kind, edge.target, edge.metadata); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func dependencyEdgesForIssueIDsInTx(ctx context.Context, tx DBTX, ids []string) ([]journalDependencyEdge, error) {
|
||||
byKey := make(map[string]journalDependencyEdge)
|
||||
for _, table := range []string{"dependencies", "wisp_dependencies"} {
|
||||
edges, err := dependencyEdgesInTableForIssueIDsInTx(ctx, tx, table, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, edge := range edges {
|
||||
byKey[dependencyEdgeKey(edge)] = edge
|
||||
}
|
||||
}
|
||||
return sortedDependencyEdges(byKey), nil
|
||||
}
|
||||
|
||||
func dependencyEdgesInTableForIssueIDsInTx(ctx context.Context, tx DBTX, table string, ids []string) ([]journalDependencyEdge, error) {
|
||||
switch table {
|
||||
case "dependencies", "wisp_dependencies":
|
||||
default:
|
||||
return nil, fmt.Errorf("journal: unsupported dependency table %q", table)
|
||||
}
|
||||
|
||||
byKey := make(map[string]journalDependencyEdge)
|
||||
for start := 0; start < len(ids); start += deleteBatchSize {
|
||||
end := start + deleteBatchSize
|
||||
if end > len(ids) {
|
||||
end = len(ids)
|
||||
}
|
||||
inClause, args := buildSQLInClause(ids[start:end])
|
||||
queryArgs := append(append([]any{}, args...), args...)
|
||||
//nolint:gosec // table is validated above and inClause contains only placeholders.
|
||||
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
|
||||
SELECT issue_id, %s AS target, type, metadata
|
||||
FROM %s
|
||||
WHERE issue_id IN (%s) OR %s IN (%s)
|
||||
`, DepTargetExpr, table, inClause, DepTargetExpr, inClause), queryArgs...)
|
||||
if err != nil {
|
||||
if optionalBlockedTable(table) && isTableNotExistError(err) {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("journal: query dependency removals from %s: %w", table, err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var edge journalDependencyEdge
|
||||
if err := rows.Scan(&edge.source, &edge.target, &edge.kind, &edge.metadata); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, fmt.Errorf("journal: scan dependency removal from %s: %w", table, err)
|
||||
}
|
||||
byKey[dependencyEdgeKey(edge)] = edge
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, fmt.Errorf("journal: iterate dependency removals from %s: %w", table, err)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, fmt.Errorf("journal: close dependency removals from %s: %w", table, err)
|
||||
}
|
||||
}
|
||||
return sortedDependencyEdges(byKey), nil
|
||||
}
|
||||
|
||||
func dependencyEdgeKey(edge journalDependencyEdge) string {
|
||||
return edge.source + "\x00" + edge.target + "\x00" + edge.kind + "\x00" + edge.metadata
|
||||
}
|
||||
|
||||
func sortedDependencyEdges(byKey map[string]journalDependencyEdge) []journalDependencyEdge {
|
||||
edges := make([]journalDependencyEdge, 0, len(byKey))
|
||||
for _, edge := range byKey {
|
||||
edges = append(edges, edge)
|
||||
}
|
||||
sort.Slice(edges, func(i, j int) bool {
|
||||
if edges[i].source != edges[j].source {
|
||||
return edges[i].source < edges[j].source
|
||||
}
|
||||
if edges[i].target != edges[j].target {
|
||||
return edges[i].target < edges[j].target
|
||||
}
|
||||
return edges[i].kind < edges[j].kind
|
||||
})
|
||||
return edges
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ func BuildEventsPruneWhere(before int64, retainDays int, now time.Time, rowsCeil
|
|||
// transaction so it works on every store plumbing (embedded, server, proxied).
|
||||
func ReadEventsInTx(ctx context.Context, tx DBTX, since int64, limit int) ([]storage.EventsJournalRow, error) {
|
||||
// CAST(ts AS CHAR) normalizes the DATETIME to a stable string across drivers.
|
||||
q := `SELECT seq, CAST(ts AS CHAR), op, issue_id, issue_json, dep_json
|
||||
q := `SELECT seq, CAST(ts AS CHAR), op, issue_id, issue_json, dep_json, comment_json
|
||||
FROM bd_events_journal WHERE seq > ? ORDER BY seq ASC`
|
||||
if limit > 0 {
|
||||
q += " LIMIT " + strconv.Itoa(limit)
|
||||
|
|
@ -75,20 +75,43 @@ func ReadEventsInTx(ctx context.Context, tx DBTX, since int64, limit int) ([]sto
|
|||
var out []storage.EventsJournalRow
|
||||
for rows.Next() {
|
||||
var (
|
||||
r storage.EventsJournalRow
|
||||
issueJS sql.NullString
|
||||
depJS sql.NullString
|
||||
r storage.EventsJournalRow
|
||||
issueJS sql.NullString
|
||||
depJS sql.NullString
|
||||
commentJS sql.NullString
|
||||
)
|
||||
if err := rows.Scan(&r.Seq, &r.TS, &r.Op, &r.IssueID, &issueJS, &depJS); err != nil {
|
||||
if err := rows.Scan(&r.Seq, &r.TS, &r.Op, &r.IssueID, &issueJS, &depJS, &commentJS); err != nil {
|
||||
return nil, fmt.Errorf("journal: scan row: %w", err)
|
||||
}
|
||||
r.TS = normalizeEventsTimestamp(r.TS)
|
||||
r.IssueJSON = issueJS.String
|
||||
r.DepJSON = depJS.String
|
||||
r.CommentJSON = commentJS.String
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// normalizeEventsTimestamp emits the journal boundary's stable RFC3339Nano UTC
|
||||
// contract. Dolt/MySQL stringify DATETIME with a space while PostgreSQL may
|
||||
// include a numeric offset; consumers (including Gasworks) require a parsable
|
||||
// UTC timestamp regardless of the backing driver.
|
||||
func normalizeEventsTimestamp(raw string) string {
|
||||
for _, layout := range []string{
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02 15:04:05.999999999Z07:00",
|
||||
"2006-01-02 15:04:05.999999999",
|
||||
"2006-01-02 15:04:05",
|
||||
} {
|
||||
if ts, err := time.Parse(layout, raw); err == nil {
|
||||
return ts.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
}
|
||||
// Preserve an unexpected driver rendering rather than silently changing the
|
||||
// event payload. Normal supported backends are covered by the layouts above.
|
||||
return raw
|
||||
}
|
||||
|
||||
// ComputeEventsPruneWhere resolves the retain-rows floor via readCeil and
|
||||
// returns the full DELETE predicate (without the "WHERE" keyword) plus its bind
|
||||
// args, or skip=true when the whole journal is inside the retained window and
|
||||
|
|
|
|||
|
|
@ -122,3 +122,18 @@ func TestComputeEventsPruneWhere(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNormalizeEventsTimestampRFC3339UTC(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{"2026-07-29 10:11:12", "2026-07-29T10:11:12Z"},
|
||||
{"2026-07-29 10:11:12.123456", "2026-07-29T10:11:12.123456Z"},
|
||||
{"2026-07-29T10:11:12.123456+02:00", "2026-07-29T08:11:12.123456Z"},
|
||||
} {
|
||||
if got := normalizeEventsTimestamp(tc.in); got != tc.want {
|
||||
t.Errorf("normalizeEventsTimestamp(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,12 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/storage/pgdialect"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
|
@ -49,8 +51,7 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
issueops.SetJournalEnabled(true)
|
||||
t.Cleanup(func() { issueops.SetJournalEnabled(false) })
|
||||
st.SetEventsJournalEnabled(true)
|
||||
newIssue := func(id string) *types.Issue {
|
||||
return &types.Issue{ID: id, Title: id, IssueType: "task", Status: types.StatusOpen, Priority: 2}
|
||||
}
|
||||
|
|
@ -60,6 +61,14 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
if err := st.CreateIssue(ctx, newIssue("pgj-2"), "tester"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.RunInTransaction(ctx, "journal transaction labels", func(tx storage.Transaction) error {
|
||||
if err := tx.AddLabel(ctx, "pgj-1", "demo", "tester"); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.RemoveLabel(ctx, "pgj-1", "demo", "tester")
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dep := &types.Dependency{IssueID: "pgj-2", DependsOnID: "pgj-1", Type: types.DepBlocks}
|
||||
if err := st.AddDependency(ctx, dep, "tester"); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -81,7 +90,14 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantOps := []string{"create", "create", "dep_add", "dep_remove", "update", "close", "delete"}
|
||||
wantOps := []string{
|
||||
"create", "create",
|
||||
"update", "update", // transaction label add/remove
|
||||
"dep_add",
|
||||
"update", // derived is_blocked flip after dependency removal
|
||||
"dep_remove",
|
||||
"update", "close", "delete",
|
||||
}
|
||||
if len(rows) != len(wantOps) {
|
||||
t.Fatalf("journal rows=%d, want %d", len(rows), len(wantOps))
|
||||
}
|
||||
|
|
@ -95,11 +111,21 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
}
|
||||
continue
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, row.TS); err != nil || !strings.HasSuffix(row.TS, "Z") {
|
||||
t.Fatalf("row %d timestamp = %q, want RFC3339Nano UTC (parse err=%v)", i, row.TS, err)
|
||||
}
|
||||
var issue types.Issue
|
||||
if err := json.Unmarshal([]byte(row.IssueJSON), &issue); err != nil {
|
||||
t.Fatalf("row %d issue payload is not canonical issue JSON: %v", i, err)
|
||||
}
|
||||
}
|
||||
var derived types.Issue
|
||||
if err := json.Unmarshal([]byte(rows[5].IssueJSON), &derived); err != nil {
|
||||
t.Fatalf("derived readiness payload: %v", err)
|
||||
}
|
||||
if derived.ID != "pgj-2" || derived.IsBlocked {
|
||||
t.Fatalf("derived readiness update = id %q blocked=%v, want pgj-2 unblocked", derived.ID, derived.IsBlocked)
|
||||
}
|
||||
|
||||
// Mutation and journal insert share one SQL transaction: rolling back both
|
||||
// cannot leave either a changed issue or an event row behind.
|
||||
|
|
@ -111,7 +137,7 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
_ = tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := issueops.RecordEventInTx(ctx, tx, issueops.EventUpdate, "pgj-1"); err != nil {
|
||||
if err := issueops.RecordEventInTx(issueops.WithEventsJournal(ctx, true), tx, issueops.EventUpdate, "pgj-1"); err != nil {
|
||||
_ = tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -126,6 +152,23 @@ func TestPostgresEventsJournalContract(t *testing.T) {
|
|||
t.Fatalf("rollback added journal rows: got %d, want %d", len(rowsAfterRollback), len(rows))
|
||||
}
|
||||
|
||||
// Recovery must seed the counter from the journal high-water mark rather
|
||||
// than restart at one. This is the PostgreSQL crash/operator-repair path;
|
||||
// a stale/missing counter must never collide with an existing source cursor.
|
||||
if _, err := st.DB().ExecContext(ctx, "DELETE FROM bd_events_seq"); err != nil {
|
||||
t.Fatalf("remove counter for recovery proof: %v", err)
|
||||
}
|
||||
if err := st.CreateIssue(ctx, newIssue("pgj-recovered-counter"), "tester"); err != nil {
|
||||
t.Fatalf("create after counter recovery: %v", err)
|
||||
}
|
||||
recoveredRows, err := st.ReadEventsJournal(ctx, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := recoveredRows[len(recoveredRows)-1].Seq, int64(len(rows)+1); got != want {
|
||||
t.Fatalf("recovered counter seq = %d, want journal high-water + 1 = %d", got, want)
|
||||
}
|
||||
|
||||
const writers = 8
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, writers)
|
||||
|
|
|
|||
|
|
@ -446,14 +446,18 @@ CREATE INDEX IF NOT EXISTS idx_repo_mtimes_checked ON repo_mtimes (last_checked)
|
|||
-- its counter increment as well and sequence numbers stay commit-visible and
|
||||
-- gapless.
|
||||
CREATE TABLE IF NOT EXISTS bd_events_journal (
|
||||
seq bigint NOT NULL PRIMARY KEY,
|
||||
ts timestamp(6) NOT NULL,
|
||||
op text NOT NULL,
|
||||
issue_id text NOT NULL,
|
||||
issue_json text,
|
||||
dep_json text
|
||||
seq bigint NOT NULL PRIMARY KEY,
|
||||
ts timestamp(6) NOT NULL,
|
||||
op text NOT NULL,
|
||||
issue_id text NOT NULL,
|
||||
issue_json text,
|
||||
dep_json text,
|
||||
comment_json text
|
||||
);
|
||||
|
||||
ALTER TABLE bd_events_journal
|
||||
ADD COLUMN IF NOT EXISTS comment_json text;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bd_events_journal_issue ON bd_events_journal (issue_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bd_events_seq (
|
||||
|
|
|
|||
|
|
@ -55,6 +55,110 @@ func TestInitSchema(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestInitSchemaAddsJournalToExistingVersionOneSchema models a Hosted project
|
||||
// provisioned by the pre-journal binary. PostgreSQL has no migrator in this
|
||||
// integration line, so reopening the same schema version must apply the new
|
||||
// additive DDL without requiring recreation or changing the version stamp.
|
||||
func TestInitSchemaAddsJournalToExistingVersionOneSchema(t *testing.T) {
|
||||
url := os.Getenv("BEADS_PG_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_PG_TEST_URL not set; skipping Postgres schema upgrade test")
|
||||
}
|
||||
|
||||
schema := fmt.Sprintf("bd_schema_journal_upgrade_%d", time.Now().UnixNano())
|
||||
raw, err := pgdialect.OpenRaw(url, schema)
|
||||
if err != nil {
|
||||
t.Fatalf("openraw: %v", err)
|
||||
}
|
||||
db, err := pgdialect.Open(url, schema)
|
||||
if err != nil {
|
||||
_ = raw.Close()
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = raw.Close() })
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
ctx := context.Background()
|
||||
if err := InitSchema(ctx, raw, schema); err != nil {
|
||||
t.Fatalf("initial InitSchema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if _, err := db.ExecContext(ctx, `DROP SCHEMA IF EXISTS "`+schema+`" CASCADE`); err != nil {
|
||||
t.Errorf("drop schema %q: %v", schema, err)
|
||||
}
|
||||
})
|
||||
if _, err := raw.ExecContext(ctx, "DROP TABLE bd_events_journal, bd_events_seq"); err != nil {
|
||||
t.Fatalf("model pre-journal schema: %v", err)
|
||||
}
|
||||
|
||||
var before string
|
||||
if err := raw.QueryRowContext(ctx, `SELECT value FROM metadata WHERE key = 'pg_schema_version'`).Scan(&before); err != nil {
|
||||
t.Fatalf("read pre-upgrade schema version: %v", err)
|
||||
}
|
||||
if err := InitSchema(ctx, raw, schema); err != nil {
|
||||
t.Fatalf("upgrade InitSchema: %v", err)
|
||||
}
|
||||
for _, table := range []string{"bd_events_journal", "bd_events_seq"} {
|
||||
if !tableExists(ctx, t, db, schema, table) {
|
||||
t.Errorf("upgrade did not restore table %q", table)
|
||||
}
|
||||
}
|
||||
var after string
|
||||
if err := raw.QueryRowContext(ctx, `SELECT value FROM metadata WHERE key = 'pg_schema_version'`).Scan(&after); err != nil {
|
||||
t.Fatalf("read post-upgrade schema version: %v", err)
|
||||
}
|
||||
if before != schemaVersion || after != schemaVersion {
|
||||
t.Fatalf("schema version changed across additive journal upgrade: before=%q after=%q want=%q", before, after, schemaVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInitSchemaAddsCommentPayloadToExistingJournal models the intermediate
|
||||
// Hosted schema created by the contributor patch: the journal table exists,
|
||||
// but predates the additive comment_json payload column. CREATE TABLE IF NOT
|
||||
// EXISTS cannot repair that shape, so every open must apply an idempotent ALTER.
|
||||
func TestInitSchemaAddsCommentPayloadToExistingJournal(t *testing.T) {
|
||||
url := os.Getenv("BEADS_PG_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_PG_TEST_URL not set; skipping Postgres journal-column upgrade test")
|
||||
}
|
||||
|
||||
schema := fmt.Sprintf("bd_schema_journal_column_upgrade_%d", time.Now().UnixNano())
|
||||
raw, err := pgdialect.OpenRaw(url, schema)
|
||||
if err != nil {
|
||||
t.Fatalf("openraw: %v", err)
|
||||
}
|
||||
db, err := pgdialect.Open(url, schema)
|
||||
if err != nil {
|
||||
_ = raw.Close()
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = raw.Close() })
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
ctx := context.Background()
|
||||
if err := InitSchema(ctx, raw, schema); err != nil {
|
||||
t.Fatalf("initial InitSchema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if _, err := db.ExecContext(ctx, `DROP SCHEMA IF EXISTS "`+schema+`" CASCADE`); err != nil {
|
||||
t.Errorf("drop schema %q: %v", schema, err)
|
||||
}
|
||||
})
|
||||
if _, err := raw.ExecContext(ctx, "ALTER TABLE bd_events_journal DROP COLUMN comment_json"); err != nil {
|
||||
t.Fatalf("model pre-comment journal: %v", err)
|
||||
}
|
||||
if columnExists(ctx, t, db, schema, "bd_events_journal", "comment_json") {
|
||||
t.Fatal("pre-upgrade journal unexpectedly has comment_json")
|
||||
}
|
||||
|
||||
if err := InitSchema(ctx, raw, schema); err != nil {
|
||||
t.Fatalf("upgrade InitSchema: %v", err)
|
||||
}
|
||||
if !columnExists(ctx, t, db, schema, "bd_events_journal", "comment_json") {
|
||||
t.Fatal("upgrade did not add bd_events_journal.comment_json")
|
||||
}
|
||||
}
|
||||
|
||||
func tableExists(ctx context.Context, t *testing.T, db *sql.DB, schema, table string) bool {
|
||||
t.Helper()
|
||||
var count int
|
||||
|
|
@ -66,3 +170,17 @@ func tableExists(ctx context.Context, t *testing.T, db *sql.DB, schema, table st
|
|||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func columnExists(ctx context.Context, t *testing.T, db *sql.DB, schema, table, column string) bool {
|
||||
t.Helper()
|
||||
var count int
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ? AND table_name = ? AND column_name = ?`,
|
||||
schema, table, column).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query information_schema.columns for %q.%q: %v", table, column, err)
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ CREATE TABLE __temp__bd_events_journal (
|
|||
issue_id VARCHAR(255) NOT NULL,
|
||||
issue_json LONGTEXT,
|
||||
dep_json TEXT,
|
||||
comment_json TEXT,
|
||||
INDEX idx_bd_events_journal_issue (issue_id)
|
||||
);
|
||||
SET @exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'bd_events_journal');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
-- The journal is clone-local, so add the replayable comment payload column
|
||||
-- through the ignored migration stream. Existing clones already carrying the
|
||||
-- journal table receive this additive upgrade; fresh clones get it from 0016.
|
||||
SET @comment_json_exists = (
|
||||
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'bd_events_journal'
|
||||
AND COLUMN_NAME = 'comment_json'
|
||||
);
|
||||
SET @comment_json_sql = IF(
|
||||
@comment_json_exists = 0,
|
||||
'ALTER TABLE bd_events_journal ADD COLUMN comment_json TEXT',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @comment_json_sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
|
|
@ -12,6 +12,7 @@ import (
|
|||
// current UTC time. issueops verifies the issue exists and wisp-routes.
|
||||
func (s *Store) AddIssueComment(ctx context.Context, issueID, author, text string) (*types.Comment, error) {
|
||||
var result *types.Comment
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
var e error
|
||||
result, e = issueops.AddIssueCommentInTx(ctx, tx, issueID, author, text)
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ func (s *Store) GetNewlyUnblockedByClose(ctx context.Context, closedIssueID stri
|
|||
// only if it is currently unassigned. Runs in a mutation tx so the claim and its
|
||||
// is_blocked reprojection commit atomically.
|
||||
func (s *Store) ClaimIssue(ctx context.Context, id string, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
_, err := issueops.ClaimIssueInTx(ctx, tx, id, actor)
|
||||
return err
|
||||
|
|
@ -64,6 +65,7 @@ func (s *Store) ClaimIssue(ctx context.Context, id string, actor string) error {
|
|||
// returns (nil, nil) when nothing is ready.
|
||||
func (s *Store) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error) {
|
||||
var claimed *types.Issue
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
var e error
|
||||
claimed, e = issueops.ClaimReadyIssueInTx(ctx, tx, filter, actor)
|
||||
|
|
@ -85,6 +87,7 @@ func (s *Store) DeleteIssues(ctx context.Context, ids []string, cascade bool, fo
|
|||
return &types.DeleteIssuesResult{}, nil
|
||||
}
|
||||
var result *types.DeleteIssuesResult
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
r, e := issueops.DeleteIssuesInTx(ctx, tx, ids, cascade, force, dryRun)
|
||||
result = r
|
||||
|
|
@ -97,6 +100,7 @@ func (s *Store) DeleteIssues(ctx context.Context, ids []string, cascade bool, fo
|
|||
// bumping the child_counters row in the same tx.
|
||||
func (s *Store) GetNextChildID(ctx context.Context, parentID string) (string, error) {
|
||||
var childID string
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
var e error
|
||||
childID, e = issueops.GetNextChildIDTx(ctx, tx, parentID)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import (
|
|||
// validates, cycle-checks, and inserts. Running everything in withMutationTx
|
||||
// commits the edge and its is_blocked reprojection atomically.
|
||||
func (s *Store) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
isCrossPrefix := types.ExtractPrefix(dep.IssueID) != types.ExtractPrefix(dep.DependsOnID)
|
||||
|
||||
|
|
@ -49,6 +50,7 @@ func (s *Store) AddDependency(ctx context.Context, dep *types.Dependency, actor
|
|||
// wisp routing internally, so both source classes collapse to one call. actor
|
||||
// is unused (kept to satisfy the storage.Storage signature).
|
||||
func (s *Store) RemoveDependency(ctx context.Context, issueID, dependsOnID string, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ func (s *Store) CreateIssue(ctx context.Context, issue *types.Issue, actor strin
|
|||
if issue == nil {
|
||||
return fmt.Errorf("issue must not be nil")
|
||||
}
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
// Route to wisps if ephemeral, no-history, or an infra type. Infra types
|
||||
// get marked ephemeral (legacy behavior); TableRouting inside issueops
|
||||
|
|
@ -49,6 +50,7 @@ func (s *Store) CreateIssues(ctx context.Context, issues []*types.Issue, actor s
|
|||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
_, err := issueops.CreateIssuesInTxWithResult(ctx, tx, issues, actor, storage.BatchCreateOptions{
|
||||
OrphanHandling: storage.OrphanAllow,
|
||||
|
|
@ -115,6 +117,7 @@ func (s *Store) UpdateIssue(ctx context.Context, id string, updates map[string]i
|
|||
return err
|
||||
}
|
||||
}
|
||||
ctx = s.journalContext(ctx)
|
||||
|
||||
// Setting no_history/wisp on a durable issue is an IN-PLACE column write on the
|
||||
// embedded-Dolt reference (the default backend + our oracle): updateIssueInTx routes
|
||||
|
|
@ -158,6 +161,7 @@ func (s *Store) ReopenIssue(ctx context.Context, id string, reason string, actor
|
|||
// DeleteIssue permanently removes an issue. issueops routes wisps internally and
|
||||
// recomputes is_blocked for affected neighbors.
|
||||
func (s *Store) DeleteIssue(ctx context.Context, id string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.DeleteIssueInTx(ctx, tx, id)
|
||||
})
|
||||
|
|
@ -167,6 +171,7 @@ func (s *Store) DeleteIssue(ctx context.Context, id string) error {
|
|||
// wisps are ephemeral and never leased, so heartbeating one is ErrNotClaimable;
|
||||
// otherwise delegate to issueops.HeartbeatIssueInTx.
|
||||
func (s *Store) HeartbeatIssue(ctx context.Context, id, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
if issueops.IsActiveWispInTx(ctx, tx, id) {
|
||||
return fmt.Errorf("%w: %s is ephemeral", storage.ErrNotClaimable, id)
|
||||
|
|
@ -181,6 +186,7 @@ func (s *Store) HeartbeatIssue(ctx context.Context, id, actor string) error {
|
|||
func (s *Store) ReclaimExpiredLeases(ctx context.Context, olderThan time.Duration, actor string) ([]types.ReclaimedLease, error) {
|
||||
cutoff := time.Now().UTC().Add(-olderThan)
|
||||
var reclaimed []types.ReclaimedLease
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
var err error
|
||||
reclaimed, err = issueops.ReclaimExpiredLeasesInTx(ctx, tx, cutoff, actor)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
// auto-route between the permanent and wisp label/event tables. Labels cannot
|
||||
// change is_blocked, so this uses withWriteTx.
|
||||
func (s *Store) AddLabel(ctx context.Context, issueID, label, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.AddLabelInTx(ctx, tx, "", "", issueID, label, actor)
|
||||
})
|
||||
|
|
@ -19,6 +20,7 @@ func (s *Store) AddLabel(ctx context.Context, issueID, label, actor string) erro
|
|||
|
||||
// RemoveLabel removes a label from an issue. issueops handles wisp routing.
|
||||
func (s *Store) RemoveLabel(ctx context.Context, issueID, label, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.RemoveLabelInTx(ctx, tx, "", "", issueID, label, actor)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -146,6 +146,7 @@ func (s *Store) FindWispDependentsRecursive(ctx context.Context, ids []string) (
|
|||
// --- Comment / audit writes ---
|
||||
|
||||
func (s *Store) AddComment(ctx context.Context, issueID, actor, comment string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.AddCommentEventInTx(ctx, tx, issueID, actor, comment)
|
||||
})
|
||||
|
|
@ -153,6 +154,7 @@ func (s *Store) AddComment(ctx context.Context, issueID, actor, comment string)
|
|||
|
||||
func (s *Store) ImportIssueComment(ctx context.Context, issueID, author, text string, createdAt time.Time) (*types.Comment, error) {
|
||||
var out *types.Comment
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
var e error
|
||||
out, e = issueops.ImportIssueCommentInTx(ctx, tx, issueID, author, text, createdAt)
|
||||
|
|
@ -164,12 +166,14 @@ func (s *Store) ImportIssueComment(ctx context.Context, issueID, author, text st
|
|||
// --- Id rekey / wisp promote / source-repo purge ---
|
||||
|
||||
func (s *Store) UpdateIssueID(ctx context.Context, oldID, newID string, issue *types.Issue, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.UpdateIssueIDInTx(ctx, tx, oldID, newID, issue, actor)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) PromoteFromEphemeral(ctx context.Context, id, actor string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
return issueops.PromoteFromEphemeralInTx(ctx, tx, id, actor)
|
||||
})
|
||||
|
|
@ -177,6 +181,7 @@ func (s *Store) PromoteFromEphemeral(ctx context.Context, id, actor string) erro
|
|||
|
||||
func (s *Store) DeleteIssuesBySourceRepo(ctx context.Context, sourceRepo string) (int, error) {
|
||||
var count int
|
||||
ctx = s.journalContext(ctx)
|
||||
err := s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
var e error
|
||||
count, e = issueops.DeleteIssuesBySourceRepoInTx(ctx, tx, sourceRepo)
|
||||
|
|
@ -196,6 +201,7 @@ func (s *Store) CreateIssuesWithFullOptions(ctx context.Context, issues []*types
|
|||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
ctx = s.journalContext(ctx)
|
||||
if issueops.AllWisps(issues) {
|
||||
for _, issue := range issues {
|
||||
if !issue.NoHistory {
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ type Store struct {
|
|||
dialect Dialect
|
||||
readiness ReadinessStrategy
|
||||
claim ClaimStrategy
|
||||
// eventsJournalEnabled is intentionally per Store, never process-global:
|
||||
// Hosted may serve several project schemas from one process.
|
||||
eventsJournalEnabled atomic.Bool
|
||||
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
|
@ -58,6 +61,15 @@ func New(ctx context.Context, cfg Config) (*Store, error) {
|
|||
// DB exposes the underlying pool for diagnostics/migrations.
|
||||
func (s *Store) DB() *sql.DB { return s.db }
|
||||
|
||||
// SetEventsJournalEnabled activates the journal for this store instance only.
|
||||
// It is used by the Hosted OpenServerPostgres entry point; ordinary CLI/config
|
||||
// opens remain safe-off unless their own store explicitly opts in.
|
||||
func (s *Store) SetEventsJournalEnabled(enabled bool) { s.eventsJournalEnabled.Store(enabled) }
|
||||
|
||||
func (s *Store) journalContext(ctx context.Context) context.Context {
|
||||
return issueops.WithEventsJournal(ctx, s.eventsJournalEnabled.Load())
|
||||
}
|
||||
|
||||
// DialectName reports the backend's dialect ("postgres", "sqlite", …).
|
||||
func (s *Store) DialectName() string { return s.dialect.Name() }
|
||||
|
||||
|
|
@ -92,6 +104,8 @@ func (s *Store) withWriteTx(ctx context.Context, fn func(tx *sql.Tx) error) erro
|
|||
if err != nil {
|
||||
return fmt.Errorf("begin write tx: %w", err)
|
||||
}
|
||||
clearJournalScope := issueops.ScopeEventsJournalTransaction(tx, s.eventsJournalEnabled.Load())
|
||||
defer clearJournalScope()
|
||||
// A panicking callback must not pin the pooled connection; Rollback after
|
||||
// Commit (or the explicit Rollback below) is a no-op.
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
|
@ -107,6 +121,7 @@ func (s *Store) withWriteTx(ctx context.Context, fn func(tx *sql.Tx) error) erro
|
|||
// withMutationTx runs fn then the readiness strategy in ONE transaction, so an
|
||||
// issue/dependency mutation and its is_blocked reprojection commit atomically.
|
||||
func (s *Store) withMutationTx(ctx context.Context, fn func(tx *sql.Tx) error) error {
|
||||
ctx = issueops.WithEventsJournal(ctx, s.eventsJournalEnabled.Load())
|
||||
return s.withWriteTx(ctx, func(tx *sql.Tx) error {
|
||||
if err := fn(tx); err != nil {
|
||||
return err
|
||||
|
|
@ -176,6 +191,7 @@ func (s *Store) GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*t
|
|||
|
||||
// CloseIssue closes an issue and reprojects blocked-ness in the same tx.
|
||||
func (s *Store) CloseIssue(ctx context.Context, id, reason, actor, session string) error {
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
_, err := issueops.CloseIssueInTx(ctx, tx, id, reason, actor, session)
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import (
|
|||
// commit-message residue and intentionally unused.
|
||||
func (s *Store) RunInTransaction(ctx context.Context, commitMsg string, fn func(tx storage.Transaction) error) error {
|
||||
_ = commitMsg
|
||||
ctx = s.journalContext(ctx)
|
||||
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
|
||||
// withWriteTx rolls back on a returned error but not on a panic; guard
|
||||
// it here so a panicking callback still releases the transaction.
|
||||
|
|
@ -206,27 +207,30 @@ func (t *sqlkitTx) CycleThroughEdges(ctx context.Context, edges [][2]string) (st
|
|||
return issueops.CycleThroughEdgesInGraph(graph, edges), nil
|
||||
}
|
||||
|
||||
// AddLabel adds a label with a bare INSERT IGNORE and no event row, mirroring
|
||||
// the Dolt reference transaction (dolt/transaction.go). Transaction label ops
|
||||
// are unevented on both backends; the evented store path lives in labels.go.
|
||||
// AddLabel adds a label without writing the user-facing events table. The
|
||||
// durable mutation journal still receives the post-mutation issue snapshot.
|
||||
func (t *sqlkitTx) AddLabel(ctx context.Context, issueID, label, actor string) error {
|
||||
_, labelTable, _, _ := issueops.WispTableRouting(issueops.IsActiveWispInTx(ctx, t.tx, issueID))
|
||||
//nolint:gosec // G201: labelTable is a fixed routing constant
|
||||
_, err := t.tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
if _, err := t.tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
INSERT IGNORE INTO %s (issue_id, label) VALUES (?, ?)
|
||||
`, labelTable), issueID, label)
|
||||
return err
|
||||
`, labelTable), issueID, label); err != nil {
|
||||
return err
|
||||
}
|
||||
return issueops.RecordEventInTx(ctx, t.tx, issueops.EventUpdate, issueID)
|
||||
}
|
||||
|
||||
// RemoveLabel removes a label with a bare DELETE and no event row, mirroring
|
||||
// the Dolt reference transaction (dolt/transaction.go).
|
||||
// RemoveLabel removes a label without writing the user-facing events table.
|
||||
// The durable mutation journal still receives the post-mutation issue snapshot.
|
||||
func (t *sqlkitTx) RemoveLabel(ctx context.Context, issueID, label, actor string) error {
|
||||
_, labelTable, _, _ := issueops.WispTableRouting(issueops.IsActiveWispInTx(ctx, t.tx, issueID))
|
||||
//nolint:gosec // G201: labelTable is a fixed routing constant
|
||||
_, err := t.tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
if _, err := t.tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
DELETE FROM %s WHERE issue_id = ? AND label = ?
|
||||
`, labelTable), issueID, label)
|
||||
return err
|
||||
`, labelTable), issueID, label); err != nil {
|
||||
return err
|
||||
}
|
||||
return issueops.RecordEventInTx(ctx, t.tx, issueops.EventUpdate, issueID)
|
||||
}
|
||||
|
||||
// GetLabels returns an issue's labels; empty table arg auto-routes wisps.
|
||||
|
|
|
|||
|
|
@ -225,15 +225,17 @@ type RawDBAccessor interface {
|
|||
|
||||
// EventsJournalRow is one raw bd_events_journal row surfaced to the
|
||||
// `bd events` CLI. IssueJSON is empty when the op is a delete (no surviving
|
||||
// row); DepJSON is empty for non-dependency ops. TS is the insert-time timestamp
|
||||
// row); DepJSON is empty for non-dependency ops; CommentJSON is empty for
|
||||
// non-comment ops. TS is the insert-time timestamp
|
||||
// (stamped inside the committing transaction) normalized to a string.
|
||||
type EventsJournalRow struct {
|
||||
Seq int64
|
||||
TS string
|
||||
Op string
|
||||
IssueID string
|
||||
IssueJSON string
|
||||
DepJSON string
|
||||
Seq int64
|
||||
TS string
|
||||
Op string
|
||||
IssueID string
|
||||
IssueJSON string
|
||||
DepJSON string
|
||||
CommentJSON string
|
||||
}
|
||||
|
||||
// EventsJournalAccessor reads and prunes the durable events journal
|
||||
|
|
@ -251,6 +253,13 @@ type EventsJournalAccessor interface {
|
|||
PruneEventsJournal(ctx context.Context, before int64, retainDays, retainRows int) (int64, error)
|
||||
}
|
||||
|
||||
// EventsJournalConfigurer controls journal activation on one storage instance.
|
||||
// Implementations must never use process-global state: Hosted serves multiple
|
||||
// project stores concurrently in one process.
|
||||
type EventsJournalConfigurer interface {
|
||||
SetEventsJournalEnabled(enabled bool)
|
||||
}
|
||||
|
||||
// StoreLocator provides filesystem path information for the store.
|
||||
// Callers that need the store's on-disk location should type-assert to this interface.
|
||||
type StoreLocator interface {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ func (u *stubUOW) IssueUseCase() domain.IssueUseCase { panic("not impl
|
|||
func (u *stubUOW) DependencyUseCase() domain.DependencyUseCase { panic("not implemented") }
|
||||
func (u *stubUOW) LabelUseCase() domain.LabelUseCase { panic("not implemented") }
|
||||
func (u *stubUOW) CommentUseCase() domain.CommentUseCase { panic("not implemented") }
|
||||
func (u *stubUOW) EventsJournalUseCase() domain.EventsJournalUseCase {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
// controlledProvider lets tests inject errors per-attempt.
|
||||
type controlledProvider struct {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ type UnitOfWork interface {
|
|||
DependencyUseCase() domain.DependencyUseCase
|
||||
LabelUseCase() domain.LabelUseCase
|
||||
CommentUseCase() domain.CommentUseCase
|
||||
EventsJournalUseCase() domain.EventsJournalUseCase
|
||||
}
|
||||
|
||||
type UnitOfWorkProvider interface {
|
||||
|
|
@ -45,6 +46,7 @@ type baseUOW struct {
|
|||
dependencyUseCase domain.DependencyUseCase
|
||||
labelUseCase domain.LabelUseCase
|
||||
commentUseCase domain.CommentUseCase
|
||||
journalUseCase domain.EventsJournalUseCase
|
||||
}
|
||||
|
||||
func (u *baseUOW) Commit(ctx context.Context, message string) error {
|
||||
|
|
@ -117,3 +119,10 @@ func (u *baseUOW) CommentUseCase() domain.CommentUseCase {
|
|||
}
|
||||
return u.commentUseCase
|
||||
}
|
||||
|
||||
func (u *baseUOW) EventsJournalUseCase() domain.EventsJournalUseCase {
|
||||
if u.journalUseCase == nil {
|
||||
u.journalUseCase = domain.NewEventsJournalUseCase(db.NewEventsJournalSQLRepository(u.tx.Runner()))
|
||||
}
|
||||
return u.journalUseCase
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,9 @@ type Issue struct {
|
|||
Status Status `json:"status,omitempty"`
|
||||
Priority int `json:"priority"` // No omitempty: 0 is valid (P0/critical)
|
||||
IssueType IssueType `json:"issue_type,omitempty"`
|
||||
// IsBlocked is the persisted readiness projection. It is included in journal
|
||||
// snapshots so graph deltas can be replayed without recomputing readiness.
|
||||
IsBlocked bool `json:"is_blocked"`
|
||||
|
||||
// ===== Assignment =====
|
||||
Assignee string `json:"assignee,omitempty"`
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/postgres"
|
||||
)
|
||||
|
||||
|
|
@ -24,6 +25,11 @@ type PostgresServerConfig struct {
|
|||
// search_path on every connection. In hosted deployments routing decides
|
||||
// this value; clients never do.
|
||||
Schema string
|
||||
|
||||
// EventsJournal enables the durable bd events journal for this project
|
||||
// instance only. It is intentionally an embedding-server setting rather
|
||||
// than a process-global or CLI config switch.
|
||||
EventsJournal bool
|
||||
}
|
||||
|
||||
// OpenServerPostgres opens the beads engine against an external Postgres
|
||||
|
|
@ -40,5 +46,15 @@ func OpenServerPostgres(ctx context.Context, cfg PostgresServerConfig) (Storage,
|
|||
if cfg.Schema == "" {
|
||||
return nil, fmt.Errorf("beads: OpenServerPostgres requires a Schema")
|
||||
}
|
||||
return postgres.Provision(ctx, cfg.DSN, cfg.Schema)
|
||||
st, err := postgres.Provision(ctx, cfg.DSN, cfg.Schema)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
journalConfig, ok := st.(storage.EventsJournalConfigurer)
|
||||
if !ok {
|
||||
_ = st.Close()
|
||||
return nil, fmt.Errorf("beads: PostgreSQL store does not support events-journal configuration")
|
||||
}
|
||||
journalConfig.SetEventsJournalEnabled(cfg.EventsJournal)
|
||||
return st, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,6 +170,73 @@ func TestOpenServerPostgres(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// TestOpenServerPostgresEventsJournalIsProjectScoped proves Hosted enables the
|
||||
// durable journal on the explicitly selected project only. Opening another
|
||||
// project in the same process must not inherit that activation.
|
||||
func TestOpenServerPostgresEventsJournalIsProjectScoped(t *testing.T) {
|
||||
url := pgTestURL(t)
|
||||
ctx := context.Background()
|
||||
enabledSchema := uniquePGSchema("open_journal_enabled")
|
||||
disabledSchema := uniquePGSchema("open_journal_disabled")
|
||||
t.Cleanup(func() {
|
||||
dropPGSchema(url, enabledSchema)
|
||||
dropPGSchema(url, disabledSchema)
|
||||
})
|
||||
|
||||
enabled, err := OpenServerPostgres(ctx, PostgresServerConfig{
|
||||
DSN: url, Schema: enabledSchema, EventsJournal: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("OpenServerPostgres(enabled): %v", err)
|
||||
}
|
||||
defer func() { _ = enabled.Close() }()
|
||||
disabled, err := OpenServerPostgres(ctx, PostgresServerConfig{DSN: url, Schema: disabledSchema})
|
||||
if err != nil {
|
||||
t.Fatalf("OpenServerPostgres(disabled): %v", err)
|
||||
}
|
||||
defer func() { _ = disabled.Close() }()
|
||||
|
||||
enabledJournal, ok := enabled.(EventsJournalAccessor)
|
||||
if !ok {
|
||||
t.Fatal("OpenServerPostgres result does not expose the public events-journal capability")
|
||||
}
|
||||
disabledJournal, ok := disabled.(EventsJournalAccessor)
|
||||
if !ok {
|
||||
t.Fatal("disabled OpenServerPostgres result does not expose the public events-journal capability")
|
||||
}
|
||||
enabledStore := enabled.(storage.DoltStorage)
|
||||
disabledStore := disabled.(storage.DoltStorage)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
store storage.DoltStorage
|
||||
}{
|
||||
{"enabled", enabledStore},
|
||||
{"disabled", disabledStore},
|
||||
} {
|
||||
if err := tc.store.SetConfig(ctx, "issue_prefix", tc.name); err != nil {
|
||||
t.Fatalf("SetConfig(%s): %v", tc.name, err)
|
||||
}
|
||||
if err := tc.store.CreateIssue(ctx, &types.Issue{ID: tc.name + "-1", Title: tc.name, IssueType: "task", Status: types.StatusOpen, Priority: 2}, "tester"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", tc.name, err)
|
||||
}
|
||||
}
|
||||
|
||||
rows, err := enabledJournal.ReadEventsJournal(ctx, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("enabled ReadEventsJournal: %v", err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].IssueID != "enabled-1" {
|
||||
t.Fatalf("enabled journal rows = %#v, want its one project-local mutation", rows)
|
||||
}
|
||||
rows, err = disabledJournal.ReadEventsJournal(ctx, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("disabled ReadEventsJournal: %v", err)
|
||||
}
|
||||
if len(rows) != 0 {
|
||||
t.Fatalf("disabled project inherited journal activation: %#v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConformanceViaOpenServerPostgres runs the backend-agnostic storage
|
||||
// conformance suite through the *hosted* entrypoint (OpenServerPostgres) rather
|
||||
// than the internal postgres package the CI already covers, certifying the public
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue