feat: migrate custom statuses and types to normalized tables (#2961)

* feat: migrate custom statuses and types to normalized tables

Migrate custom statuses from comma-separated config string
(status.custom) to a normalized custom_statuses(name, category) table.
Similarly migrate types.custom to a custom_types(name) table.

Views now use JOINs against these tables instead of dynamically
building IN clauses with escapeSQL(). Config set/get commands maintain
backward compatibility by reading/writing through the tables.

Includes schema migration (v9→v10) that auto-populates tables from
existing config values.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: address PR review — rows.Err(), error propagation, CTE, dead params

Fixes all issues raised in PR #2961 review:

🔴 Critical:
- Add rows.Err() checks after all rows.Next() loops in
  ResolveCustomStatusesDetailedInTx, ResolveCustomTypesInTx,
  and readCustomStatusesFromDB (now removed as unused)
- Use types.ParseCustomStatusConfig() in migration instead of
  hand-rolled inline parsing, ensuring validation parity

🟡 Medium:
- Propagate parse errors from syncCustomStatusesTable instead of
  silently swallowing them (returns error to SetConfig caller)
- Use json.Unmarshal in migration's parseTypesValue instead of
  hand-rolled bracket/comma splitting
- Remove dead []types.CustomStatus parameter from BuildReadyIssuesView
  and BuildBlockedIssuesView (now parameterless)
- Add CTE (done_frozen) to blocked_issues view to deduplicate the
  repeated custom_statuses subquery
- Fix empty-table fallback ambiguity: successful table query with 0
  rows is now authoritative (no fallthrough to config string)
- Add // Deprecated: godoc comment to escapeSQL

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Harry Miller 2026-04-02 20:41:16 -07:00 • committed by GitHub
commit 8537d943bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 327 additions and 247 deletions

View file

@ -15,7 +15,21 @@ import (
// SetConfig sets a configuration value
func (s *DoltStore) SetConfig(ctx context.Context, key, value string) error {
if err := s.withRetryTx(ctx, func(tx *sql.Tx) error {
return issueops.SetConfigInTx(ctx, tx, key, value)
if err := issueops.SetConfigInTx(ctx, tx, key, value); err != nil {
return err
}
// Sync normalized tables when config keys change
switch key {
case "status.custom":
if err := syncCustomStatusesTable(ctx, tx, value); err != nil {
return fmt.Errorf("syncing custom_statuses table: %w", err)
}
case "types.custom":
if err := syncCustomTypesTable(ctx, tx, value); err != nil {
return fmt.Errorf("syncing custom_types table: %w", err)
}
}
return nil
}); err != nil {
return err
}
@ -46,6 +60,44 @@ func (s *DoltStore) SetConfig(ctx context.Context, key, value string) error {
return nil
}
// syncCustomStatusesTable replaces all rows in custom_statuses with parsed config value.
func syncCustomStatusesTable(ctx context.Context, tx *sql.Tx, value string) error {
if _, err := tx.ExecContext(ctx, "DELETE FROM custom_statuses"); err != nil {
return err
}
if value == "" {
return nil
}
parsed, err := types.ParseCustomStatusConfig(value)
if err != nil {
return fmt.Errorf("invalid status.custom value: %w", err)
}
for _, s := range parsed {
if _, err := tx.ExecContext(ctx, "INSERT INTO custom_statuses (name, category) VALUES (?, ?)",
s.Name, string(s.Category)); err != nil {
return err
}
}
return nil
}
// syncCustomTypesTable replaces all rows in custom_types with parsed config value.
func syncCustomTypesTable(ctx context.Context, tx *sql.Tx, value string) error {
if _, err := tx.ExecContext(ctx, "DELETE FROM custom_types"); err != nil {
return err
}
if value == "" {
return nil
}
names := issueops.ParseCommaSeparatedList(value)
for _, name := range names {
if _, err := tx.ExecContext(ctx, "INSERT INTO custom_types (name) VALUES (?)", name); err != nil {
return err
}
}
return nil
}
// GetConfig retrieves a configuration value
func (s *DoltStore) GetConfig(ctx context.Context, key string) (string, error) {
var value string

View file

@ -3,66 +3,27 @@ package dolt
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestBuildReadyIssuesView(t *testing.T) {
tests := []struct {
name string
customStatuses []types.CustomStatus
wantContains []string
wantNotContain []string
name string
wantContains []string
}{
{
name: "no custom statuses uses static view",
customStatuses: nil,
wantContains: []string{"i.status = 'open'"},
wantNotContain: []string{"'review'"},
},
{
name: "no active custom statuses uses static view",
customStatuses: []types.CustomStatus{
{Name: "review", Category: types.CategoryWIP},
},
wantContains: []string{"i.status = 'open'"},
wantNotContain: []string{"'review'"},
},
{
name: "active custom statuses added to IN clause",
customStatuses: []types.CustomStatus{
{Name: "review", Category: types.CategoryActive},
{Name: "triage", Category: types.CategoryActive},
{Name: "testing", Category: types.CategoryWIP},
},
wantContains: []string{"IN", "'open'", "'review'", "'triage'"},
wantNotContain: []string{"'testing'"},
},
{
name: "unspecified category not included",
customStatuses: []types.CustomStatus{
{Name: "legacy", Category: types.CategoryUnspecified},
{Name: "review", Category: types.CategoryActive},
},
wantContains: []string{"'review'"},
wantNotContain: []string{"'legacy'"},
name: "uses table-backed view",
wantContains: []string{"i.status = 'open'", "custom_statuses WHERE category = 'active'"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sql := BuildReadyIssuesView(tt.customStatuses)
sql := BuildReadyIssuesView()
for _, want := range tt.wantContains {
if !strings.Contains(sql, want) {
t.Errorf("expected SQL to contain %q, got:\n%s", want, sql)
}
}
for _, notWant := range tt.wantNotContain {
if strings.Contains(sql, notWant) {
t.Errorf("expected SQL to NOT contain %q, got:\n%s", notWant, sql)
}
}
// All views must be valid CREATE OR REPLACE VIEW statements
if !strings.Contains(sql, "CREATE OR REPLACE VIEW ready_issues") {
t.Errorf("expected valid CREATE VIEW statement")
}
@ -72,51 +33,23 @@ func TestBuildReadyIssuesView(t *testing.T) {
func TestBuildBlockedIssuesView(t *testing.T) {
tests := []struct {
name string
customStatuses []types.CustomStatus
wantContains []string
wantNotContain []string
name string
wantContains []string
}{
{
name: "no custom statuses uses static view",
customStatuses: nil,
wantContains: []string{"NOT IN ('closed', 'pinned')"},
wantNotContain: []string{"'archived'"},
},
{
name: "done/frozen custom statuses added to NOT IN",
customStatuses: []types.CustomStatus{
{Name: "archived", Category: types.CategoryDone},
{Name: "on-ice", Category: types.CategoryFrozen},
{Name: "review", Category: types.CategoryActive},
},
wantContains: []string{"'closed'", "'pinned'", "'archived'", "'on-ice'"},
wantNotContain: []string{"'review'"},
},
{
name: "only wip and active statuses - uses static view",
customStatuses: []types.CustomStatus{
{Name: "review", Category: types.CategoryActive},
{Name: "testing", Category: types.CategoryWIP},
},
wantContains: []string{"NOT IN ('closed', 'pinned')"},
wantNotContain: []string{"'review'", "'testing'"},
name: "uses table-backed view with CTE",
wantContains: []string{"NOT IN ('closed', 'pinned')", "done_frozen", "custom_statuses WHERE category IN ('done', 'frozen')"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sql := BuildBlockedIssuesView(tt.customStatuses)
sql := BuildBlockedIssuesView()
for _, want := range tt.wantContains {
if !strings.Contains(sql, want) {
t.Errorf("expected SQL to contain %q, got:\n%s", want, sql)
}
}
for _, notWant := range tt.wantNotContain {
if strings.Contains(sql, notWant) {
t.Errorf("expected SQL to NOT contain %q, got:\n%s", notWant, sql)
}
}
if !strings.Contains(sql, "CREATE OR REPLACE VIEW blocked_issues") {
t.Errorf("expected valid CREATE VIEW statement")
}
@ -125,6 +58,7 @@ func TestBuildBlockedIssuesView(t *testing.T) {
}
func TestEscapeSQL(t *testing.T) {
// escapeSQL is legacy but retained for backward compatibility
tests := []struct {
input string
want string
@ -145,94 +79,44 @@ func TestEscapeSQL(t *testing.T) {
}
}
func TestBuildReadyIssuesViewManyActiveStatuses(t *testing.T) {
// 12 active statuses should all appear in the IN clause
var statuses []types.CustomStatus
for i := 0; i < 12; i++ {
name := "status" + strings.Repeat("x", i+1) // statusx, statusxx, ...
statuses = append(statuses, types.CustomStatus{Name: name, Category: types.CategoryActive})
}
sql := BuildReadyIssuesView(statuses)
if !strings.Contains(sql, "IN") {
t.Fatal("expected IN clause for multiple active statuses")
}
for _, s := range statuses {
if !strings.Contains(sql, "'"+s.Name+"'") {
t.Errorf("expected SQL to contain %q", s.Name)
}
}
if !strings.Contains(sql, "'open'") {
t.Error("expected SQL to still contain 'open'")
func TestBuildReadyIssuesViewIsStatic(t *testing.T) {
// The view is now static (table-backed) — same SQL on every call
sql1 := BuildReadyIssuesView()
sql2 := BuildReadyIssuesView()
if sql1 != sql2 {
t.Error("BuildReadyIssuesView should return identical SQL on every call")
}
}
func TestBuildBlockedIssuesViewManyDoneFrozen(t *testing.T) {
var statuses []types.CustomStatus
for i := 0; i < 5; i++ {
statuses = append(statuses, types.CustomStatus{
Name: "done" + strings.Repeat("x", i+1),
Category: types.CategoryDone,
})
}
for i := 0; i < 5; i++ {
statuses = append(statuses, types.CustomStatus{
Name: "frozen" + strings.Repeat("x", i+1),
Category: types.CategoryFrozen,
})
}
sql := BuildBlockedIssuesView(statuses)
for _, s := range statuses {
if !strings.Contains(sql, "'"+s.Name+"'") {
t.Errorf("expected SQL to contain %q", s.Name)
}
}
// Built-in exclusions should still be present
if !strings.Contains(sql, "'closed'") {
t.Error("expected SQL to contain 'closed'")
}
if !strings.Contains(sql, "'pinned'") {
t.Error("expected SQL to contain 'pinned'")
func TestBuildBlockedIssuesViewIsStatic(t *testing.T) {
// The view is now static (table-backed) — same SQL on every call
sql1 := BuildBlockedIssuesView()
sql2 := BuildBlockedIssuesView()
if sql1 != sql2 {
t.Error("BuildBlockedIssuesView should return identical SQL on every call")
}
}
func TestBuildReadyIssuesViewUnspecifiedNotIncluded(t *testing.T) {
statuses := []types.CustomStatus{
{Name: "legacy", Category: types.CategoryUnspecified},
{Name: "old-flow", Category: types.CategoryUnspecified},
}
func TestViewsReferenceCustomStatusesTable(t *testing.T) {
readySQL := BuildReadyIssuesView()
blockedSQL := BuildBlockedIssuesView()
sql := BuildReadyIssuesView(statuses)
// Unspecified should NOT affect the ready view
if strings.Contains(sql, "'legacy'") {
t.Error("unspecified status 'legacy' should not appear in ready view")
if !strings.Contains(readySQL, "custom_statuses") {
t.Error("ready_issues view should reference custom_statuses table")
}
if strings.Contains(sql, "'old-flow'") {
t.Error("unspecified status 'old-flow' should not appear in ready view")
}
// Should use static view (no IN clause needed)
if !strings.Contains(sql, "i.status = 'open'") {
t.Error("expected static view with just i.status = 'open'")
if !strings.Contains(blockedSQL, "custom_statuses") {
t.Error("blocked_issues view should reference custom_statuses table")
}
}
func TestBuildBlockedIssuesViewUnspecifiedNotIncluded(t *testing.T) {
statuses := []types.CustomStatus{
{Name: "legacy", Category: types.CategoryUnspecified},
{Name: "old-flow", Category: types.CategoryUnspecified},
func TestBlockedViewUsesCTE(t *testing.T) {
sql := BuildBlockedIssuesView()
if !strings.Contains(sql, "WITH done_frozen AS") {
t.Error("blocked_issues view should use done_frozen CTE")
}
sql := BuildBlockedIssuesView(statuses)
// Unspecified should NOT affect the blocked view
if strings.Contains(sql, "'legacy'") {
t.Error("unspecified status 'legacy' should not appear in blocked view")
}
if strings.Contains(sql, "'old-flow'") {
t.Error("unspecified status 'old-flow' should not appear in blocked view")
}
// Should use static NOT IN
if !strings.Contains(sql, "NOT IN ('closed', 'pinned')") {
t.Error("expected static NOT IN clause")
// Verify the CTE is referenced, not the raw subquery repeated
count := strings.Count(sql, "SELECT name FROM custom_statuses WHERE category IN ('done', 'frozen')")
if count != 1 {
t.Errorf("expected custom_statuses subquery exactly once (in CTE), found %d times", count)
}
}

View file

@ -33,6 +33,7 @@ var migrationsList = []Migration{
{"drop_hop_columns", migrations.MigrateDropHOPColumns},
{"drop_child_counters_fk", migrations.MigrateDropChildCountersFK},
{"wisp_events_created_at_index", migrations.MigrateWispEventsCreatedAtIndex},
{"custom_status_type_tables", migrations.MigrateCustomStatusTypeTables},
}
// RunMigrations executes all registered Dolt migrations in order.
@ -53,6 +54,7 @@ func RunMigrations(db *sql.DB) error {
"wisp_dependencies", "labels", "wisp_labels", "comments",
"wisp_comments", "metadata", "child_counters", "issue_counter",
"issue_snapshots", "compaction_snapshots", "federation_peers",
"custom_statuses", "custom_types",
"dolt_ignore",
}
for _, table := range migrationTables {

View file

@ -0,0 +1,129 @@
package migrations
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/steveyegge/beads/internal/types"
)
// MigrateCustomStatusTypeTables creates normalized custom_statuses and
// custom_types tables, then populates them from existing config values.
// This migration is idempotent — safe to run multiple times.
func MigrateCustomStatusTypeTables(db *sql.DB) error {
if err := migrateCustomStatusesTable(db); err != nil {
return fmt.Errorf("custom_statuses table: %w", err)
}
if err := migrateCustomTypesTable(db); err != nil {
return fmt.Errorf("custom_types table: %w", err)
}
return nil
}
func migrateCustomStatusesTable(db *sql.DB) error {
exists, err := tableExists(db, "custom_statuses")
if err != nil {
return err
}
if exists {
return nil
}
_, err = db.Exec(`CREATE TABLE custom_statuses (
name VARCHAR(64) PRIMARY KEY,
category VARCHAR(32) NOT NULL DEFAULT 'unspecified'
)`)
if err != nil {
return fmt.Errorf("creating table: %w", err)
}
// Populate from existing config value using validated parser.
// Invalid entries are logged and skipped — the migration is intentionally lenient
// so existing databases aren't blocked. The first `bd config set status.custom`
// will normalize the data through the stricter runtime validation.
var value string
err = db.QueryRow("SELECT `value` FROM config WHERE `key` = 'status.custom'").Scan(&value)
if err != nil || value == "" {
return nil
}
parsed, parseErr := types.ParseCustomStatusConfig(value)
if parseErr != nil {
// Config has invalid entries — log and skip rather than blocking the migration.
log.Printf("migration: skipping invalid status.custom entries: %v", parseErr)
return nil
}
for _, s := range parsed {
_, err = db.Exec("INSERT IGNORE INTO custom_statuses (name, category) VALUES (?, ?)", s.Name, string(s.Category))
if err != nil {
return fmt.Errorf("inserting status %q: %w", s.Name, err)
}
}
return nil
}
func migrateCustomTypesTable(db *sql.DB) error {
exists, err := tableExists(db, "custom_types")
if err != nil {
return err
}
if exists {
return nil
}
_, err = db.Exec(`CREATE TABLE custom_types (
name VARCHAR(64) PRIMARY KEY
)`)
if err != nil {
return fmt.Errorf("creating table: %w", err)
}
// Populate from existing config value
var value string
err = db.QueryRow("SELECT `value` FROM config WHERE `key` = 'types.custom'").Scan(&value)
if err != nil || value == "" {
return nil
}
// Try JSON array first, fall back to comma-separated
names := parseTypesValue(value)
for _, name := range names {
name = strings.TrimSpace(name)
if name == "" {
continue
}
_, err = db.Exec("INSERT IGNORE INTO custom_types (name) VALUES (?)", name)
if err != nil {
return fmt.Errorf("inserting type %q: %w", name, err)
}
}
return nil
}
// parseTypesValue tries JSON array then comma-separated.
func parseTypesValue(value string) []string {
value = strings.TrimSpace(value)
if value == "" {
return nil
}
// Try JSON array first (e.g. '["gate","convoy"]')
var jsonTypes []string
if err := json.Unmarshal([]byte(value), &jsonTypes); err == nil {
return jsonTypes
}
// Fall back to comma-separated
parts := strings.Split(value, ",")
var result []string
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
result = append(result, p)
}
}
return result
}

View file

@ -2,14 +2,12 @@ package dolt
import (
"strings"
"github.com/steveyegge/beads/internal/types"
)
// currentSchemaVersion is bumped whenever the schema or migrations change.
// initSchemaOnDB checks this against the stored version and skips re-initialization
// when they match, avoiding ~20 DDL statements per bd invocation.
const currentSchemaVersion = 9
const currentSchemaVersion = 10
// schema defines the MySQL-compatible database schema for Dolt.
const schema = `
@ -242,6 +240,17 @@ CREATE TABLE IF NOT EXISTS federation_peers (
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_federation_peers_sovereignty (sovereignty)
);
-- Custom statuses table (normalized from config status.custom)
CREATE TABLE IF NOT EXISTS custom_statuses (
name VARCHAR(64) PRIMARY KEY,
category VARCHAR(32) NOT NULL DEFAULT 'unspecified'
);
-- Custom types table (normalized from config types.custom)
CREATE TABLE IF NOT EXISTS custom_types (
name VARCHAR(64) PRIMARY KEY
);
`
// defaultConfig contains the default configuration values
@ -258,14 +267,11 @@ INSERT IGNORE INTO config (` + "`key`" + `, value) VALUES
('auto_compact_enabled', 'false');
`
// readyIssuesView is a MySQL-compatible view for ready work
// readyIssuesView is a MySQL-compatible view for ready work.
// Uses a subquery against custom_statuses table for active custom statuses.
// Note: Dolt supports recursive CTEs.
// Uses LEFT JOIN instead of NOT EXISTS to avoid Dolt mergeJoinIter panic.
// See: https://github.com/dolthub/go-mysql-server/issues/3413
//
// Active status checks use NOT IN ('closed', 'pinned') rather than listing
// active statuses explicitly — this ensures custom statuses (configured via
// status.custom) are automatically included. (bd-1x0)
const readyIssuesView = `
CREATE OR REPLACE VIEW ready_issues AS
WITH RECURSIVE
@ -292,7 +298,10 @@ WITH RECURSIVE
SELECT i.*
FROM issues i
LEFT JOIN blocked_transitively bt ON bt.issue_id = i.id
WHERE i.status = 'open'
WHERE (
i.status = 'open'
OR i.status IN (SELECT name FROM custom_statuses WHERE category = 'active')
)
AND (i.ephemeral = 0 OR i.ephemeral IS NULL)
AND bt.issue_id IS NULL
AND (i.defer_until IS NULL OR i.defer_until <= UTC_TIMESTAMP())
@ -307,6 +316,7 @@ WHERE i.status = 'open'
`
// blockedIssuesView is a MySQL-compatible view for blocked issues.
// Uses subqueries against custom_statuses table for done/frozen exclusions.
// Uses subquery instead of three-table join to avoid Dolt mergeJoinIter panic.
const blockedIssuesView = `
CREATE OR REPLACE VIEW blocked_issues AS
@ -320,10 +330,12 @@ SELECT
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_id
AND blocker.status NOT IN ('closed', 'pinned')
AND blocker.status NOT IN (SELECT name FROM custom_statuses WHERE category IN ('done', 'frozen'))
)
) as blocked_by_count
FROM issues i
WHERE i.status NOT IN ('closed', 'pinned')
AND i.status NOT IN (SELECT name FROM custom_statuses WHERE category IN ('done', 'frozen'))
AND EXISTS (
SELECT 1 FROM dependencies d
WHERE d.issue_id = i.id
@ -332,24 +344,15 @@ WHERE i.status NOT IN ('closed', 'pinned')
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_id
AND blocker.status NOT IN ('closed', 'pinned')
AND blocker.status NOT IN (SELECT name FROM custom_statuses WHERE category IN ('done', 'frozen'))
)
);
`
// BuildReadyIssuesView generates the ready_issues view SQL, incorporating
// custom statuses with CategoryActive into the status filter.
// When no active custom statuses exist, falls back to the static view.
func BuildReadyIssuesView(customStatuses []types.CustomStatus) string {
active := types.CustomStatusesByCategory(customStatuses, types.CategoryActive)
if len(active) == 0 {
return readyIssuesView
}
statusList := "'open'"
for _, s := range active {
statusList += ", '" + escapeSQL(s.Name) + "'"
}
// custom statuses with CategoryActive from the custom_statuses table.
// The view uses a subquery against custom_statuses rather than dynamic IN clauses.
func BuildReadyIssuesView() string {
return `
CREATE OR REPLACE VIEW ready_issues AS
WITH RECURSIVE
@ -376,7 +379,10 @@ WITH RECURSIVE
SELECT i.*
FROM issues i
LEFT JOIN blocked_transitively bt ON bt.issue_id = i.id
WHERE i.status IN (` + statusList + `)
WHERE (
i.status = 'open'
OR i.status IN (SELECT name FROM custom_statuses WHERE category = 'active')
)
AND (i.ephemeral = 0 OR i.ephemeral IS NULL)
AND bt.issue_id IS NULL
AND (i.defer_until IS NULL OR i.defer_until <= UTC_TIMESTAMP())
@ -392,24 +398,14 @@ WHERE i.status IN (` + statusList + `)
}
// BuildBlockedIssuesView generates the blocked_issues view SQL, incorporating
// custom statuses with CategoryDone/CategoryFrozen into the exclusion filter.
func BuildBlockedIssuesView(customStatuses []types.CustomStatus) string {
done := types.CustomStatusesByCategory(customStatuses, types.CategoryDone)
frozen := types.CustomStatusesByCategory(customStatuses, types.CategoryFrozen)
if len(done) == 0 && len(frozen) == 0 {
return blockedIssuesView
}
excludeList := "'closed', 'pinned'"
for _, s := range done {
excludeList += ", '" + escapeSQL(s.Name) + "'"
}
for _, s := range frozen {
excludeList += ", '" + escapeSQL(s.Name) + "'"
}
// custom statuses with CategoryDone/CategoryFrozen from the custom_statuses table.
// The view uses a CTE against custom_statuses to deduplicate the subquery.
func BuildBlockedIssuesView() string {
return `
CREATE OR REPLACE VIEW blocked_issues AS
WITH done_frozen AS (
SELECT name FROM custom_statuses WHERE category IN ('done', 'frozen')
)
SELECT
i.*,
(SELECT COUNT(*)
@ -419,11 +415,13 @@ SELECT
AND EXISTS (
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_id
AND blocker.status NOT IN (` + excludeList + `)
AND blocker.status NOT IN ('closed', 'pinned')
AND blocker.status NOT IN (SELECT name FROM done_frozen)
)
) as blocked_by_count
FROM issues i
WHERE i.status NOT IN (` + excludeList + `)
WHERE i.status NOT IN ('closed', 'pinned')
AND i.status NOT IN (SELECT name FROM done_frozen)
AND EXISTS (
SELECT 1 FROM dependencies d
WHERE d.issue_id = i.id
@ -431,15 +429,16 @@ WHERE i.status NOT IN (` + excludeList + `)
AND EXISTS (
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_id
AND blocker.status NOT IN (` + excludeList + `)
AND blocker.status NOT IN ('closed', 'pinned')
AND blocker.status NOT IN (SELECT name FROM done_frozen)
)
);
`
}
// escapeSQL escapes a string for safe inclusion in SQL string literals.
// Defense-in-depth: status names are already validated by ParseCustomStatusConfig
// to match [a-z][a-z0-9_-]*, so this should never actually escape anything.
// Deprecated: View building now uses JOINs against the custom_statuses table
// instead of string interpolation. Retained for backward compatibility and testing.
func escapeSQL(s string) string {
return strings.ReplaceAll(s, "'", "''")
}

View file

@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"hash/fnv"
"log"
"net"
"os"
"os/exec"
@ -1249,11 +1248,10 @@ func initSchemaOnDB(ctx context.Context, db *sql.DB) error {
}
// Rebuild status views to match current custom status config.
// This ensures views stay in sync even after direct SQL config edits.
customStatuses := readCustomStatusesFromDB(ctx, db)
if _, err := db.ExecContext(ctx, BuildReadyIssuesView(customStatuses)); err != nil {
if _, err := db.ExecContext(ctx, BuildReadyIssuesView()); err != nil {
return fmt.Errorf("failed to create ready_issues view: %w", err)
}
if _, err := db.ExecContext(ctx, BuildBlockedIssuesView(customStatuses)); err != nil {
if _, err := db.ExecContext(ctx, BuildBlockedIssuesView()); err != nil {
return fmt.Errorf("failed to create blocked_issues view: %w", err)
}
return nil
@ -1317,13 +1315,11 @@ func initSchemaOnDB(ctx context.Context, db *sql.DB) error {
return fmt.Errorf("failed to drop fk_dep_depends_on: %w", err)
}
// Create views — dynamically built to incorporate custom status categories.
// Read status.custom config directly from DB (DoltStore not yet constructed).
customStatuses := readCustomStatusesFromDB(ctx, db)
if _, err := db.ExecContext(ctx, BuildReadyIssuesView(customStatuses)); err != nil {
// Create views — table-backed, no dynamic IN clauses needed.
if _, err := db.ExecContext(ctx, BuildReadyIssuesView()); err != nil {
return fmt.Errorf("failed to create ready_issues view: %w", err)
}
if _, err := db.ExecContext(ctx, BuildBlockedIssuesView(customStatuses)); err != nil {
if _, err := db.ExecContext(ctx, BuildBlockedIssuesView()); err != nil {
return fmt.Errorf("failed to create blocked_issues view: %w", err)
}
@ -2219,43 +2215,13 @@ type DoltStatus = storage.Status
// StatusEntry is an alias for storage.StatusEntry.
type StatusEntry = storage.StatusEntry
// readCustomStatusesFromDB reads status.custom config directly from the database.
// Used during initialization when DoltStore is not yet available.
// Returns nil on any error (degraded mode — views use built-in statuses only).
func readCustomStatusesFromDB(ctx context.Context, db *sql.DB) []types.CustomStatus {
var value string
err := db.QueryRowContext(ctx, "SELECT `value` FROM config WHERE `key` = 'status.custom'").Scan(&value)
if err != nil || value == "" {
return nil
}
parsed, parseErr := types.ParseCustomStatusConfig(value)
if parseErr != nil {
// Degraded mode: log warning, return nil so views use built-in statuses
log.Printf("warning: invalid status.custom config: %v. Using built-in statuses only.", parseErr)
return nil
}
return parsed
}
// RebuildStatusViews regenerates the ready_issues and blocked_issues views
// based on current custom status configuration. Called within a write
// transaction when status.custom config changes.
// RebuildStatusViews regenerates the ready_issues and blocked_issues views.
// Views are now table-backed (static SQL), so no custom status parameter needed.
func (s *DoltStore) RebuildStatusViews(ctx context.Context) error {
detailed, err := s.GetCustomStatusesDetailed(ctx)
if err != nil {
// On error, rebuild with built-in statuses only
detailed = nil
}
return s.rebuildStatusViewsWithStatuses(ctx, detailed)
}
func (s *DoltStore) rebuildStatusViewsWithStatuses(ctx context.Context, customStatuses []types.CustomStatus) error {
readySQL := BuildReadyIssuesView(customStatuses)
if _, err := s.db.ExecContext(ctx, readySQL); err != nil {
if _, err := s.db.ExecContext(ctx, BuildReadyIssuesView()); err != nil {
return fmt.Errorf("failed to rebuild ready_issues view: %w", err)
}
blockedSQL := BuildBlockedIssuesView(customStatuses)
if _, err := s.db.ExecContext(ctx, blockedSQL); err != nil {
if _, err := s.db.ExecContext(ctx, BuildBlockedIssuesView()); err != nil {
return fmt.Errorf("failed to rebuild blocked_issues view: %w", err)
}
return nil

View file

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"github.com/steveyegge/beads/internal/config"
@ -46,11 +47,36 @@ func ParseCommaSeparatedList(value string) []string {
return result
}
// ResolveCustomStatusesDetailedInTx reads custom statuses from the database,
// falling back to config.yaml if DB config is unavailable or empty.
// ResolveCustomStatusesDetailedInTx reads custom statuses from the custom_statuses
// table, falling back to the config string and then config.yaml if the table
// doesn't exist (pre-migration databases).
// Returns nil on parse errors (degraded mode). Does not cache or log —
// callers layer those concerns on top.
func ResolveCustomStatusesDetailedInTx(ctx context.Context, tx *sql.Tx) ([]types.CustomStatus, error) {
// Try the normalized table first
rows, err := tx.QueryContext(ctx, "SELECT name, category FROM custom_statuses ORDER BY name")
if err == nil {
defer rows.Close()
var result []types.CustomStatus
for rows.Next() {
var name, category string
if err := rows.Scan(&name, &category); err != nil {
continue
}
result = append(result, types.CustomStatus{
Name: name,
Category: types.StatusCategory(category),
})
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("reading custom_statuses: %w", err)
}
// Table query succeeded — return result even if empty.
// Only fall through to config string when the table doesn't exist (query error above).
return result, nil
}
// Fallback: table doesn't exist (pre-migration) — read from config string
value, err := GetConfigInTx(ctx, tx, "status.custom")
if err != nil {
if yamlStatuses := config.GetCustomStatusesFromYAML(); len(yamlStatuses) > 0 {
@ -73,10 +99,32 @@ func ResolveCustomStatusesDetailedInTx(ctx context.Context, tx *sql.Tx) ([]types
return nil, nil
}
// ResolveCustomTypesInTx reads custom issue types from the database,
// falling back to config.yaml if DB config is unavailable or empty.
// ResolveCustomTypesInTx reads custom issue types from the custom_types table,
// falling back to config string and then config.yaml if the table doesn't exist
// (pre-migration databases).
// Does not cache — callers layer caching on top.
func ResolveCustomTypesInTx(ctx context.Context, tx *sql.Tx) ([]string, error) {
// Try the normalized table first
rows, err := tx.QueryContext(ctx, "SELECT name FROM custom_types ORDER BY name")
if err == nil {
defer rows.Close()
var result []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
continue
}
result = append(result, name)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("reading custom_types: %w", err)
}
// Table query succeeded — return result even if empty.
// Only fall through to config string when the table doesn't exist (query error above).
return result, nil
}
// Fallback: table doesn't exist (pre-migration) — read from config string
value, err := GetConfigInTx(ctx, tx, "types.custom")
if err != nil {
if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {