{"record":{"id":"c0ee24300cbab4eb","repo":"gastownhall/beads","slug":"iterate-constraint-violations-w","errorCode":null,"errorMessage":"iterate constraint violations: %w","messagePattern":"iterate constraint violations: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/versioncontrolops/conflicts.go","lineNumber":537,"sourceCode":"\trows, err := db.QueryContext(ctx,\n\t\t\"SELECT `table`, num_violations FROM dolt_constraint_violations WHERE num_violations > 0\")\n\tif err != nil {\n\t\tif isMissingSystemTable(err) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"query constraint violations: %w\", err)\n\t}\n\tdefer func() { _ = rows.Close() }()\n\tvar out []storage.ConstraintViolation\n\tfor rows.Next() {\n\t\tvar v storage.ConstraintViolation\n\t\tif err := rows.Scan(&v.Table, &v.Count); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"scan constraint violation: %w\", err)\n\t\t}\n\t\tout = append(out, v)\n\t}\n\tif err := rows.Err(); err != nil {\n\t\treturn nil, fmt.Errorf(\"iterate constraint violations: %w\", err)\n\t}\n\treturn out, nil\n}\n\n// isMissingSystemTable reports whether err is dolt/MySQL's \"table does not\n// exist\". Matched on the message because the embedded engine and the MySQL\n// driver report it as different error types.\nfunc isMissingSystemTable(err error) bool {\n\tif err == nil {\n\t\treturn false\n\t}\n\tmsg := strings.ToLower(err.Error())\n\treturn strings.Contains(msg, \"table not found\") ||\n\t\tstrings.Contains(msg, \"doesn't exist\") ||\n\t\tstrings.Contains(msg, \"does not exist\") ||\n\t\tstrings.Contains(msg, \"unknown table\")\n}\n","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/versioncontrolops/conflicts.go#L519-L555","documentation":"After scanning dolt_constraint_violations rows, rows.Err() returned non-nil — the result-set iteration failed mid-stream (connection drop, context cancellation, or embedded engine error). Like its schema-conflicts sibling, this is a transport/engine failure while reading the violation list, not bad data.","triggerScenarios":"GetMergeBlockers iterating dolt_constraint_violations when the connection drops, the context deadline expires, sql-server closes the result, or the embedded engine errors while reading a large violation set.","commonSituations":"Slow servers with large violation lists and short context timeouts; flaky network to a remote dolt sql-server; connection-pool reclamation mid-query in long-lived services.","solutions":["Retry GetMergeBlockers on a fresh connection/context; mid-iteration failures are typically transient.","Increase context and driver timeouts when the violations table is large or the server is loaded.","Check server-side logs for the disconnect/error that terminated the result set.","Stabilize the connection (pool settings, keepalives) if this recurs during long merge-resolution sessions."],"exampleFix":"// before: one attempt with a tight deadline\nv, err := ops.GetMergeBlockers(ctx, db)\n// after: retry with an extended deadline on transient failure\nattempt := func() (storage.MergeBlockers, error) {\n  c, cancel := context.WithTimeout(ctx, 90*time.Second)\n  defer cancel()\n  return ops.GetMergeBlockers(c, db)\n}\nv, err := attempt()\nif err != nil && isTransient(err) { v, err = attempt() }","handlingStrategy":"retry","validationCode":"if err := db.PingContext(ctx); err != nil { return err }\nctx, cancel := context.WithTimeout(ctx, 90*time.Second); defer cancel()","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"iterate constraint violations\") {\n  time.Sleep(2 * time.Second)\n  blockers, err = ops.GetMergeBlockers(freshCtx, freshDB)\n}","preventionTips":["Set ample context timeouts for diagnostics on large violation tables.","Maintain stable connections (pool lifetimes, keepalives) during merge sessions.","Retry transient iteration errors instead of surfacing them to operators."],"tags":["dolt","result-set-iteration","network","constraint-violations"],"backgroundTag":"result-set-iteration-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}