/{cmd,internal}: remove depends_on_id

This commit is contained in:
coffeegoddd☕️✨ 2026-05-19 09:41:08 -07:00
commit d15c7572f4
39 changed files with 559 additions and 189 deletions

View file

@ -115,7 +115,7 @@ func assertDepExists(t *testing.T, beadsDir, database, issueID, dependsOnID stri
defer cleanup()
var count int
err = db.QueryRowContext(t.Context(),
"SELECT COUNT(*) FROM dependencies WHERE issue_id = ? AND depends_on_id = ?",
"SELECT COUNT(*) FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?",
issueID, dependsOnID).Scan(&count)
if err != nil {
t.Fatalf("query dependencies: %v", err)
@ -136,7 +136,7 @@ func assertDepExistsWithType(t *testing.T, beadsDir, database, issueID, dependsO
var depType string
err = db.QueryRowContext(t.Context(),
"SELECT type FROM dependencies WHERE issue_id = ? AND depends_on_id = ?",
"SELECT type FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?",
issueID, dependsOnID).Scan(&depType)
if err != nil {
t.Fatalf("query dependencies for %s -> %s: %v", issueID, dependsOnID, err)

View file

@ -128,12 +128,12 @@ func checkParentConsistency(db *sql.DB) DoctorCheck {
// Find parent-child deps where either side doesn't exist
query := `
SELECT d.issue_id, d.depends_on_id
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id
FROM dependencies d
WHERE d.type = 'parent-child'
AND (
NOT EXISTS (SELECT 1 FROM issues WHERE id = d.issue_id)
OR NOT EXISTS (SELECT 1 FROM issues WHERE id = d.depends_on_id)
OR NOT EXISTS (SELECT 1 FROM issues WHERE id = COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external))
)
LIMIT 10`
@ -182,11 +182,11 @@ func checkDependencyIntegrity(db *sql.DB) DoctorCheck {
// Find any deps where either side is missing
query := `
SELECT d.issue_id, d.depends_on_id, d.type
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id, d.type
FROM dependencies d
WHERE (
NOT EXISTS (SELECT 1 FROM issues WHERE id = d.issue_id)
OR NOT EXISTS (SELECT 1 FROM issues WHERE id = d.depends_on_id)
OR NOT EXISTS (SELECT 1 FROM issues WHERE id = COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external))
)
LIMIT 10`
@ -239,7 +239,7 @@ func checkEpicCompleteness(db *sql.DB) DoctorCheck {
COUNT(c.id) as total_children,
SUM(CASE WHEN c.status = 'closed' THEN 1 ELSE 0 END) as closed_children
FROM issues e
JOIN dependencies d ON d.depends_on_id = e.id AND d.type = 'parent-child'
JOIN dependencies d ON d.depends_on_issue_id = e.id AND d.type = 'parent-child'
JOIN issues c ON c.id = d.issue_id
WHERE e.issue_type = 'epic'
AND e.status != 'closed'
@ -425,12 +425,12 @@ func checkMoleculeIntegrity(db *sql.DB) DoctorCheck {
// nolint:gosec // G201: placeholders contains only ? markers, actual values passed via args
brokenQuery := fmt.Sprintf(`
SELECT d.depends_on_id, COUNT(*) AS orphan_count
SELECT d.depends_on_issue_id, COUNT(*) AS orphan_count
FROM dependencies d
WHERE d.depends_on_id IN (%s)
WHERE d.depends_on_issue_id IN (%s)
AND d.type = 'parent-child'
AND NOT EXISTS (SELECT 1 FROM issues WHERE id = d.issue_id)
GROUP BY d.depends_on_id`, strings.Join(placeholders, ","))
GROUP BY d.depends_on_issue_id`, strings.Join(placeholders, ","))
brokenRows, err := db.Query(brokenQuery, molIDs...)
if err == nil {

View file

@ -62,14 +62,22 @@ func TestCheckParentConsistency_OrphanedDeps(t *testing.T) {
t.Fatal(err)
}
// Insert a parent-child dep pointing to non-existent parent via raw SQL
// Insert a parent-child dep pointing to non-existent parent via raw SQL.
// FK on depends_on_issue_id would normally block this; disable checks to
// simulate the schema-drift scenario the validator is designed to catch.
db := store.UnderlyingDB()
if _, err := db.ExecContext(ctx, "SET FOREIGN_KEY_CHECKS = 0"); err != nil {
t.Fatal(err)
}
_, err := db.ExecContext(ctx,
"INSERT INTO dependencies (issue_id, depends_on_id, type, created_at, created_by) VALUES (?, ?, ?, NOW(), ?)",
"INSERT INTO dependencies (issue_id, depends_on_issue_id, type, created_at, created_by) VALUES (?, ?, ?, NOW(), ?)",
"bd-1", "bd-missing", "parent-child", "test")
if err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "SET FOREIGN_KEY_CHECKS = 1"); err != nil {
t.Fatal(err)
}
check := checkParentConsistency(db)
@ -160,7 +168,7 @@ func TestCheckMailThreadIntegrity_ValidThreads(t *testing.T) {
// Insert a dependency with valid thread_id via raw SQL (replies-to with thread_id)
db := store.UnderlyingDB()
_, err := db.ExecContext(ctx,
"INSERT INTO dependencies (issue_id, depends_on_id, type, thread_id, created_at, created_by) VALUES (?, ?, ?, ?, NOW(), ?)",
"INSERT INTO dependencies (issue_id, depends_on_issue_id, type, thread_id, created_at, created_by) VALUES (?, ?, ?, ?, NOW(), ?)",
"thread-reply", "thread-root", "replies-to", "thread-root", "test")
if err != nil {
t.Fatalf("Failed to insert thread dep: %v", err)

View file

@ -37,11 +37,11 @@ func OrphanedDependencies(path string, verbose bool) error {
// Find orphaned dependencies (exclude external: cross-rig tracking refs, #1593)
query := `
SELECT d.issue_id, d.depends_on_id
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id
FROM dependencies d
LEFT JOIN issues i ON d.depends_on_id = i.id
LEFT JOIN issues i ON i.id = COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external)
WHERE i.id IS NULL
AND d.depends_on_id NOT LIKE 'external:%'
AND d.depends_on_external IS NULL
`
rows, err := db.Query(query)
if err != nil {
@ -80,7 +80,7 @@ func OrphanedDependencies(path string, verbose bool) error {
}
var removed int
for _, o := range orphans {
_, err := tx.Exec("DELETE FROM dependencies WHERE issue_id = ? AND depends_on_id = ?",
_, err := tx.Exec("DELETE FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?",
o.issueID, o.dependsOnID)
if err != nil {
fmt.Printf(" Warning: failed to remove %s→%s: %v\n", o.issueID, o.dependsOnID, err)
@ -119,13 +119,13 @@ func ChildParentDependencies(path string, verbose bool) error {
}
defer db.Close()
// Find child→parent BLOCKING dependencies where issue_id starts with depends_on_id + "."
// Find child→parent BLOCKING dependencies where issue_id starts with target id + "."
// Only matches blocking types (blocks, conditional-blocks, waits-for) that cause deadlock.
// Excludes 'parent-child' type which is a legitimate structural hierarchy relationship.
query := `
SELECT d.issue_id, d.depends_on_id, d.type
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id, d.type
FROM dependencies d
WHERE d.issue_id LIKE CONCAT(d.depends_on_id, '.%')
WHERE d.issue_id LIKE CONCAT(COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external), '.%')
AND d.type IN ('blocks', 'conditional-blocks', 'waits-for')
`
rows, err := db.Query(query)
@ -166,7 +166,7 @@ func ChildParentDependencies(path string, verbose bool) error {
}
var removed int
for _, d := range badDeps {
_, err := tx.Exec("DELETE FROM dependencies WHERE issue_id = ? AND depends_on_id = ? AND type = ?",
_, err := tx.Exec("DELETE FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ? AND type = ?",
d.issueID, d.dependsOnID, d.depType)
if err != nil {
fmt.Printf(" Warning: failed to remove %s→%s: %v\n", d.issueID, d.dependsOnID, err)

View file

@ -163,9 +163,9 @@ func checkDependencyCyclesWithStore(store *dolt.DoltStore) DoctorCheck {
WITH RECURSIVE paths AS (
SELECT
issue_id,
depends_on_id,
COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) AS depends_on_id,
issue_id as start_id,
CONCAT(issue_id, '→', depends_on_id) as path,
CONCAT(issue_id, '→', COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external)) as path,
0 as depth
FROM dependencies
@ -173,14 +173,14 @@ func checkDependencyCyclesWithStore(store *dolt.DoltStore) DoctorCheck {
SELECT
d.issue_id,
d.depends_on_id,
COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id,
p.start_id,
CONCAT(p.path, '→', d.depends_on_id),
CONCAT(p.path, '→', COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external)),
p.depth + 1
FROM dependencies d
JOIN paths p ON d.issue_id = p.depends_on_id
WHERE p.depth < 100
AND p.path NOT LIKE CONCAT('%', d.depends_on_id, '→%')
AND p.path NOT LIKE CONCAT('%', COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external), '→%')
)
SELECT DISTINCT start_id
FROM paths

View file

@ -181,7 +181,7 @@ func runDoltDiagnosticQueries(ctx context.Context, db *sql.DB, metrics *DoltPerf
WHERE status IN ('open', 'in_progress')
AND id NOT IN (
SELECT issue_id FROM dependencies
WHERE depends_on_id IN (SELECT id FROM issues WHERE status != 'closed')
WHERE depends_on_issue_id IN (SELECT id FROM issues WHERE status != 'closed')
)
LIMIT 100
`)

View file

@ -58,11 +58,11 @@ func checkOrphanedDependenciesDB(db *sql.DB) DoctorCheck {
// injected by the JSONL exporter and intentionally reference issues not
// present in the local database (#1593).
query := `
SELECT d.issue_id, d.depends_on_id, d.type
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id, d.type
FROM dependencies d
LEFT JOIN issues i ON d.depends_on_id = i.id
LEFT JOIN issues i ON i.id = COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external)
WHERE i.id IS NULL
AND d.depends_on_id NOT LIKE 'external:%'
AND d.depends_on_external IS NULL
`
rows, err := db.Query(query)
if err != nil {
@ -416,13 +416,13 @@ func checkDoltConflicts(beadsDir string) DoctorCheck {
// checkChildParentDependenciesDB is the core logic for CheckChildParentDependencies.
func checkChildParentDependenciesDB(db *sql.DB) DoctorCheck {
// Query for child→parent BLOCKING dependencies where issue_id starts with depends_on_id + "."
// Query for child→parent BLOCKING dependencies where issue_id starts with target id + "."
// Only matches blocking types (blocks, conditional-blocks, waits-for) that cause deadlock.
// Excludes 'parent-child' type which is a legitimate structural hierarchy relationship.
query := `
SELECT d.issue_id, d.depends_on_id
SELECT d.issue_id, COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) AS depends_on_id
FROM dependencies d
WHERE d.issue_id LIKE CONCAT(d.depends_on_id, '.%')
WHERE d.issue_id LIKE CONCAT(COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external), '.%')
AND d.type IN ('blocks', 'conditional-blocks', 'waits-for')
`
rows, err := db.Query(query)

View file

@ -454,7 +454,7 @@ func TestCheckChildParentDependenciesDB_BlockingDetected(t *testing.T) {
// Add blocking dependency: child depends on parent
_, err = db.ExecContext(ctx,
`INSERT INTO dependencies (issue_id, depends_on_id, type, created_at, created_by) VALUES (?, ?, 'blocks', NOW(), 'test')`,
`INSERT INTO dependencies (issue_id, depends_on_issue_id, type, created_at, created_by) VALUES (?, ?, 'blocks', NOW(), 'test')`,
childID, parent.ID)
if err != nil {
t.Fatalf("Failed to insert dependency: %v", err)
@ -494,7 +494,7 @@ func TestCheckChildParentDependenciesDB_NonBlockingIgnored(t *testing.T) {
// Add parent-child type dependency (NOT blocking — should be ignored)
_, err = db.ExecContext(ctx,
`INSERT INTO dependencies (issue_id, depends_on_id, type, created_at, created_by) VALUES (?, ?, 'parent-child', NOW(), 'test')`,
`INSERT INTO dependencies (issue_id, depends_on_issue_id, type, created_at, created_by) VALUES (?, ?, 'parent-child', NOW(), 'test')`,
childID, parent.ID)
if err != nil {
t.Fatalf("Failed to insert dependency: %v", err)

View file

@ -112,7 +112,12 @@ func TestValidateCheck_DetectsOrphanedDeps(t *testing.T) {
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
_, err = tx.Exec("INSERT INTO dependencies (issue_id, depends_on_id, type, created_by) VALUES (?, ?, ?, ?)",
// FK on depends_on_issue_id would normally block this; simulate the
// schema-drift scenario the validator is designed to catch.
if _, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
t.Fatalf("Failed to disable FK checks: %v", err)
}
_, err = tx.Exec("INSERT INTO dependencies (issue_id, depends_on_issue_id, type, created_by) VALUES (?, ?, ?, ?)",
issue.ID, "test-nonexistent", "blocks", "test")
if err != nil {
t.Fatalf("Failed to insert orphaned dep: %v", err)
@ -231,7 +236,12 @@ func TestValidateCheck_FixOrphanedDeps(t *testing.T) {
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
_, err = tx.Exec("INSERT INTO dependencies (issue_id, depends_on_id, type, created_by) VALUES (?, ?, ?, ?)",
// FK on depends_on_issue_id would normally block this; simulate the
// schema-drift scenario the validator/fixer is designed to catch.
if _, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
t.Fatalf("Failed to disable FK checks: %v", err)
}
_, err = tx.Exec("INSERT INTO dependencies (issue_id, depends_on_issue_id, type, created_by) VALUES (?, ?, ?, ?)",
issue.ID, "test-nonexistent", "blocks", "test")
if err != nil {
t.Fatalf("Failed to insert orphaned dep: %v", err)

View file

@ -519,7 +519,7 @@ func bondMolMol(ctx context.Context, s storage.DoltStorage, molA, molB *types.Is
// Sequential: use blocks (B runs after A completes)
// Conditional: use conditional-blocks (B runs only if A fails)
// Parallel: use parent-child (organizational, no blocking)
// Note: Schema only allows one dependency per (issue_id, depends_on_id) pair
// Note: Schema only allows one dependency per (issue_id, target) pair (target = typed column)
var depType types.DependencyType
switch bondType {
case types.BondTypeSequential:

View file

@ -125,11 +125,11 @@ func (s *DoltStore) GetDependenciesWithMetadata(ctx context.Context, issueID str
return s.getWispDependenciesWithMetadata(ctx, issueID)
}
rows, err := s.queryContext(ctx, `
SELECT d.depends_on_id, d.type, d.created_at, d.created_by, d.metadata, d.thread_id
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT %s AS depends_on_id, d.type, d.created_at, d.created_by, d.metadata, d.thread_id
FROM dependencies d
WHERE d.issue_id = ?
`, issueID)
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get dependencies with metadata: %w", err)
}
@ -207,11 +207,11 @@ func (s *DoltStore) GetDependencyRecords(ctx context.Context, issueID string) ([
return s.getWispDependencyRecords(ctx, issueID)
}
rows, err := s.queryContext(ctx, `
SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM dependencies
WHERE issue_id = ?
`, issueID)
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get dependency records: %w", err)
}
@ -321,9 +321,9 @@ func (s *DoltStore) IsBlocked(ctx context.Context, issueID string) (bool, []stri
// computeBlockedIDs which considers blocks, waits-for, and
// conditional-blocks (GH-1524).
rows, err := s.queryContext(ctx, `
SELECT d.depends_on_id, d.type
SELECT d.depends_on_issue_id, d.type
FROM dependencies d
JOIN issues i ON d.depends_on_id = i.id
JOIN issues i ON d.depends_on_issue_id = i.id
WHERE d.issue_id = ?
AND d.type IN ('blocks', 'waits-for', 'conditional-blocks')
AND i.status NOT IN ('closed', 'pinned')
@ -364,7 +364,7 @@ func (s *DoltStore) GetNewlyUnblockedByClose(ctx context.Context, closedIssueID
SELECT d.issue_id
FROM dependencies d
JOIN issues i ON d.issue_id = i.id
WHERE d.depends_on_id = ?
WHERE d.depends_on_issue_id = ?
AND d.type = 'blocks'
AND i.status NOT IN ('closed', 'pinned')
`, closedIssueID)
@ -407,10 +407,10 @@ func (s *DoltStore) GetNewlyUnblockedByClose(ctx context.Context, closedIssueID
stillBlockedQuery := fmt.Sprintf(`
SELECT DISTINCT d2.issue_id
FROM dependencies d2
JOIN issues blocker ON d2.depends_on_id = blocker.id
JOIN issues blocker ON d2.depends_on_issue_id = blocker.id
WHERE d2.issue_id IN (%s)
AND d2.type = 'blocks'
AND d2.depends_on_id != ?
AND d2.depends_on_issue_id != ?
AND blocker.status NOT IN ('closed', 'pinned')
`, placeholders)

View file

@ -10,6 +10,7 @@ import (
"time"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
@ -399,11 +400,11 @@ func applyUpdatesToIssueStruct(issue *types.Issue, updates map[string]interface{
// getAllWispDependencyRecords returns all wisp dependency records, keyed by issue_id.
// Used by DetectCycles to include wisp dependencies in cross-table cycle detection. (bd-xe27)
func (s *DoltStore) getAllWispDependencyRecords(ctx context.Context) (map[string][]*types.Dependency, error) {
rows, err := s.queryContext(ctx, `
SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM wisp_dependencies
ORDER BY issue_id
`)
`, issueops.DepTargetExpr))
if err != nil {
return nil, fmt.Errorf("failed to get all wisp dependency records: %w", err)
}
@ -422,11 +423,11 @@ func (s *DoltStore) getAllWispDependencyRecords(ctx context.Context) (map[string
// getWispDependencyRecords returns raw dependency records for a wisp from wisp_dependencies.
func (s *DoltStore) getWispDependencyRecords(ctx context.Context, issueID string) ([]*types.Dependency, error) {
rows, err := s.queryContext(ctx, `
SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM wisp_dependencies
WHERE issue_id = ?
`, issueID)
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get wisp dependency records: %w", err)
}

View file

@ -528,7 +528,7 @@ func TestGitRemoteRoundTripAllTables(t *testing.T) {
}
// Verify dependency
depRows := queryCSV(t, cloneDir, "SELECT depends_on_id FROM dependencies WHERE issue_id = 'rt-child'")
depRows := queryCSV(t, cloneDir, "SELECT COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) AS depends_on_id FROM dependencies WHERE issue_id = 'rt-child'")
if len(depRows) != 1 {
t.Errorf("clone: expected 1 dependency, got %d", len(depRows))
} else if depRows[0]["depends_on_id"] != "rt-parent" {
@ -537,7 +537,7 @@ func TestGitRemoteRoundTripAllTables(t *testing.T) {
// Verify blocked status (rt-child depends on open rt-parent)
blockerCount := queryCount(t, cloneDir,
`SELECT COUNT(*) FROM dependencies d JOIN issues i ON d.depends_on_id = i.id `+
`SELECT COUNT(*) FROM dependencies d JOIN issues i ON d.depends_on_issue_id = i.id `+
`WHERE d.issue_id = 'rt-child' AND i.status IN ('open', 'in_progress')`)
if blockerCount != 1 {
t.Errorf("clone: expected rt-child to be blocked by 1 issue, got %d", blockerCount)

View file

@ -200,9 +200,11 @@ func (s *DoltStore) GetMoleculeProgress(ctx context.Context, moleculeID string)
// Route to correct table based on whether molecule is a wisp (bd-w2w)
issueTable := "issues"
depTable := "dependencies"
parentCol := "depends_on_issue_id"
if s.isActiveWisp(ctx, moleculeID) {
issueTable = "wisps"
depTable = "wisp_dependencies"
parentCol = "depends_on_wisp_id"
}
// Get molecule title
@ -217,11 +219,11 @@ func (s *DoltStore) GetMoleculeProgress(ctx context.Context, moleculeID string)
// (join_iters.go:192) which triggers on JOIN between issues and dependencies.
// Step 1: Get child issue IDs from dependencies table (single-table scan)
//nolint:gosec // G201: depTable is hardcoded to "dependencies" or "wisp_dependencies"
//nolint:gosec // G201: depTable and parentCol are hardcoded
depRows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE depends_on_id = ? AND type = 'parent-child'
`, depTable), moleculeID)
WHERE %s = ? AND type = 'parent-child'
`, depTable, parentCol), moleculeID)
if err != nil {
return nil, fmt.Errorf("failed to get molecule children: %w", err)
}

View file

@ -60,15 +60,15 @@ func TestUpdateIssueIDUpdatesWispDependencyTargets(t *testing.T) {
t.Fatalf("UpdateIssueID failed: %v", err)
}
// Verify wisp_dependencies.depends_on_id was updated
// Verify wisp_dependencies typed target columns were updated
var depCount int
err := store.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM wisp_dependencies WHERE depends_on_id = ?`, newID).Scan(&depCount)
err = store.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM wisp_dependencies WHERE COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?`, newID).Scan(&depCount)
if err != nil {
t.Fatalf("failed to query wisp_dependencies depends_on_id: %v", err)
t.Fatalf("failed to query wisp_dependencies target: %v", err)
}
if depCount != 1 {
t.Errorf("expected 1 wisp_dependencies row with depends_on_id=%q, got %d", newID, depCount)
t.Errorf("expected 1 wisp_dependencies row targeting %q, got %d", newID, depCount)
}
// Verify wisp_dependencies.issue_id still points at the source wisp.
@ -84,7 +84,8 @@ func TestUpdateIssueIDUpdatesWispDependencyTargets(t *testing.T) {
// Verify old ID is gone from wisp_dependencies
var oldCount int
err = store.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM wisp_dependencies WHERE depends_on_id = ?`, "test-old1").Scan(&oldCount)
`SELECT COUNT(*) FROM wisp_dependencies WHERE issue_id = ? OR COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?`,
"test-old1", "test-old1").Scan(&oldCount)
if err != nil {
t.Fatalf("failed to query old wisp_dependencies: %v", err)
}

View file

@ -497,7 +497,7 @@ func (t *doltTransaction) SearchIssues(ctx context.Context, query string, filter
if filter.ParentID != nil {
parentID := *filter.ParentID
//nolint:gosec // G201: depTable is hardcoded to "dependencies" or "wisp_dependencies"
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM %s WHERE type = 'parent-child' AND depends_on_id = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child')))", depTable, depTable))
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM %s WHERE type = 'parent-child' AND %s = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child')))", depTable, issueops.DepTargetExpr, depTable))
args = append(args, parentID, parentID)
}
@ -736,10 +736,10 @@ func (t *doltTransaction) GetDependencyRecords(ctx context.Context, issueID stri
//nolint:gosec // G201: table is hardcoded
rows, err := t.txFor(table).QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s
WHERE issue_id = ?
`, table), issueID)
`, issueops.DepTargetExpr, table), issueID)
if err != nil {
return nil, wrapQueryError("get dependency records in tx", err)
}
@ -773,8 +773,8 @@ func (t *doltTransaction) RemoveDependency(ctx context.Context, issueID, depends
//nolint:gosec // G201: table is hardcoded
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf(`
DELETE FROM %s WHERE issue_id = ? AND depends_on_id = ?
`, table), issueID, dependsOnID)
DELETE FROM %s WHERE issue_id = ? AND %s = ?
`, table, issueops.DepTargetExpr), issueID, dependsOnID)
if err == nil {
t.dirty.MarkDirty(table)
}

View file

@ -254,7 +254,7 @@ func countWispDependencyRows(t *testing.T, ctx context.Context, db *sql.DB, ids
inClause, args := doltBuildSQLInClause(ids)
//nolint:gosec // G201: inClause contains only ? markers
query := fmt.Sprintf(
"SELECT COUNT(*) FROM wisp_dependencies WHERE issue_id IN (%s) OR depends_on_id IN (%s)",
"SELECT COUNT(*) FROM wisp_dependencies WHERE issue_id IN (%s) OR COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) IN (%s)",
inClause, inClause,
)
var count int

View file

@ -516,17 +516,24 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
}
}
// Classify the target so the pair lookup and INSERT both hit the same
// typed column.
kind := issueops.ClassifyDepTarget(ctx, tx, dep, isCrossPrefix)
targetCol := kind.Column()
// Check for existing dependency to prevent silent type overwrites.
var existingType string
err = tx.QueryRowContext(ctx, `
SELECT type FROM wisp_dependencies WHERE issue_id = ? AND depends_on_id = ?
`, dep.IssueID, dep.DependsOnID).Scan(&existingType)
//nolint:gosec // G201: targetCol from DepTargetKind.Column()
err = tx.QueryRowContext(ctx, fmt.Sprintf(`
SELECT type FROM wisp_dependencies WHERE issue_id = ? AND %s = ?
`, targetCol), dep.IssueID, dep.DependsOnID).Scan(&existingType)
if err == nil {
if existingType == string(dep.Type) {
// Same type — idempotent; update metadata in case it changed
if _, err := tx.ExecContext(ctx, `
UPDATE wisp_dependencies SET metadata = ? WHERE issue_id = ? AND depends_on_id = ?
`, metadata, dep.IssueID, dep.DependsOnID); err != nil {
//nolint:gosec // G201: targetCol from DepTargetKind.Column()
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
UPDATE wisp_dependencies SET metadata = ? WHERE issue_id = ? AND %s = ?
`, targetCol), metadata, dep.IssueID, dep.DependsOnID); err != nil {
return fmt.Errorf("failed to update wisp dependency metadata: %w", err)
}
return wrapTransactionError("commit add wisp dependency", tx.Commit())
@ -537,12 +544,11 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
return fmt.Errorf("failed to check existing wisp dependency: %w", err)
}
kind := issueops.ClassifyDepTarget(ctx, tx, dep, isCrossPrefix)
//nolint:gosec // G201: typed column name from DepTargetKind.Column()
//nolint:gosec // G201: targetCol from DepTargetKind.Column()
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
INSERT INTO wisp_dependencies (issue_id, %s, type, created_at, created_by, metadata, thread_id)
VALUES (?, ?, ?, NOW(), ?, ?, ?)
`, kind.Column()), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
`, targetCol), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
return fmt.Errorf("failed to add wisp dependency: %w", err)
}
@ -557,17 +563,17 @@ func wispCycleReachabilityQuery(depTables []string) string {
WITH RECURSIVE reachable(node) AS (
SELECT ?
UNION
SELECT d.depends_on_id
SELECT %s
FROM reachable r
JOIN %s d ON d.issue_id = r.node AND d.type = 'blocks'
)
SELECT COUNT(*) FROM reachable WHERE node = ?
`, depTables[0])
`, issueops.DepTargetExpr, depTables[0])
}
var unions []string
for _, t := range depTables {
unions = append(unions, fmt.Sprintf("SELECT issue_id, depends_on_id FROM %s WHERE type = 'blocks'", t))
unions = append(unions, fmt.Sprintf("SELECT issue_id, %s AS depends_on_id FROM %s WHERE type = 'blocks'", issueops.DepTargetExpr, t))
}
unionQuery := strings.Join(unions, " UNION ")
return fmt.Sprintf(`
@ -588,9 +594,9 @@ func wispCycleDetectionTables() []string {
// getWispDependencies retrieves issues that a wisp depends on.
func (s *DoltStore) getWispDependencies(ctx context.Context, issueID string) ([]*types.Issue, error) {
rows, err := s.queryContext(ctx, `
SELECT depends_on_id FROM wisp_dependencies WHERE issue_id = ?
`, issueID)
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT %s AS depends_on_id FROM wisp_dependencies WHERE issue_id = ?
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get wisp dependencies: %w", err)
}
@ -618,9 +624,9 @@ func (s *DoltStore) getWispDependencies(ctx context.Context, issueID string) ([]
// getWispDependents retrieves issues that depend on a wisp.
func (s *DoltStore) getWispDependents(ctx context.Context, issueID string) ([]*types.Issue, error) {
rows, err := s.queryContext(ctx, `
SELECT issue_id FROM wisp_dependencies WHERE depends_on_id = ?
`, issueID)
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM wisp_dependencies WHERE %s = ?
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get wisp dependents: %w", err)
}
@ -648,9 +654,9 @@ func (s *DoltStore) getWispDependents(ctx context.Context, issueID string) ([]*t
// getWispDependenciesWithMetadata returns wisp dependencies with metadata.
func (s *DoltStore) getWispDependenciesWithMetadata(ctx context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
rows, err := s.queryContext(ctx, `
SELECT depends_on_id, type FROM wisp_dependencies WHERE issue_id = ?
`, issueID)
rows, err := s.queryContext(ctx, fmt.Sprintf(`
SELECT %s AS depends_on_id, type FROM wisp_dependencies WHERE issue_id = ?
`, issueops.DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("failed to get wisp dependencies with metadata: %w", err)
}

View file

@ -6,6 +6,7 @@ import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
@ -38,12 +39,15 @@ func TestWispCycleReachabilityQueryMultipleTablesTraversesUniqueNodes(t *testing
if strings.Contains(query, "UNION ALL") || strings.Contains(query, "depth") {
t.Fatalf("multi-table wisp cycle query should traverse unique nodes, not enumerate paths:\n%s", query)
}
if !strings.Contains(query, "SELECT issue_id, depends_on_id FROM dependencies") {
if !strings.Contains(query, "FROM dependencies") {
t.Fatalf("query does not include dependencies table:\n%s", query)
}
if !strings.Contains(query, "SELECT issue_id, depends_on_id FROM wisp_dependencies") {
if !strings.Contains(query, "FROM wisp_dependencies") {
t.Fatalf("query does not include wisp_dependencies table:\n%s", query)
}
if !strings.Contains(query, issueops.DepTargetExpr) {
t.Fatalf("query does not resolve depends_on_id via DepTargetExpr:\n%s", query)
}
}
func TestAddDependencyRejectsPermanentEndpointCycleThroughWisp(t *testing.T) {

View file

@ -261,9 +261,9 @@ func markBlockedFromDepsInTx(
placeholders, args := buildSQLInClause(allSpawnerIDs[start:end])
childRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, depends_on_id FROM %s
WHERE type = 'parent-child' AND depends_on_id IN (%s)
`, depTbl, placeholders), args...)
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, DepTargetExpr, depTbl, DepTargetExpr, placeholders), args...)
if err != nil {
if optionalBlockedTable(depTbl) && isTableNotExistError(err) {
continue
@ -382,10 +382,10 @@ func loadBlockingDepsForIssueIDsInTx(ctx context.Context, tx *sql.Tx, depTables
}
placeholders, args := buildSQLInClause(issueIDs[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, depends_on_id, type, metadata FROM %s
SELECT issue_id, %s AS depends_on_id, type, metadata FROM %s
WHERE issue_id IN (%s)
AND type IN ('blocks', 'waits-for', 'conditional-blocks')
`, depTable, placeholders), args...)
`, DepTargetExpr, depTable, placeholders), args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -459,10 +459,10 @@ func loadParentIDsForChildrenInTx(ctx context.Context, tx *sql.Tx, depTables []s
}
placeholders, args := buildSQLInClause(childIDs[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, depends_on_id FROM %s
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE issue_id IN (%s)
AND type = 'parent-child'
`, depTable, placeholders), args...)
`, DepTargetExpr, depTable, placeholders), args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -504,9 +504,9 @@ func GetChildrenWithParentsInTx(ctx context.Context, tx *sql.Tx, parentIDs []str
placeholders, args := buildSQLInClause(parentIDs[start:end])
query := fmt.Sprintf(`
SELECT issue_id, depends_on_id FROM %s
WHERE type = 'parent-child' AND depends_on_id IN (%s)
`, depTable, placeholders)
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, DepTargetExpr, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
@ -549,8 +549,8 @@ func GetChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child' AND depends_on_id IN (%s)
`, depTable, placeholders)
WHERE type = 'parent-child' AND %s IN (%s)
`, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
@ -586,16 +586,17 @@ func GetDescendantIDsInTx(ctx context.Context, tx *sql.Tx, rootID string, maxDep
}
queryDescendants := func(includeWisps bool) ([]string, bool, error) {
edgeQuery := `
SELECT issue_id, depends_on_id FROM dependencies WHERE type = 'parent-child'
`
edgeQuery := fmt.Sprintf(`
SELECT issue_id, %s FROM dependencies WHERE type = 'parent-child'
`, DepTargetExpr)
if includeWisps {
edgeQuery += `
edgeQuery += fmt.Sprintf(`
UNION ALL
SELECT issue_id, depends_on_id FROM wisp_dependencies WHERE type = 'parent-child'
`
SELECT issue_id, %s FROM wisp_dependencies WHERE type = 'parent-child'
`, DepTargetExpr)
}
//nolint:gosec // G201: edgeQuery is built from hardcoded SQL plus DepTargetExpr (no user input)
query := fmt.Sprintf(`
WITH RECURSIVE
parent_edges(issue_id, depends_on_id) AS (

View file

@ -12,8 +12,8 @@ import (
const (
activeIssuesQuery = `(?s)SELECT id FROM issues\s+WHERE status NOT IN \('closed', 'pinned'\)`
blockingDepsQuery = `(?s)SELECT issue_id, depends_on_id, type, metadata FROM dependencies\s+WHERE issue_id IN \(.+\)\s+AND type IN \('blocks', 'waits-for', 'conditional-blocks'\)`
childrenQuery = `(?s)SELECT issue_id, depends_on_id FROM dependencies\s+WHERE type = 'parent-child' AND depends_on_id IN \(.+\)`
blockingDepsQuery = `(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id, type, metadata FROM dependencies\s+WHERE issue_id IN \(.+\)\s+AND type IN \('blocks', 'waits-for', 'conditional-blocks'\)`
childrenQuery = `(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id FROM dependencies\s+WHERE type = 'parent-child' AND COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) IN \(.+\)`
activeChildQuery = `(?s)SELECT id FROM issues\s+WHERE id IN \(.+\)\s+AND status NOT IN \('closed', 'pinned'\)`
closedChildQuery = `(?s)SELECT id FROM issues\s+WHERE status = 'closed' AND id IN \(.+\)`
)

View file

@ -293,8 +293,10 @@ func FindWispDependentsRecursiveInTx(ctx context.Context, tx *sql.Tx, ids []stri
toProcess = toProcess[end:]
placeholders, args := buildSQLInClause(batch)
// IDs from recursive traversal may target any kind (issue/wisp/external),
// so resolve via DepTargetExpr rather than a typed column.
rows, err := tx.QueryContext(ctx,
fmt.Sprintf(`SELECT issue_id FROM wisp_dependencies WHERE depends_on_id IN (%s)`, placeholders),
fmt.Sprintf(`SELECT issue_id FROM wisp_dependencies WHERE %s IN (%s)`, DepTargetExpr, placeholders),
args...)
if err != nil {
return discovered, fmt.Errorf("query wisp dependents: %w", err)

View file

@ -103,7 +103,7 @@ func GetTier1CandidatesInTx(ctx context.Context, tx *sql.Tx) ([]*types.Compactio
rows, err := tx.QueryContext(ctx, `
SELECT i.id, i.closed_at,
CHAR_LENGTH(i.description) + CHAR_LENGTH(i.design) + CHAR_LENGTH(i.notes) + CHAR_LENGTH(i.acceptance_criteria) AS original_size,
COALESCE((SELECT COUNT(*) FROM dependencies d WHERE d.depends_on_id = i.id AND d.type = 'blocks'), 0) AS dependent_count
COALESCE((SELECT COUNT(*) FROM dependencies d WHERE d.depends_on_issue_id = i.id AND d.type = 'blocks'), 0) AS dependent_count
FROM issues i
WHERE i.status = ?
AND i.closed_at IS NOT NULL
@ -124,7 +124,7 @@ func GetTier2CandidatesInTx(ctx context.Context, tx *sql.Tx) ([]*types.Compactio
rows, err := tx.QueryContext(ctx, `
SELECT i.id, i.closed_at,
CHAR_LENGTH(i.description) + CHAR_LENGTH(i.design) + CHAR_LENGTH(i.notes) + CHAR_LENGTH(i.acceptance_criteria) AS original_size,
COALESCE((SELECT COUNT(*) FROM dependencies d WHERE d.depends_on_id = i.id AND d.type = 'blocks'), 0) AS dependent_count
COALESCE((SELECT COUNT(*) FROM dependencies d WHERE d.depends_on_issue_id = i.id AND d.type = 'blocks'), 0) AS dependent_count
FROM issues i
WHERE i.status = ?
AND i.closed_at IS NOT NULL
@ -158,12 +158,16 @@ func scanCompactionCandidates(rows *sql.Rows) ([]*types.CompactionCandidate, err
func GetMoleculeLastActivityInTx(ctx context.Context, tx *sql.Tx, moleculeID string) (*types.MoleculeLastActivity, error) {
isWisp := IsActiveWispInTx(ctx, tx, moleculeID)
issueTable, _, _, depTable := WispTableRouting(isWisp)
parentCol := "depends_on_issue_id"
if isWisp {
parentCol = "depends_on_wisp_id"
}
// Get child IDs
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE depends_on_id = ? AND type = 'parent-child'
`, depTable), moleculeID)
WHERE %s = ? AND type = 'parent-child'
`, depTable, parentCol), moleculeID)
if err != nil {
return nil, fmt.Errorf("get molecule children: %w", err)
}

View file

@ -19,9 +19,9 @@ func DetectCyclesInTx(ctx context.Context, tx *sql.Tx) ([][]*types.Issue, error)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, depends_on_id, type
SELECT issue_id, %s AS depends_on_id, type
FROM %s
`, depTable))
`, DepTargetExpr, depTable))
if err != nil {
return nil, fmt.Errorf("detect cycles: query %s: %w", depTable, err)
}

View file

@ -98,7 +98,7 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
inClause, args := buildSQLInClause(batch)
rows, err := tx.QueryContext(ctx,
fmt.Sprintf(`SELECT depends_on_id, issue_id FROM dependencies WHERE depends_on_id IN (%s)`, inClause),
fmt.Sprintf(`SELECT depends_on_issue_id, issue_id FROM dependencies WHERE depends_on_issue_id IN (%s)`, inClause),
args...)
if err != nil {
return nil, fmt.Errorf("check dependents: %w", err)
@ -184,7 +184,7 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
batchInClause, batchArgs := buildSQLInClause(batch)
rows, err := tx.QueryContext(ctx,
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_id IN (%s)`, batchInClause),
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_issue_id IN (%s)`, batchInClause),
batchArgs...)
if err != nil {
return nil, fmt.Errorf("count inbound dependencies: %w", err)
@ -264,7 +264,7 @@ func findAllDependentsRecursiveInTx(ctx context.Context, tx *sql.Tx, ids []strin
inClause, args := buildSQLInClause(batch)
rows, err := tx.QueryContext(ctx,
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_id IN (%s)`, inClause),
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_issue_id IN (%s)`, inClause),
args...)
if err != nil {
return nil, fmt.Errorf("query dependents for batch: %w", err)
@ -305,7 +305,7 @@ func findExternalDependentsBatchedInTx(ctx context.Context, tx *sql.Tx, ids []st
inClause, args := buildSQLInClause(batch)
rows, err := tx.QueryContext(ctx,
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_id IN (%s)`, inClause),
fmt.Sprintf(`SELECT issue_id FROM dependencies WHERE depends_on_issue_id IN (%s)`, inClause),
args...)
if err != nil {
return nil, fmt.Errorf("query dependents: %w", err)

View file

@ -29,6 +29,12 @@ func (k DepTargetKind) Column() string {
}
}
// DepTargetExpr is the SQL expression that resolves a dependency row's target
// id from its three typed columns. Use this in SELECT projections (aliased as
// depends_on_id) and in WHERE clauses when the caller doesn't know the target
// kind ahead of time.
const DepTargetExpr = "COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external)"
func ClassifyDepTarget(ctx context.Context, tx *sql.Tx, dep *types.Dependency, isCrossPrefix bool) DepTargetKind {
if isCrossPrefix || strings.HasPrefix(dep.DependsOnID, "external:") {
return DepTargetExternal
@ -165,16 +171,26 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
}
}
// Classify the target so the pair lookup and INSERT both hit the same
// typed column.
var kind DepTargetKind
if opts.TargetKind != nil {
kind = *opts.TargetKind
} else {
kind = ClassifyDepTarget(ctx, tx, dep, opts.IsCrossPrefix)
}
targetCol := kind.Column()
// Check for existing dependency between the same pair.
var existingType string
//nolint:gosec // G201: writeTable is from WispTableRouting ("dependencies" or "wisp_dependencies")
err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT type FROM %s WHERE issue_id = ? AND depends_on_id = ?`, writeTable),
//nolint:gosec // G201: writeTable from WispTableRouting; targetCol from DepTargetKind.Column()
err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT type FROM %s WHERE issue_id = ? AND %s = ?`, writeTable, targetCol),
dep.IssueID, dep.DependsOnID).Scan(&existingType)
if err == nil {
if existingType == string(dep.Type) {
// Same type — idempotent; update metadata.
//nolint:gosec // G201: writeTable is from WispTableRouting
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE %s SET metadata = ? WHERE issue_id = ? AND depends_on_id = ?`, writeTable),
//nolint:gosec // G201: writeTable from WispTableRouting; targetCol from DepTargetKind.Column()
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE %s SET metadata = ? WHERE issue_id = ? AND %s = ?`, writeTable, targetCol),
metadata, dep.IssueID, dep.DependsOnID); err != nil {
return fmt.Errorf("failed to update dependency metadata: %w", err)
}
@ -186,18 +202,11 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
return fmt.Errorf("failed to check existing dependency: %w", err)
}
var kind DepTargetKind
if opts.TargetKind != nil {
kind = *opts.TargetKind
} else {
kind = ClassifyDepTarget(ctx, tx, dep, opts.IsCrossPrefix)
}
//nolint:gosec // G201: writeTable from WispTableRouting; target column from DepTargetKind.Column()
//nolint:gosec // G201: writeTable from WispTableRouting; targetCol from DepTargetKind.Column()
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
INSERT INTO %s (issue_id, %s, type, created_at, created_by, metadata, thread_id)
VALUES (?, ?, ?, NOW(), ?, ?, ?)
`, writeTable, kind.Column()), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
`, writeTable, targetCol), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
return fmt.Errorf("failed to add dependency: %w", err)
}
return nil
@ -211,17 +220,17 @@ func cycleReachabilityQuery(depTables []string) string {
WITH RECURSIVE reachable(node) AS (
SELECT ?
UNION
SELECT d.depends_on_id
SELECT %s
FROM reachable r
JOIN %s d ON d.issue_id = r.node AND d.type IN ('blocks', 'conditional-blocks')
)
SELECT COUNT(*) FROM reachable WHERE node = ?
`, depTables[0])
`, DepTargetExpr, depTables[0])
}
var unions []string
for _, t := range depTables {
unions = append(unions, fmt.Sprintf("SELECT issue_id, depends_on_id FROM %s WHERE type IN ('blocks', 'conditional-blocks')", t))
unions = append(unions, fmt.Sprintf("SELECT issue_id, %s AS depends_on_id FROM %s WHERE type IN ('blocks', 'conditional-blocks')", DepTargetExpr, t))
}
unionQuery := strings.Join(unions, " UNION ")
return fmt.Sprintf(`
@ -354,13 +363,18 @@ func moveWispDependencyTargets(ctx context.Context, tx *sql.Tx, table string, ol
// existing transaction. Automatically routes to wisp_dependencies if the
// source issue is an active wisp.
//
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
// The target is matched via DepTargetExpr rather than a typed column: we
// can't reliably classify the target at remove time (it may have been
// promoted/deleted since the row was written), and CHECK guarantees the row
// has exactly one typed column set so COALESCE matches unambiguously.
//
//nolint:gosec // G201: depTable from WispTableRouting (hardcoded constants)
func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID string) error {
isWisp := IsActiveWispInTx(ctx, tx, issueID)
_, _, _, depTable := WispTableRouting(isWisp)
if _, err := tx.ExecContext(ctx, fmt.Sprintf(
`DELETE FROM %s WHERE issue_id = ? AND depends_on_id = ?`, depTable),
`DELETE FROM %s WHERE issue_id = ? AND %s = ?`, depTable, DepTargetExpr),
issueID, dependsOnID); err != nil {
return fmt.Errorf("remove dependency: %w", err)
}
@ -485,7 +499,7 @@ func GetDependenciesWithMetadataInTx(ctx context.Context, tx *sql.Tx, issueID st
var deps []depMeta
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT depends_on_id, type FROM %s WHERE issue_id = ?`, depTable), issueID)
`SELECT %s AS depends_on_id, type FROM %s WHERE issue_id = ?`, DepTargetExpr, depTable), issueID)
if err != nil {
return nil, fmt.Errorf("get dependencies from %s: %w", depTable, err)
}
@ -548,7 +562,7 @@ func GetDependentsWithMetadataInTx(ctx context.Context, tx *sql.Tx, issueID stri
var deps []depMeta
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT issue_id, type FROM %s WHERE depends_on_id = ?`, depTable), issueID)
`SELECT issue_id, type FROM %s WHERE %s = ?`, depTable, DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("get dependents from %s: %w", depTable, err)
}
@ -606,7 +620,7 @@ func GetDependenciesInTx(ctx context.Context, tx *sql.Tx, issueID string) ([]*ty
var ids []string
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT depends_on_id FROM %s WHERE issue_id = ?`, depTable), issueID)
`SELECT %s AS depends_on_id FROM %s WHERE issue_id = ?`, DepTargetExpr, depTable), issueID)
if err != nil {
return nil, fmt.Errorf("get dependencies from %s: %w", depTable, err)
}
@ -639,7 +653,7 @@ func GetDependentsInTx(ctx context.Context, tx *sql.Tx, issueID string) ([]*type
var ids []string
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT issue_id FROM %s WHERE depends_on_id = ?`, depTable), issueID)
`SELECT issue_id FROM %s WHERE %s = ?`, depTable, DepTargetExpr), issueID)
if err != nil {
return nil, fmt.Errorf("get dependents from %s: %w", depTable, err)
}

View file

@ -35,10 +35,13 @@ func TestCycleReachabilityQueryMultipleTablesTraversesUniqueNodes(t *testing.T)
if strings.Contains(query, "UNION ALL") || strings.Contains(query, "depth") {
t.Fatalf("multi-table cycle query should traverse unique nodes, not enumerate paths:\n%s", query)
}
if !strings.Contains(query, "SELECT issue_id, depends_on_id FROM dependencies") {
if !strings.Contains(query, "FROM dependencies") {
t.Fatalf("query does not include dependencies table:\n%s", query)
}
if !strings.Contains(query, "SELECT issue_id, depends_on_id FROM wisp_dependencies") {
if !strings.Contains(query, "FROM wisp_dependencies") {
t.Fatalf("query does not include wisp_dependencies table:\n%s", query)
}
if !strings.Contains(query, DepTargetExpr) {
t.Fatalf("query does not resolve depends_on_id via DepTargetExpr:\n%s", query)
}
}

View file

@ -11,11 +11,11 @@ import (
// GetAllDependencyRecordsInTx returns all dependency records from the dependencies table.
func GetAllDependencyRecordsInTx(ctx context.Context, tx *sql.Tx) (map[string][]*types.Dependency, error) {
rows, err := tx.QueryContext(ctx, `
SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM dependencies
ORDER BY issue_id
`)
`, DepTargetExpr))
if err != nil {
return nil, fmt.Errorf("get all dependency records: %w", err)
}
@ -90,9 +90,9 @@ func getDependencyRecordsIntoFromTable(ctx context.Context, tx *sql.Tx, depTable
args[i] = id
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT issue_id, depends_on_id, type, created_at, created_by, metadata, thread_id
`SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s WHERE issue_id IN (%s) ORDER BY issue_id`,
depTable, strings.Join(placeholders, ",")), args...)
DepTargetExpr, depTable, strings.Join(placeholders, ",")), args...)
if err != nil {
return fmt.Errorf("get dependency records from %s: %w", depTable, err)
}
@ -166,14 +166,16 @@ func GetDependencyCountsInTx(ctx context.Context, tx *sql.Tx, issueIDs []string)
return nil, fmt.Errorf("get dependency counts: blocker rows: %w", err)
}
// Dependents: issues blocked by the given IDs
// Dependents: issues blocked by the given IDs.
// IDs can be of any target kind (perm/wisp/external), so resolve via
// COALESCE; the table is `dependencies` only, matching prior behavior.
//nolint:gosec // G201: inClause contains only ? placeholders
blockingRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT depends_on_id, COUNT(*) as cnt
SELECT %s AS depends_on_id, COUNT(*) as cnt
FROM dependencies
WHERE depends_on_id IN (%s) AND type = 'blocks'
GROUP BY depends_on_id
`, inClause), args...)
WHERE %s IN (%s) AND type = 'blocks'
GROUP BY %s
`, DepTargetExpr, DepTargetExpr, inClause, DepTargetExpr), args...)
if err != nil {
return nil, fmt.Errorf("get dependency counts (dependents): %w", err)
}
@ -226,14 +228,14 @@ func GetBlockingInfoForIssuesInTx(ctx context.Context, tx *sql.Tx, issueIDs []st
// Process wisp IDs against wisp_dependencies.
if len(wispIDs) > 0 {
if err := queryBlockingInfo(ctx, tx, wispIDs, "wisp_dependencies", "wisps", blockedByMap, blocksMap, parentMap); err != nil {
if err := queryBlockingInfo(ctx, tx, wispIDs, "wisp_dependencies", "wisps", "depends_on_wisp_id", blockedByMap, blocksMap, parentMap); err != nil {
return nil, nil, nil, err
}
}
// Process perm IDs against dependencies.
if len(permIDs) > 0 {
if err := queryBlockingInfo(ctx, tx, permIDs, "dependencies", "issues", blockedByMap, blocksMap, parentMap); err != nil {
if err := queryBlockingInfo(ctx, tx, permIDs, "dependencies", "issues", "depends_on_issue_id", blockedByMap, blocksMap, parentMap); err != nil {
return nil, nil, nil, err
}
}
@ -243,10 +245,15 @@ func GetBlockingInfoForIssuesInTx(ctx context.Context, tx *sql.Tx, issueIDs []st
// queryBlockingInfo queries blocking info from a specific dep table + issue table pair.
// Uses batched IN clauses (queryBatchSize) to avoid query-planner spikes.
//
// targetCol names the typed column that resolves a row's target id for the
// given issueTable (depends_on_issue_id for issues, depends_on_wisp_id for
// wisps). External rows can't be blockers (no row in any local table) so they
// are skipped by joining only on the typed column.
func queryBlockingInfo(
ctx context.Context, tx *sql.Tx,
issueIDs []string,
depTable, issueTable string,
depTable, issueTable, targetCol string,
blockedByMap map[string][]string,
blocksMap map[string][]string,
parentMap map[string]string,
@ -266,14 +273,18 @@ func queryBlockingInfo(
}
inClause := strings.Join(placeholders, ",")
// Query 1: "blocked by" — deps where issue_id is in our set
//nolint:gosec // G201: depTable and issueTable are caller-controlled constants
// Query 1: "blocked by" — deps where issue_id is in our set.
// Project COALESCE so cross-table targets (wisp/external blockers in a
// perm row, etc.) still return their id; status JOIN narrows to the
// typed column for index use, but rows that don't join still surface
// with the blocker id and an empty status, preserving prior semantics.
//nolint:gosec // G201: depTable, issueTable, targetCol are caller-controlled constants
blockedByQuery := fmt.Sprintf(`
SELECT d.issue_id, d.depends_on_id, d.type, COALESCE(i.status, '') AS blocker_status
SELECT d.issue_id, %s AS depends_on_id, d.type, COALESCE(i.status, '') AS blocker_status
FROM %s d
LEFT JOIN %s i ON i.id = d.depends_on_id
LEFT JOIN %s i ON i.id = d.%s
WHERE d.issue_id IN (%s) AND d.type IN ('blocks', 'parent-child')
`, depTable, issueTable, inClause)
`, DepTargetExpr, depTable, issueTable, targetCol, inClause)
rows, err := tx.QueryContext(ctx, blockedByQuery, args...)
if err != nil {
@ -299,14 +310,14 @@ func queryBlockingInfo(
return fmt.Errorf("get blocking info: blocked-by rows: %w", err)
}
// Query 2: "blocks" — deps where depends_on_id is in our set
//nolint:gosec // G201: depTable and issueTable are caller-controlled constants
// Query 2: "blocks" — deps where the typed target is in our set
//nolint:gosec // G201: depTable, issueTable, targetCol are caller-controlled constants
blocksQuery := fmt.Sprintf(`
SELECT d.depends_on_id, d.issue_id, d.type, COALESCE(i.status, '') AS blocker_status
SELECT d.%s, d.issue_id, d.type, COALESCE(i.status, '') AS blocker_status
FROM %s d
LEFT JOIN %s i ON i.id = d.depends_on_id
WHERE d.depends_on_id IN (%s) AND d.type IN ('blocks', 'parent-child')
`, depTable, issueTable, inClause)
LEFT JOIN %s i ON i.id = d.%s
WHERE d.%s IN (%s) AND d.type IN ('blocks', 'parent-child')
`, targetCol, depTable, issueTable, targetCol, targetCol, inClause)
rows2, err := tx.QueryContext(ctx, blocksQuery, args...)
if err != nil {
@ -349,8 +360,8 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE depends_on_id = ? AND type = 'blocks'
`, depTable), closedIssueID)
WHERE %s = ? AND type = 'blocks'
`, depTable, DepTargetExpr), closedIssueID)
if err != nil {
return nil, fmt.Errorf("find blocked candidates from %s: %w", depTable, err)
}
@ -406,9 +417,9 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
//nolint:gosec // G201: depTable from WispTableRouting (hardcoded)
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT depends_on_id FROM %s
WHERE issue_id = ? AND type = 'blocks' AND depends_on_id != ?
`, depTable), candidateID, closedIssueID)
SELECT %s AS depends_on_id FROM %s
WHERE issue_id = ? AND type = 'blocks' AND %s != ?
`, DepTargetExpr, depTable, DepTargetExpr), candidateID, closedIssueID)
if err != nil {
return nil, fmt.Errorf("check remaining blockers for %s: %w", candidateID, err)
}
@ -467,9 +478,9 @@ func IsBlockedInTx(ctx context.Context, tx *sql.Tx, issueID string) (bool, []str
dependsOnID, depType string
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT depends_on_id, type FROM %s
SELECT %s AS depends_on_id, type FROM %s
WHERE issue_id = ? AND type IN ('blocks', 'waits-for', 'conditional-blocks')
`, depTable), issueID)
`, DepTargetExpr, depTable), issueID)
if err != nil {
return false, nil, fmt.Errorf("check blockers: %w", err)
}

View file

@ -48,8 +48,8 @@ func GetEpicsEligibleForClosureInTx(ctx context.Context, tx *sql.Tx) ([]*types.E
}
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT depends_on_id, issue_id FROM %s
WHERE type = 'parent-child'
SELECT depends_on_issue_id, issue_id FROM %s
WHERE type = 'parent-child' AND depends_on_issue_id IS NOT NULL
`, depTable))
if err != nil {
if isTableNotExistError(err) {

View file

@ -134,7 +134,7 @@ func BuildIssueFilterClauses(query string, filter types.IssueFilter, tables Filt
if filter.ParentID != nil {
parentID := *filter.ParentID
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM %s WHERE type = 'parent-child' AND depends_on_id = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child')))", tables.Dependencies, tables.Dependencies))
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM %s WHERE type = 'parent-child' AND %s = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child')))", tables.Dependencies, DepTargetExpr, tables.Dependencies))
args = append(args, parentID, parentID)
}
if filter.NoParent {

View file

@ -20,6 +20,10 @@ func GetMoleculeProgressInTx(ctx context.Context, tx *sql.Tx, moleculeID string)
isWisp := IsActiveWispInTx(ctx, tx, moleculeID)
issueTable, _, _, depTable := WispTableRouting(isWisp)
parentCol := "depends_on_issue_id"
if isWisp {
parentCol = "depends_on_wisp_id"
}
// Get molecule title.
var title sql.NullString
@ -31,8 +35,8 @@ func GetMoleculeProgressInTx(ctx context.Context, tx *sql.Tx, moleculeID string)
// Step 1: Get child issue IDs from dependencies table.
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE depends_on_id = ? AND type = 'parent-child'
`, depTable), moleculeID)
WHERE %s = ? AND type = 'parent-child'
`, depTable, parentCol), moleculeID)
if err != nil {
return nil, fmt.Errorf("failed to get molecule children: %w", err)
}

View file

@ -130,7 +130,7 @@ func GetReadyWorkInTx(
// Molecule filtering: filter to direct children of the specified molecule.
if filter.MoleculeID != "" {
whereClauses = append(whereClauses, "(id IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child' AND depends_on_id = ?) OR (id LIKE CONCAT(?, '.%') AND id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child')))")
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child' AND %s = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child')))", DepTargetExpr))
args = append(args, filter.MoleculeID, filter.MoleculeID)
}
@ -603,8 +603,8 @@ func getChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child' AND depends_on_id IN (%s)
`, depTable, placeholders)
WHERE type = 'parent-child' AND %s IN (%s)
`, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
// wisp_dependencies table may not exist on pre-migration databases.

View file

@ -0,0 +1,6 @@
-- Reverse of 0043: not reversible. The application no longer writes to a
-- polymorphic depends_on_id column; reintroducing the generated column would
-- not be a problem on its own, but the surrogate id PK and the three typed
-- UNIQUE KEYs cannot be unwound to the original (issue_id, depends_on_id) PK
-- without rewriting every existing row's key. Intentional no-op; restore from
-- a prior dolt commit if a rollback is needed.

View file

@ -0,0 +1,90 @@
SET FOREIGN_KEY_CHECKS = 0;
SET @needs_drop = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dependencies'
AND COLUMN_NAME = 'depends_on_id'
);
SET @has_idx_type_target = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dependencies'
AND INDEX_NAME = 'idx_dep_type_target'
);
SET @sql = IF(@needs_drop = 1 AND @has_idx_type_target = 1,
'ALTER TABLE dependencies DROP INDEX idx_dep_type_target',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Dolt blocks DROP PRIMARY KEY while any FK references the table, even if the
-- FK doesn't reference the PK columns. Drop fk_dep_issue_target now and re-add
-- it after the schema reshape.
SET @has_fk_issue_target = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dependencies'
AND CONSTRAINT_NAME = 'fk_dep_issue_target'
);
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue_target = 1,
'ALTER TABLE dependencies DROP FOREIGN KEY fk_dep_issue_target',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies DROP PRIMARY KEY',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies DROP COLUMN depends_on_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD UNIQUE KEY uk_dep_issue_target (issue_id, depends_on_issue_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD UNIQUE KEY uk_dep_wisp_target (issue_id, depends_on_wisp_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD UNIQUE KEY uk_dep_external_target (issue_id, depends_on_external)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD INDEX idx_dep_type_issue (type, depends_on_issue_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD INDEX idx_dep_type_wisp (type, depends_on_wisp_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE dependencies ADD INDEX idx_dep_type_external (type, depends_on_external)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Restore the FK we dropped above.
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue_target = 1,
'ALTER TABLE dependencies ADD CONSTRAINT fk_dep_issue_target FOREIGN KEY (depends_on_issue_id) REFERENCES issues(id) ON DELETE CASCADE ON UPDATE CASCADE',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

View file

@ -0,0 +1,4 @@
-- Reverse of 0044: not reversible while the depends_on_id generated column is
-- gone (dropped in 0043). Reapplying the prior view definitions would fail
-- because they reference the dropped column. Intentional no-op; restore from a
-- prior dolt commit if a rollback is needed.

View file

@ -0,0 +1,40 @@
CREATE OR REPLACE VIEW ready_issues AS
WITH RECURSIVE
blocked_directly AS (
SELECT DISTINCT d.issue_id
FROM dependencies d
WHERE d.type = 'blocks'
AND EXISTS (
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_issue_id
AND blocker.status NOT IN ('closed', 'pinned')
)
),
blocked_transitively AS (
SELECT issue_id, 0 as depth
FROM blocked_directly
UNION ALL
SELECT d.issue_id, bt.depth + 1
FROM blocked_transitively bt
JOIN dependencies d ON d.depends_on_issue_id = bt.issue_id
WHERE d.type = 'parent-child'
AND bt.depth < 50
)
SELECT i.*
FROM issues i
LEFT JOIN blocked_transitively bt ON bt.issue_id = i.id
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())
AND NOT EXISTS (
SELECT 1 FROM dependencies d_parent
JOIN issues parent ON parent.id = d_parent.depends_on_issue_id
WHERE d_parent.issue_id = i.id
AND d_parent.type = 'parent-child'
AND parent.defer_until IS NOT NULL
AND parent.defer_until > UTC_TIMESTAMP()
);

View file

@ -0,0 +1,4 @@
-- Reverse of 0045: not reversible while the depends_on_id generated column is
-- gone (dropped in 0043). Reapplying the prior view definition would fail
-- because it references the dropped column. Intentional no-op; restore from a
-- prior dolt commit if a rollback is needed.

View file

@ -0,0 +1,31 @@
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(*)
FROM dependencies d
WHERE d.issue_id = i.id
AND d.type = 'blocks'
AND EXISTS (
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_issue_id
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 ('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
AND d.type = 'blocks'
AND EXISTS (
SELECT 1 FROM issues blocker
WHERE blocker.id = d.depends_on_issue_id
AND blocker.status NOT IN ('closed', 'pinned')
AND blocker.status NOT IN (SELECT name FROM done_frozen)
)
);

View file

@ -0,0 +1,124 @@
SET FOREIGN_KEY_CHECKS = 0;
SET @needs_drop = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisp_dependencies'
AND COLUMN_NAME = 'depends_on_id'
);
SET @has_idx_type_target = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisp_dependencies'
AND INDEX_NAME = 'idx_wisp_dep_type_target'
);
SET @sql = IF(@needs_drop = 1 AND @has_idx_type_target = 1,
'ALTER TABLE wisp_dependencies DROP INDEX idx_wisp_dep_type_target',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Dolt blocks DROP PRIMARY KEY while any FK references the table, even if the
-- FK doesn't reference the PK columns. Drop all three FKs now and re-add them
-- after the schema reshape.
SET @has_fk_issue = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisp_dependencies'
AND CONSTRAINT_NAME = 'fk_wisp_dep_issue'
);
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue = 1,
'ALTER TABLE wisp_dependencies DROP FOREIGN KEY fk_wisp_dep_issue',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @has_fk_wisp_target = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisp_dependencies'
AND CONSTRAINT_NAME = 'fk_wisp_dep_wisp_target'
);
SET @sql = IF(@needs_drop = 1 AND @has_fk_wisp_target = 1,
'ALTER TABLE wisp_dependencies DROP FOREIGN KEY fk_wisp_dep_wisp_target',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @has_fk_issue_target = (
SELECT IF(COUNT(*) > 0, 1, 0)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisp_dependencies'
AND CONSTRAINT_NAME = 'fk_wisp_dep_issue_target'
);
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue_target = 1,
'ALTER TABLE wisp_dependencies DROP FOREIGN KEY fk_wisp_dep_issue_target',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies DROP PRIMARY KEY',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies DROP COLUMN depends_on_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD UNIQUE KEY uk_wisp_dep_issue_target (issue_id, depends_on_issue_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD UNIQUE KEY uk_wisp_dep_wisp_target (issue_id, depends_on_wisp_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD UNIQUE KEY uk_wisp_dep_external_target (issue_id, depends_on_external)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD INDEX idx_wisp_dep_type_issue (type, depends_on_issue_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD INDEX idx_wisp_dep_type_wisp (type, depends_on_wisp_id)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1,
'ALTER TABLE wisp_dependencies ADD INDEX idx_wisp_dep_type_external (type, depends_on_external)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Restore the FKs we dropped above.
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue = 1,
'ALTER TABLE wisp_dependencies ADD CONSTRAINT fk_wisp_dep_issue FOREIGN KEY (issue_id) REFERENCES wisps(id) ON DELETE CASCADE ON UPDATE CASCADE',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1 AND @has_fk_wisp_target = 1,
'ALTER TABLE wisp_dependencies ADD CONSTRAINT fk_wisp_dep_wisp_target FOREIGN KEY (depends_on_wisp_id) REFERENCES wisps(id) ON DELETE CASCADE ON UPDATE CASCADE',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql = IF(@needs_drop = 1 AND @has_fk_issue_target = 1,
'ALTER TABLE wisp_dependencies ADD CONSTRAINT fk_wisp_dep_issue_target FOREIGN KEY (depends_on_issue_id) REFERENCES issues(id) ON DELETE CASCADE ON UPDATE CASCADE',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;