From d44d399bdbdb80c33b3a0aed03880eebfaee1acb Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Wed, 19 Aug 2026 20:40:34 -0700 Subject: [PATCH] fix(storage): stop reporting a live issue absent when the leases table is missing (#5804) getIssueFromTableInTx folded any table-not-exist error into storage.ErrNotFound. That tolerance was written for the optional wisps plane, which pre-migration databases do not have. Lease state now lives in its own table and sqlbuild.LeaseJoin is unconditionally in the same FROM clause, so a database missing 'leases' misfires the tolerance: GetIssue answers "no such issue" for a row that is demonstrably present, and the mutation path inherits it -- updating a live issue reports it gone. A client cannot tell that apart from a deletion, so a sync or reconciler treating 404 as "gone" would recreate or tombstone live work. The sibling reads (SearchIssues, GetReadyWorkWithCounts) already fail loudly on the identical condition; getIssue is the outlier. Narrow the tolerance to the table it was written for: it now fires only when the error names the optional wisps plane the query just read. dberrors gains MissingTableName/IsMissingTable, both gated on the existing IsTableNotExist classification, so no error IsTableNotExist rejects becomes newly tolerated -- this is a pure tightening, not a reclassification. Latent on an up-to-date database, where leases exists; it arms on any schema skew, which is exactly when an operator can least tell a wrong answer from a real one. Tests are red-first against the unfixed code. Coverage is the read path and the mutation path in sqlmock, an end-to-end RENAME TABLE leases against real embedded Dolt with a raw-SQL control proving the row is present, and two controls in the other direction: a pre-migration database missing 'wisps' must still answer not-found, and an id absent from both planes must still answer not-found. --- internal/storage/dberrors/errors.go | 44 ++++++ internal/storage/dberrors/errors_test.go | 99 +++++++++++++ .../storage/embeddeddolt/get_issue_test.go | 46 ++++++ internal/storage/issueops/get_issue.go | 11 +- internal/storage/issueops/get_issue_test.go | 136 ++++++++++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 internal/storage/dberrors/errors_test.go create mode 100644 internal/storage/issueops/get_issue_test.go diff --git a/internal/storage/dberrors/errors.go b/internal/storage/dberrors/errors.go index 3e0589c8e..5811a44e2 100644 --- a/internal/storage/dberrors/errors.go +++ b/internal/storage/dberrors/errors.go @@ -41,6 +41,50 @@ func IsTableNotExist(err error) bool { unquotedTableMissingPattern.MatchString(s) } +var missingTableNamePatterns = []*regexp.Regexp{ + // go-mysql-server (embedded Dolt and sql-server alike): "table not found: x". + regexp.MustCompile("(?i)table not found:\\s*`?([^\\s'`,;]+)`?"), + // Stock MySQL: "Table 'schema.x' doesn't exist". + regexp.MustCompile(`(?i)\btable\s+'([^']+)'\s+(?:doesn't exist|does not exist)\b`), + // Same, unquoted or backticked. + regexp.MustCompile("(?i)\\btable\\s+`?([^\\s'`]+)`?\\s+(?:doesn't exist|does not exist)\\b"), +} + +// MissingTableName returns the table named by a table-not-exist error, with any +// schema qualifier stripped. It reports false when err is not a table-not-exist +// error (as IsTableNotExist classifies it) or when the message names no table, +// so callers that tolerate one specific optional table cannot accidentally +// tolerate a different missing one. +func MissingTableName(err error) (string, bool) { + if !IsTableNotExist(err) { + return "", false + } + for _, pattern := range missingTableNamePatterns { + m := pattern.FindStringSubmatch(err.Error()) + if m == nil { + continue + } + name := strings.Trim(m[1], "`'\"") + if i := strings.LastIndex(name, "."); i >= 0 { + name = name[i+1:] + } + if name != "" { + return name, true + } + } + return "", false +} + +// IsMissingTable reports whether err is a table-not-exist error naming exactly +// the given table. Use it instead of IsTableNotExist wherever a query's FROM +// clause spans more than one table and only one of them is optional: a blanket +// table-not-exist check over a joined FROM tolerates the absence of tables the +// tolerance was never written for. +func IsMissingTable(err error, table string) bool { + name, ok := MissingTableName(err) + return ok && strings.EqualFold(name, table) +} + // IsAccessDenied reports whether err is a MySQL/Dolt privilege refusal: the // connected user lacks the right to run the statement. Covers 1044 // (ER_DBACCESS_DENIED_ERROR), 1045 (ER_ACCESS_DENIED_ERROR), 1142 diff --git a/internal/storage/dberrors/errors_test.go b/internal/storage/dberrors/errors_test.go new file mode 100644 index 000000000..4ddabb43a --- /dev/null +++ b/internal/storage/dberrors/errors_test.go @@ -0,0 +1,99 @@ +package dberrors + +import ( + "errors" + "fmt" + "testing" + + mysql "github.com/go-sql-driver/mysql" +) + +func TestMissingTableName(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + err error + wantName string + wantOK bool + }{ + {name: "nil", err: nil}, + { + name: "go-mysql-server wording", + err: &mysql.MySQLError{Number: 1146, Message: "table not found: leases"}, + wantName: "leases", + wantOK: true, + }, + { + name: "mysql wording strips the schema qualifier", + err: &mysql.MySQLError{Number: 1146, Message: "Table 'beads.leases' doesn't exist"}, + wantName: "leases", + wantOK: true, + }, + { + name: "backticked", + err: errors.New("Error 1146 (42S02): Table `wisps` does not exist"), + wantName: "wisps", + wantOK: true, + }, + { + name: "wrapped", + err: fmt.Errorf("get issue: %w", &mysql.MySQLError{Number: 1146, Message: "table not found: wisp_labels"}), + wantName: "wisp_labels", + wantOK: true, + }, + { + // A different failure must never be read as a missing table, or the + // narrowed tolerance would be as blind as the blanket one. + name: "unrelated mysql error", + err: &mysql.MySQLError{Number: 1213, Message: "Deadlock found when trying to get lock"}, + }, + { + name: "connection failure", + err: errors.New("dial tcp 127.0.0.1:3306: connect: connection refused"), + }, + { + name: "missing column is not a missing table", + err: &mysql.MySQLError{Number: 1054, Message: "Unknown column 'leases.granted_node' in 'field list'"}, + }, + { + // The IsTableNotExist gate is what makes this a tightening rather + // than a widening: prose that merely reads like a missing table, + // without the classification, must stay untolerated. Drop the gate + // and this case starts returning ("leases", true). + name: "table-not-exist wording alone is not a classification", + err: errors.New("table not found: leases"), + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + got, ok := MissingTableName(tc.err) + if ok != tc.wantOK || got != tc.wantName { + t.Fatalf("MissingTableName(%v) = (%q, %v), want (%q, %v)", tc.err, got, ok, tc.wantName, tc.wantOK) + } + }) + } +} + +func TestIsMissingTable(t *testing.T) { + t.Parallel() + + leasesGone := &mysql.MySQLError{Number: 1146, Message: "table not found: leases"} + + if !IsMissingTable(leasesGone, "leases") { + t.Error("IsMissingTable(leases error, \"leases\") = false, want true") + } + // The whole point of the helper: the table the caller is willing to do + // without is not the table that actually went missing. + if IsMissingTable(leasesGone, "wisps") { + t.Error("IsMissingTable(leases error, \"wisps\") = true, want false") + } + if IsMissingTable(nil, "leases") { + t.Error("IsMissingTable(nil, \"leases\") = true, want false") + } + if !IsMissingTable(&mysql.MySQLError{Number: 1146, Message: "Table 'beads.WISPS' doesn't exist"}, "wisps") { + t.Error("IsMissingTable should match table names case-insensitively") + } +} diff --git a/internal/storage/embeddeddolt/get_issue_test.go b/internal/storage/embeddeddolt/get_issue_test.go index b24e33064..5003ba2d0 100644 --- a/internal/storage/embeddeddolt/get_issue_test.go +++ b/internal/storage/embeddeddolt/get_issue_test.go @@ -105,6 +105,52 @@ func TestGetIssue(t *testing.T) { } }) + // The hydration FROM clause joins the leases table, so a database missing it + // must fail loudly: reporting the row absent is a wrong answer no client can + // tell apart from a deletion. assertRowExists is the control — the row is + // demonstrably present while GetIssue is being asked about it. + t.Run("missing_leases_table_is_an_error_not_absent", func(t *testing.T) { + te := newTestEnv(t, "lg") + ctx := t.Context() + + issue := &types.Issue{ + ID: "lg-live", + Title: "Live issue on a database missing leases", + Status: types.StatusOpen, + Priority: 2, + IssueType: types.TypeTask, + } + if err := te.store.CreateIssue(ctx, issue, "tester"); err != nil { + t.Fatalf("CreateIssue: %v", err) + } + te.exec(t, ctx, "RENAME TABLE leases TO leases_renamed_away") + te.assertRowExists(t, ctx, "issues", "lg-live") + + _, err := te.store.GetIssue(ctx, "lg-live") + if err == nil { + t.Fatal("GetIssue succeeded with no leases table") + } + if errors.Is(err, storage.ErrNotFound) { + t.Fatalf("GetIssue reported a present row absent: %v", err) + } + if !strings.Contains(err.Error(), "leases") { + t.Fatalf("expected the error to name leases, got: %v", err) + } + + // The mutation path reads the pre-update row through the same query, so + // it inherited the same wrong answer: "no such issue" for a live write. + updErr := te.store.UpdateIssue(ctx, "lg-live", map[string]interface{}{"title": "renamed"}, "tester") + if updErr == nil { + t.Fatal("UpdateIssue succeeded with no leases table") + } + if errors.Is(updErr, storage.ErrNotFound) { + t.Fatalf("UpdateIssue reported a present row absent: %v", updErr) + } + if !strings.Contains(updErr.Error(), "leases") { + t.Fatalf("expected the update error to name leases, got: %v", updErr) + } + }) + t.Run("wisp_label_table_error_propagates", func(t *testing.T) { te := newTestEnv(t, "wl") ctx := t.Context() diff --git a/internal/storage/issueops/get_issue.go b/internal/storage/issueops/get_issue.go index 1b51d404e..0bc3c0fb1 100644 --- a/internal/storage/issueops/get_issue.go +++ b/internal/storage/issueops/get_issue.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/internal/storage/dberrors" "github.com/steveyegge/beads/internal/storage/sqlbuild" "github.com/steveyegge/beads/internal/types" ) @@ -34,12 +35,20 @@ func GetIssueInTx(ctx context.Context, tx DBTX, id string) (*types.Issue, error) return nil, err } +// missingOptionalIssueTable reports whether err is the absence of the optional +// issue plane the hydration query just read. The hydration FROM clause also +// carries sqlbuild.LeaseJoin, so a blanket table-not-exist check here folds a +// missing leases table into "row absent" — a wrong answer, not an empty one. +func missingOptionalIssueTable(err error, issueTable string) bool { + return optionalBlockedTable(issueTable) && dberrors.IsMissingTable(err, issueTable) +} + func getIssueFromTableInTx(ctx context.Context, tx DBTX, issueTable, labelTable, id string) (*types.Issue, error) { //nolint:gosec // G201: issueTable is a hardcoded literal supplied by GetIssueInTx ("issues" or "wisps") row := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT %s FROM %s %s WHERE id = ?`, IssueSelectColumns, issueTable, sqlbuild.LeaseJoin(issueTable)), id) issue, err := ScanIssueFrom(row) - if err == sql.ErrNoRows || isTableNotExistError(err) { + if err == sql.ErrNoRows || missingOptionalIssueTable(err, issueTable) { return nil, storage.ErrNotFound } if err != nil { diff --git a/internal/storage/issueops/get_issue_test.go b/internal/storage/issueops/get_issue_test.go new file mode 100644 index 000000000..e6f9e05b3 --- /dev/null +++ b/internal/storage/issueops/get_issue_test.go @@ -0,0 +1,136 @@ +package issueops + +import ( + "context" + "database/sql" + "errors" + "regexp" + "strings" + "testing" + + "github.com/DATA-DOG/go-sqlmock" + mysql "github.com/go-sql-driver/mysql" + + "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/internal/storage/sqlbuild" +) + +// missingTableError builds the driver error a Dolt query returns when its FROM +// clause names a table that does not exist. Both the embedded driver +// (dolthub/driver/v2 translateError) and the sql-server wire protocol surface +// it as a *mysql.MySQLError carrying 1146 and go-mysql-server's +// "table not found: %s" text. +func missingTableError(table string) error { + return &mysql.MySQLError{Number: 1146, Message: "table not found: " + table} +} + +// missingTableErrorMySQLText is the same condition worded the way stock MySQL +// (and Dolt's MySQL-compatible error mode) words it. +func missingTableErrorMySQLText(schema, table string) error { + return &mysql.MySQLError{Number: 1146, Message: "Table '" + schema + "." + table + "' doesn't exist"} +} + +func expectHydrationQuery(mock sqlmock.Sqlmock, table, id string, err error) { + mock.ExpectQuery(regexp.QuoteMeta("SELECT " + IssueSelectColumns + " FROM " + table + " " + sqlbuild.LeaseJoin(table) + " WHERE id = ?")). + WithArgs(id). + WillReturnError(err) +} + +// TestGetIssueInTxMissingLeasesTableIsAnError pins the narrowed tolerance. The +// hydration FROM clause carries sqlbuild.LeaseJoin, so a database missing the +// leases table fails the query for a row that is demonstrably present. The +// table-not-exist tolerance exists for the optional wisps plane only; folding +// a missing leases table into "row absent" hands the caller a 404 it cannot +// tell apart from a deletion. +func TestGetIssueInTxMissingLeasesTableIsAnError(t *testing.T) { + tests := []struct { + name string + err error + }{ + {name: "dolt wording", err: missingTableError("leases")}, + {name: "mysql wording", err: missingTableErrorMySQLText("beads", "leases")}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + _, mock, tx := beginMockTx(t) + + // Both planes are primed: before the fix the issues query is + // swallowed and the wisps query runs too, so ExpectationsWereMet + // is deliberately not asserted here — the returned error is the + // assertion. + expectHydrationQuery(mock, "issues", "bd-1", tc.err) + expectHydrationQuery(mock, "wisps", "bd-1", tc.err) + + _, err := GetIssueInTx(context.Background(), tx, "bd-1") + if err == nil { + t.Fatal("GetIssueInTx succeeded with no leases table") + } + if errors.Is(err, storage.ErrNotFound) { + t.Fatalf("GetIssueInTx reported the row absent for a broken lease join: %v", err) + } + if !strings.Contains(err.Error(), "leases") { + t.Fatalf("error does not name the missing table: %v", err) + } + }) + } +} + +// TestGetIssueInTxMissingWispsTableIsNotFound is the control: the tolerance +// the narrowed guard must keep. A pre-migration database has no wisps table, +// and an ID on neither plane is genuinely absent. +func TestGetIssueInTxMissingWispsTableIsNotFound(t *testing.T) { + _, mock, tx := beginMockTx(t) + + expectHydrationQuery(mock, "issues", "bd-1", sql.ErrNoRows) + expectHydrationQuery(mock, "wisps", "bd-1", missingTableError("wisps")) + + _, err := GetIssueInTx(context.Background(), tx, "bd-1") + if !errors.Is(err, storage.ErrNotFound) { + t.Fatalf("GetIssueInTx = %v, want ErrNotFound for a pre-migration database", err) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatalf("unmet sql expectations: %v", err) + } +} + +// TestGetIssueInTxAbsentRowIsNotFound is the second control: an ID on neither +// plane of a healthy database still answers not-found. +func TestGetIssueInTxAbsentRowIsNotFound(t *testing.T) { + _, mock, tx := beginMockTx(t) + + expectHydrationQuery(mock, "issues", "bd-1", sql.ErrNoRows) + expectHydrationQuery(mock, "wisps", "bd-1", sql.ErrNoRows) + + _, err := GetIssueInTx(context.Background(), tx, "bd-1") + if !errors.Is(err, storage.ErrNotFound) { + t.Fatalf("GetIssueInTx = %v, want ErrNotFound", err) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatalf("unmet sql expectations: %v", err) + } +} + +// TestUpdateIssueInTxMissingLeasesTableIsAnError covers the mutation path. +// updateIssueInTx reads the pre-update row through the same hydration query, +// so the same swallowed error told a writer its live issue did not exist. +func TestUpdateIssueInTxMissingLeasesTableIsAnError(t *testing.T) { + _, mock, tx := beginMockTx(t) + + mock.ExpectQuery(regexp.QuoteMeta("SELECT 1 FROM wisps WHERE id = ? LIMIT 1")). + WithArgs("bd-1"). + WillReturnError(sql.ErrNoRows) + expectHydrationQuery(mock, "issues", "bd-1", missingTableError("leases")) + expectHydrationQuery(mock, "wisps", "bd-1", missingTableError("leases")) + + _, err := UpdateIssueInTx(context.Background(), tx, "bd-1", map[string]interface{}{"title": "new"}, "tester") + if err == nil { + t.Fatal("UpdateIssueInTx succeeded with no leases table") + } + if errors.Is(err, storage.ErrNotFound) { + t.Fatalf("UpdateIssueInTx reported the row absent for a broken lease join: %v", err) + } + if !strings.Contains(err.Error(), "leases") { + t.Fatalf("error does not name the missing table: %v", err) + } +}