feat(conflicts): dolt-backed conflict tests, --conclude, merge-blocker visibility (wy-36ilm)
The wy-jpd3.5 adversarial review's deferred findings. F9 (the important one): every wy-jpd3.5 test was a pure unit test over synthetic column maps — no test drove a REAL dolt conflict through GetConflictRows/ResolveConflictRows, the session flags, the store decorator chain, or the commit gate, which is why the review's BLOCKER (the decorator never satisfied ConflictInspector, so `bd conflicts show` printed "No merge conflicts" over a wedged merge) was invisible to the suite. conflicts_integration_test.go wedges a real two-branch modify/modify conflict and drives list/show, resolve --ours and --theirs, a miskeyed resolve, and the decorator chain — the last asserting BOTH halves: the naked assertion on the wrapper still fails, and UnwrapStore still reaches the concrete store. That test immediately caught a live bug: CommitMergeResolution went through commitWorkingSet, which returns early when no table is dirty — exactly the --ours case, where our values already stood and clearing the conflict dirtied nothing. The merge stayed open (is_merging true) while the CLI printed "Merge committed", and the next pull re-wedged. commitWorkingSet now concludes an open merge in the merge-resolution mode only; the other modes keep the old "nothing to commit" behavior. F4: `bd conflicts resolve --conclude` commits a merge whose conflicts are already gone — the state --no-commit leaves, or a partial resolve whose later ID errored out. Before, that merge could not be concluded through bd at all: with zero live conflicts --all returns early and a named ID errors "no live conflict". It refuses while anything is still outstanding, and says so rather than minting an empty commit when no merge is open. F12: totalConflicts summed dolt_conflicts only, so a schema conflict or a constraint violation the auto-repair declined could be outstanding while remaining==0 — CommitMergeResolution then failed with a raw dolt error and no guidance. storage.MergeBlockerInspector (GetMergeBlockers over dolt_merge_status, dolt_schema_conflicts, dolt_constraint_violations) is a new optional interface on both Dolt-backed stores; `bd conflicts list` and the resolve path report the blockers with the dolt commands that clear them, and hold the commit instead of letting it fail rawly. Each source is read independently and a missing system table is not an error — diagnosis must never be what fails the command.
This commit is contained in:
parent
c2796999d1
commit
6b7ec5bf7d
7 changed files with 676 additions and 21 deletions
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
|
@ -55,6 +56,7 @@ var (
|
|||
conflictsResolveAll bool
|
||||
conflictsResolveTable string
|
||||
conflictsNoCommit bool
|
||||
conflictsConclude bool
|
||||
)
|
||||
|
||||
var conflictsListCmd = &cobra.Command{
|
||||
|
|
@ -100,14 +102,35 @@ var conflictsListCmd = &cobra.Command{
|
|||
total += count
|
||||
out = append(out, tableOut{Table: t.Field, Count: count, Keys: keys})
|
||||
}
|
||||
// Schema conflicts and constraint violations are outstanding merge
|
||||
// state that dolt_conflicts never lists, so "No merge conflicts."
|
||||
// over a wedged merge was a lie (wy-36ilm F12).
|
||||
blockers, blockerErr := mergeBlockers(ctx)
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{
|
||||
payload := map[string]interface{}{
|
||||
"conflicts": total,
|
||||
"tables": out,
|
||||
})
|
||||
"blockers": blockers,
|
||||
}
|
||||
if blockerErr != nil {
|
||||
payload["blockers_error"] = blockerErr.Error()
|
||||
}
|
||||
return outputJSON(payload)
|
||||
}
|
||||
if total == 0 {
|
||||
fmt.Println("No merge conflicts.")
|
||||
switch {
|
||||
case blockers.Blocked():
|
||||
fmt.Println("No conflicted rows.")
|
||||
printMergeBlockers(blockers)
|
||||
case blockers.Merging:
|
||||
fmt.Println("No merge conflicts; a merge is open and resolved.")
|
||||
fmt.Println("Conclude it with: bd conflicts resolve --conclude")
|
||||
default:
|
||||
fmt.Println("No merge conflicts.")
|
||||
}
|
||||
if blockerErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not read schema conflicts/constraint violations: %v\n", blockerErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("\n%s %d live merge conflict(s):\n\n", ui.RenderAccent("!!"), total)
|
||||
|
|
@ -117,6 +140,10 @@ var conflictsListCmd = &cobra.Command{
|
|||
fmt.Printf(" %s\n", k)
|
||||
}
|
||||
}
|
||||
printMergeBlockers(blockers)
|
||||
if blockerErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not read schema conflicts/constraint violations: %v\n", blockerErr)
|
||||
}
|
||||
fmt.Printf("\nInspect with: bd conflicts show [<issue-id>]\n%s\n\n", conflictsResolveHint)
|
||||
return nil
|
||||
},
|
||||
|
|
@ -210,7 +237,8 @@ Examples:
|
|||
bd conflicts resolve bd-1234 --ours # keep our side of one issue
|
||||
bd conflicts resolve bd-1234 bd-5678 --theirs # take their side of two
|
||||
bd conflicts resolve --all --ours # every conflicted table
|
||||
bd conflicts resolve --all --table config --theirs`,
|
||||
bd conflicts resolve --all --table config --theirs
|
||||
bd conflicts resolve --conclude # commit an already-resolved merge`,
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -219,6 +247,21 @@ Examples:
|
|||
if err := requireConflictSupport(); err != nil {
|
||||
return err
|
||||
}
|
||||
// --conclude commits a merge whose conflicts are ALREADY gone: the
|
||||
// state left by --no-commit, or by a partial resolve whose later ID
|
||||
// errored out. Before this, that merge could not be concluded through
|
||||
// bd at all — with zero live conflicts, --all returns early and a
|
||||
// named ID errors "no live conflict" (wy-36ilm F4).
|
||||
if conflictsConclude {
|
||||
if len(args) > 0 || conflictsResolveAll || conflictsResolveOurs || conflictsResolveTheirs || conflictsResolveStrat != "" {
|
||||
return HandleErrorRespectJSON("--conclude takes no issue IDs and no strategy: it commits an already-resolved merge")
|
||||
}
|
||||
if conflictsNoCommit {
|
||||
return HandleErrorRespectJSON("--conclude and --no-commit are opposites")
|
||||
}
|
||||
return concludeResolvedMerge(ctx)
|
||||
}
|
||||
|
||||
strategy, err := resolveStrategy()
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
|
|
@ -287,26 +330,18 @@ Examples:
|
|||
return HandleErrorRespectJSON("resolved %d conflict(s) but failed to re-check conflicts: %v", resolved, err)
|
||||
}
|
||||
|
||||
// Schema conflicts and constraint violations survive a clean
|
||||
// dolt_conflicts, and CommitMergeResolution would fail on them with a
|
||||
// raw dolt error; hold the commit and explain instead (wy-36ilm F12).
|
||||
blockers, _ := mergeBlockers(ctx)
|
||||
|
||||
committed := false
|
||||
if resolved > 0 && remaining == 0 && !conflictsNoCommit {
|
||||
// CommitMergeResolution, not Commit: server-mode Commit excludes
|
||||
// the config table (GH#2455), so a resolved config conflict would
|
||||
// be dropped and the merge would re-wedge on the next pull
|
||||
// (GH#2474).
|
||||
if resolved > 0 && remaining == 0 && !conflictsNoCommit && !blockers.Blocked() {
|
||||
msg := fmt.Sprintf("Resolve %d merge conflict(s) using %s strategy", resolved, strategy)
|
||||
if err := store.CommitMergeResolution(ctx, msg); err != nil {
|
||||
return HandleErrorRespectJSON("conflicts resolved but commit failed: %v", err)
|
||||
if err := commitMergeResolution(ctx, msg, preHead); err != nil {
|
||||
return HandleErrorRespectJSON("conflicts resolved but %v", err)
|
||||
}
|
||||
committed = true
|
||||
// Same unwrap rule as conflictInspector: RecomputeBlockedAfterMerge
|
||||
// lives on the concrete Dolt store, not on DoltStorage.
|
||||
if rs, ok := storage.UnwrapStore(store).(interface {
|
||||
RecomputeBlockedAfterMerge(ctx context.Context, fromCommit string) error
|
||||
}); ok {
|
||||
if err := rs.RecomputeBlockedAfterMerge(ctx, preHead); err != nil {
|
||||
return HandleErrorRespectJSON("conflicts resolved but is_blocked recompute failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
|
|
@ -315,6 +350,7 @@ Examples:
|
|||
"strategy": strategy,
|
||||
"remaining": remaining,
|
||||
"committed": committed,
|
||||
"blockers": blockers,
|
||||
})
|
||||
}
|
||||
fmt.Printf("Resolved %d conflict(s) using '%s'.\n", resolved, strategy)
|
||||
|
|
@ -323,15 +359,82 @@ Examples:
|
|||
fmt.Printf("%d conflict(s) remain; the merge is not committed yet.\nRun: bd conflicts list\n", remaining)
|
||||
case committed:
|
||||
fmt.Println("Merge committed. Push when ready: bd sync")
|
||||
case blockers.Blocked():
|
||||
printMergeBlockers(blockers)
|
||||
case resolved == 0:
|
||||
fmt.Println("Nothing was conflicted; no commit made.")
|
||||
default:
|
||||
fmt.Println("All conflicts resolved; commit withheld. Conclude the merge with: bd dolt commit")
|
||||
fmt.Println("All conflicts resolved; commit withheld. Conclude the merge with: bd conflicts resolve --conclude")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// commitMergeResolution concludes the merge: CommitMergeResolution, not
|
||||
// Commit, because server-mode Commit excludes the config table (GH#2455), so
|
||||
// a resolved config conflict would be dropped and the merge would re-wedge on
|
||||
// the next pull (GH#2474). preHead scopes the is_blocked recompute the
|
||||
// merged-in writes bypassed, exactly as bd vc merge does (bd-578h9.11).
|
||||
func commitMergeResolution(ctx context.Context, msg, preHead string) error {
|
||||
if err := store.CommitMergeResolution(ctx, msg); err != nil {
|
||||
return fmt.Errorf("commit failed: %w", err)
|
||||
}
|
||||
// Same unwrap rule as conflictInspector: RecomputeBlockedAfterMerge lives
|
||||
// on the concrete Dolt store, not on DoltStorage.
|
||||
if rs, ok := storage.UnwrapStore(store).(interface {
|
||||
RecomputeBlockedAfterMerge(ctx context.Context, fromCommit string) error
|
||||
}); ok {
|
||||
if err := rs.RecomputeBlockedAfterMerge(ctx, preHead); err != nil {
|
||||
return fmt.Errorf("is_blocked recompute failed: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// concludeResolvedMerge commits a merge that has no live conflicts left —
|
||||
// `bd conflicts resolve --conclude` (wy-36ilm F4). It refuses while anything
|
||||
// is still outstanding, so it can only ever finish a resolution someone else
|
||||
// already made, never paper over one.
|
||||
func concludeResolvedMerge(ctx context.Context) error {
|
||||
remaining, err := totalConflicts(ctx)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("failed to read conflicts: %v", err)
|
||||
}
|
||||
if remaining > 0 {
|
||||
return HandleErrorRespectJSON("%d conflict(s) are still live; resolve them first (bd conflicts list)", remaining)
|
||||
}
|
||||
blockers, blockerErr := mergeBlockers(ctx)
|
||||
if blockers.Blocked() {
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{"committed": false, "remaining": 0, "blockers": blockers})
|
||||
}
|
||||
printMergeBlockers(blockers)
|
||||
return fmt.Errorf("merge not concluded: schema conflicts or constraint violations are outstanding")
|
||||
}
|
||||
// No merge open and nothing blocking: there is nothing to conclude. Say
|
||||
// so rather than minting an empty commit. A backend with no
|
||||
// MergeBlockerInspector reports Merging=false, so it keeps the old
|
||||
// behavior of just attempting the commit.
|
||||
_, haveStatus := storage.UnwrapStore(store).(storage.MergeBlockerInspector)
|
||||
if haveStatus && blockerErr == nil && !blockers.Merging {
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{"committed": false, "remaining": 0, "merging": false})
|
||||
}
|
||||
fmt.Println("No merge is in progress; nothing to conclude.")
|
||||
return nil
|
||||
}
|
||||
|
||||
preHead, _ := store.GetCurrentCommit(ctx)
|
||||
if err := commitMergeResolution(ctx, "Conclude resolved merge", preHead); err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{"committed": true, "remaining": 0})
|
||||
}
|
||||
fmt.Println("Merge committed. Push when ready: bd sync")
|
||||
return nil
|
||||
}
|
||||
|
||||
// conflictsMetrics starts a command metrics event and returns its closer, so
|
||||
// each subcommand can arm it with one deferred call.
|
||||
func conflictsMetrics(name string) func() {
|
||||
|
|
@ -387,6 +490,44 @@ func conflictRows(ctx context.Context, table string) ([]storage.ConflictRow, err
|
|||
return inspector.GetConflictRows(ctx, table)
|
||||
}
|
||||
|
||||
// mergeBlockers reports the non-row merge state — schema conflicts and
|
||||
// constraint violations — that blocks the merge commit while
|
||||
// dolt_conflicts is empty (wy-36ilm F12). Same UnwrapStore rule as
|
||||
// conflictInspector. A backend without the interface reports nothing, and a
|
||||
// read error is the caller's to soften: this is diagnosis, never a gate.
|
||||
func mergeBlockers(ctx context.Context) (storage.MergeBlockers, error) {
|
||||
inspector, ok := storage.UnwrapStore(store).(storage.MergeBlockerInspector)
|
||||
if !ok {
|
||||
return storage.MergeBlockers{}, nil
|
||||
}
|
||||
return inspector.GetMergeBlockers(ctx)
|
||||
}
|
||||
|
||||
// printMergeBlockers renders the blockers an operator has to clear by hand.
|
||||
// dolt offers no ours/theirs for either class, so this is diagnosis plus the
|
||||
// dolt commands that do resolve them — the guidance the raw commit error
|
||||
// (wy-36ilm F12) never carried.
|
||||
func printMergeBlockers(b storage.MergeBlockers) {
|
||||
if !b.Blocked() {
|
||||
return
|
||||
}
|
||||
fmt.Printf("\n%s the merge cannot be committed yet:\n", ui.RenderAccent("!!"))
|
||||
for _, t := range b.SchemaConflictTables {
|
||||
fmt.Printf(" schema conflict: %s\n", t)
|
||||
}
|
||||
for _, v := range b.ConstraintViolations {
|
||||
fmt.Printf(" constraint violations: %s (%d)\n", v.Table, v.Count)
|
||||
}
|
||||
fmt.Println("\nThese are not row conflicts, so bd conflicts resolve cannot settle them.")
|
||||
if len(b.SchemaConflictTables) > 0 {
|
||||
fmt.Println(" Schema conflicts: dolt conflicts resolve --ours|--theirs <table>")
|
||||
}
|
||||
if len(b.ConstraintViolations) > 0 {
|
||||
fmt.Println(" Constraint violations: inspect dolt_constraint_violations_<table>, delete the offending rows")
|
||||
}
|
||||
fmt.Println("Then conclude the merge with: bd conflicts resolve --conclude")
|
||||
}
|
||||
|
||||
// totalConflicts counts every live conflicted row, the gate on committing the
|
||||
// merge: dolt refuses a commit while any conflict is live.
|
||||
func totalConflicts(ctx context.Context) (int, error) {
|
||||
|
|
@ -516,6 +657,7 @@ func init() {
|
|||
conflictsResolveCmd.Flags().BoolVar(&conflictsResolveAll, "all", false, "Resolve whole tables instead of named issues")
|
||||
conflictsResolveCmd.Flags().StringVar(&conflictsResolveTable, "table", "", "Table to resolve (default: issues)")
|
||||
conflictsResolveCmd.Flags().BoolVar(&conflictsNoCommit, "no-commit", false, "Resolve without committing the merge")
|
||||
conflictsResolveCmd.Flags().BoolVar(&conflictsConclude, "conclude", false, "Commit a merge whose conflicts are already resolved")
|
||||
|
||||
conflictsCmd.AddCommand(conflictsListCmd)
|
||||
conflictsCmd.AddCommand(conflictsShowCmd)
|
||||
|
|
|
|||
302
internal/storage/dolt/conflicts_integration_test.go
Normal file
302
internal/storage/dolt/conflicts_integration_test.go
Normal file
|
|
@ -0,0 +1,302 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// Dolt-backed coverage for the `bd conflicts` surface (wy-36ilm F9). Every
|
||||
// test the feature shipped with was a pure unit test over synthetic column
|
||||
// maps — which is exactly why its review BLOCKER (the store decorator never
|
||||
// satisfied ConflictInspector, so `bd conflicts show` printed "No merge
|
||||
// conflicts" over a wedged merge) was invisible to the suite. These drive a
|
||||
// REAL two-branch conflict through GetConflictRows / ResolveConflictRows /
|
||||
// GetMergeBlockers / CommitMergeResolution, plus the decorator chain the CLI
|
||||
// actually holds.
|
||||
|
||||
// conflictTS is one timestamp for base, ours and theirs, so title is the only
|
||||
// column that diverges and a field assertion reads unambiguously.
|
||||
const conflictTS = "2026-07-10 11:00:00"
|
||||
|
||||
// wedgeIssueConflict leaves a real, UNRESOLVED issues conflict in the working
|
||||
// set — the state a halted CLI-level pull hands `bd conflicts`. It merges
|
||||
// without the auto-resolver on purpose: the operator surface exists for what
|
||||
// the auto path declined.
|
||||
func wedgeIssueConflict(t *testing.T, issueID string) (*DoltStore, context.Context) {
|
||||
t.Helper()
|
||||
store, peerBranch := setupIssueMergeConflict(t, issueID,
|
||||
"seed", conflictTS, "ours", conflictTS, "theirs", conflictTS, true)
|
||||
|
||||
ctx, cancel := testContext(t)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
// A session holding live conflicts cannot autocommit without dolt's
|
||||
// conflict-tolerance flags — the same pair ResolveConflictRows sets.
|
||||
// MaxOpenConns is 1 for these stores, so the session state persists.
|
||||
for _, stmt := range []string{
|
||||
"SET @@dolt_allow_commit_conflicts = 1",
|
||||
"SET @@dolt_force_transaction_commit = 1",
|
||||
} {
|
||||
if _, err := store.db.ExecContext(ctx, stmt); err != nil {
|
||||
t.Fatalf("%s: %v", stmt, err)
|
||||
}
|
||||
}
|
||||
if _, err := store.db.ExecContext(ctx, "CALL DOLT_MERGE(?)", peerBranch); err != nil {
|
||||
// DOLT_MERGE reports the halt through its error on some versions;
|
||||
// the conflict assertion below is the real check.
|
||||
t.Logf("DOLT_MERGE returned: %v", err)
|
||||
}
|
||||
|
||||
rows, err := store.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows: %v", err)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("expected exactly 1 live issues conflict after the merge, got %d", len(rows))
|
||||
}
|
||||
return store, ctx
|
||||
}
|
||||
|
||||
// issueTitle reads the working-set title of an issue.
|
||||
func issueTitle(t *testing.T, ctx context.Context, store *DoltStore, issueID string) string {
|
||||
t.Helper()
|
||||
var title string
|
||||
if err := store.db.QueryRowContext(ctx, "SELECT title FROM issues WHERE id = ?", issueID).Scan(&title); err != nil {
|
||||
t.Fatalf("read title of %s: %v", issueID, err)
|
||||
}
|
||||
return title
|
||||
}
|
||||
|
||||
// TestConflictsListAndShowSeeALiveMergeConflict is the `bd conflicts
|
||||
// list`/`show` path end to end: the tables dolt reports, and the per-field
|
||||
// base/ours/theirs presentation of the conflicted row.
|
||||
func TestConflictsListAndShowSeeALiveMergeConflict(t *testing.T) {
|
||||
const issueID = "conflict-list-1"
|
||||
store, ctx := wedgeIssueConflict(t, issueID)
|
||||
|
||||
// `bd conflicts list` counts through GetConflicts.
|
||||
tables, err := store.GetConflicts(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflicts: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, c := range tables {
|
||||
if c.Field == "issues" {
|
||||
found = true
|
||||
if c.Count < 1 {
|
||||
t.Errorf("issues conflict count = %d, want >= 1", c.Count)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("GetConflicts did not report the issues table: %+v", tables)
|
||||
}
|
||||
|
||||
// `bd conflicts show` reads the rows per field.
|
||||
rows, err := store.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows: %v", err)
|
||||
}
|
||||
row := rows[0]
|
||||
if row.Key != issueID {
|
||||
t.Errorf("conflict row key = %q, want %q", row.Key, issueID)
|
||||
}
|
||||
if !row.BaseExists || !row.OurExists || !row.TheirExists {
|
||||
t.Errorf("modify/modify conflict should exist on all three sides, got base=%v ours=%v theirs=%v",
|
||||
row.BaseExists, row.OurExists, row.TheirExists)
|
||||
}
|
||||
var title *storage.ConflictFieldValue
|
||||
for i := range row.Fields {
|
||||
if row.Fields[i].Name == "title" {
|
||||
title = &row.Fields[i]
|
||||
}
|
||||
// The conflict must be presented as fields, never as dolt's raw
|
||||
// base_/our_/their_ column soup.
|
||||
if strings.HasPrefix(row.Fields[i].Name, "our_") || strings.HasPrefix(row.Fields[i].Name, "their_") {
|
||||
t.Errorf("field %q still carries a dolt side prefix", row.Fields[i].Name)
|
||||
}
|
||||
}
|
||||
if title == nil {
|
||||
t.Fatalf("no title field in the conflict row: %+v", row.Fields)
|
||||
}
|
||||
if title.Base == nil || *title.Base != "seed" {
|
||||
t.Errorf("title base = %v, want \"seed\"", title.Base)
|
||||
}
|
||||
if title.Ours == nil || *title.Ours != "ours" {
|
||||
t.Errorf("title ours = %v, want \"ours\"", title.Ours)
|
||||
}
|
||||
if title.Theirs == nil || *title.Theirs != "theirs" {
|
||||
t.Errorf("title theirs = %v, want \"theirs\"", title.Theirs)
|
||||
}
|
||||
if !title.Differs() {
|
||||
t.Error("title should be reported as a diverged field")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConflictsResolveOursCommitsTheMerge drives `bd conflicts resolve <id>
|
||||
// --ours` against a real conflict: our value survives, the conflict clears,
|
||||
// and the merge concludes through CommitMergeResolution.
|
||||
func TestConflictsResolveOursCommitsTheMerge(t *testing.T) {
|
||||
const issueID = "conflict-ours-1"
|
||||
store, ctx := wedgeIssueConflict(t, issueID)
|
||||
|
||||
// The merge is open and blocked on the row conflict, not on schema
|
||||
// conflicts or constraint violations (wy-36ilm F12).
|
||||
blockers, err := store.GetMergeBlockers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMergeBlockers: %v", err)
|
||||
}
|
||||
if !blockers.Merging {
|
||||
t.Error("GetMergeBlockers reported no merge in progress while a merge is wedged")
|
||||
}
|
||||
if blockers.Blocked() {
|
||||
t.Errorf("unexpected non-row merge blockers: %+v", blockers)
|
||||
}
|
||||
|
||||
n, err := store.ResolveConflictRows(ctx, "issues", []string{issueID}, "ours")
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveConflictRows --ours: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("resolved %d rows, want 1", n)
|
||||
}
|
||||
|
||||
rows, err := store.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows after resolve: %v", err)
|
||||
}
|
||||
if len(rows) != 0 {
|
||||
t.Fatalf("expected no live conflicts after resolve, got %d", len(rows))
|
||||
}
|
||||
if got := issueTitle(t, ctx, store, issueID); got != "ours" {
|
||||
t.Errorf("title after --ours = %q, want %q", got, "ours")
|
||||
}
|
||||
|
||||
if err := store.CommitMergeResolution(ctx, "Resolve 1 merge conflict(s) using ours strategy"); err != nil {
|
||||
t.Fatalf("CommitMergeResolution: %v", err)
|
||||
}
|
||||
after, err := store.GetConflicts(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflicts after commit: %v", err)
|
||||
}
|
||||
if len(after) != 0 {
|
||||
t.Errorf("conflicts still reported after the merge commit: %+v", after)
|
||||
}
|
||||
blockers, err = store.GetMergeBlockers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMergeBlockers after commit: %v", err)
|
||||
}
|
||||
if blockers.Merging {
|
||||
t.Error("merge still reported as in progress after CommitMergeResolution")
|
||||
}
|
||||
if got := issueTitle(t, ctx, store, issueID); got != "ours" {
|
||||
t.Errorf("title after the merge commit = %q, want %q", got, "ours")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConflictsResolveTheirsTakesTheirValue is the --theirs half: their values
|
||||
// are written over ours before the conflict row is cleared.
|
||||
func TestConflictsResolveTheirsTakesTheirValue(t *testing.T) {
|
||||
const issueID = "conflict-theirs-1"
|
||||
store, ctx := wedgeIssueConflict(t, issueID)
|
||||
|
||||
n, err := store.ResolveConflictRows(ctx, "issues", []string{issueID}, "theirs")
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveConflictRows --theirs: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("resolved %d rows, want 1", n)
|
||||
}
|
||||
if got := issueTitle(t, ctx, store, issueID); got != "theirs" {
|
||||
t.Errorf("title after --theirs = %q, want %q", got, "theirs")
|
||||
}
|
||||
rows, err := store.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows after resolve: %v", err)
|
||||
}
|
||||
if len(rows) != 0 {
|
||||
t.Fatalf("expected no live conflicts after resolve, got %d", len(rows))
|
||||
}
|
||||
if err := store.CommitMergeResolution(ctx, "Resolve 1 merge conflict(s) using theirs strategy"); err != nil {
|
||||
t.Fatalf("CommitMergeResolution: %v", err)
|
||||
}
|
||||
if got := issueTitle(t, ctx, store, issueID); got != "theirs" {
|
||||
t.Errorf("title after the merge commit = %q, want %q", got, "theirs")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConflictsResolveRefusesAnUnknownIssue proves a miskeyed resolve fails
|
||||
// loudly instead of silently clearing someone else's conflict.
|
||||
func TestConflictsResolveRefusesAnUnknownIssue(t *testing.T) {
|
||||
const issueID = "conflict-unknown-1"
|
||||
store, ctx := wedgeIssueConflict(t, issueID)
|
||||
|
||||
if _, err := store.ResolveConflictRows(ctx, "issues", []string{"no-such-issue"}, "ours"); err == nil {
|
||||
t.Fatal("resolving an unconflicted ID should fail")
|
||||
} else if !strings.Contains(err.Error(), "no live conflict") {
|
||||
t.Errorf("unexpected error for an unconflicted ID: %v", err)
|
||||
}
|
||||
rows, err := store.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows: %v", err)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Errorf("the real conflict should survive a failed resolve, got %d rows", len(rows))
|
||||
}
|
||||
}
|
||||
|
||||
// TestConflictInspectorSurvivesTheStoreDecoratorChain is the regression test
|
||||
// for the wy-jpd3.5 review BLOCKER: the CLI never holds the concrete store,
|
||||
// it holds a decorator that embeds the DoltStorage interface and so promotes
|
||||
// only that interface's methods. Asserting ConflictInspector on the wrapper
|
||||
// is always false — which made `bd conflicts` report "No merge conflicts"
|
||||
// over a wedged merge. storage.UnwrapStore is the fix, and this test fails if
|
||||
// either half of it regresses.
|
||||
func TestConflictInspectorSurvivesTheStoreDecoratorChain(t *testing.T) {
|
||||
const issueID = "conflict-decorator-1"
|
||||
store, ctx := wedgeIssueConflict(t, issueID)
|
||||
|
||||
// Two decorators deep, exactly as cmd/bd's chain can be.
|
||||
var decorated storage.DoltStorage = storage.NewHookFiringStore(store, nil)
|
||||
decorated = storage.NewHookFiringStore(decorated, nil)
|
||||
|
||||
if _, ok := decorated.(storage.ConflictInspector); ok {
|
||||
t.Error("the decorator itself satisfies ConflictInspector — the UnwrapStore rule this test guards would be moot, and the naked assertion is no longer the trap it was")
|
||||
}
|
||||
|
||||
inspector, ok := storage.UnwrapStore(decorated).(storage.ConflictInspector)
|
||||
if !ok {
|
||||
t.Fatal("UnwrapStore(decorated) does not satisfy ConflictInspector: bd conflicts would report 'No merge conflicts' over a wedged merge (the wy-jpd3.5 review blocker)")
|
||||
}
|
||||
rows, err := inspector.GetConflictRows(ctx, "issues")
|
||||
if err != nil {
|
||||
t.Fatalf("GetConflictRows through the decorator chain: %v", err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].Key != issueID {
|
||||
t.Fatalf("conflict invisible through the decorator chain: %+v", rows)
|
||||
}
|
||||
|
||||
blockers, ok := storage.UnwrapStore(decorated).(storage.MergeBlockerInspector)
|
||||
if !ok {
|
||||
t.Fatal("UnwrapStore(decorated) does not satisfy MergeBlockerInspector: schema conflicts and constraint violations would be invisible to bd conflicts")
|
||||
}
|
||||
b, err := blockers.GetMergeBlockers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMergeBlockers through the decorator chain: %v", err)
|
||||
}
|
||||
if !b.Merging {
|
||||
t.Error("merge status invisible through the decorator chain")
|
||||
}
|
||||
|
||||
// And the resolution path reaches the concrete store too.
|
||||
n, err := inspector.ResolveConflictRows(ctx, "issues", []string{issueID}, "ours")
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveConflictRows through the decorator chain: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Errorf("resolved %d rows through the decorator chain, want 1", n)
|
||||
}
|
||||
}
|
||||
|
|
@ -2232,6 +2232,16 @@ func (s *DoltStore) commitWorkingSet(ctx context.Context, message string, mode c
|
|||
}
|
||||
|
||||
if len(tables) == 0 {
|
||||
// A merge resolution with a clean working set is NOT a no-op: it is
|
||||
// the `--ours` case, where our values already stood and resolving the
|
||||
// conflict dirtied nothing. Returning here left is_merging true while
|
||||
// the caller reported "Merge committed", and the next pull re-wedged
|
||||
// on the unconcluded merge (wy-36ilm, caught by the F9 integration
|
||||
// test). Only the merge-conclusion mode takes this path: for the
|
||||
// other modes an empty working set really is nothing to commit.
|
||||
if mode == configIncludeAll {
|
||||
return s.concludeOpenMerge(ctx, conn, message)
|
||||
}
|
||||
return nil // Nothing to commit (all changes were config-only or dolt_ignore'd)
|
||||
}
|
||||
|
||||
|
|
@ -2261,6 +2271,31 @@ func (s *DoltStore) commitWorkingSet(ctx context.Context, message string, mode c
|
|||
return nil
|
||||
}
|
||||
|
||||
// concludeOpenMerge commits an open merge whose resolution left the working
|
||||
// set clean, so the merge is actually concluded rather than left open with
|
||||
// nothing to show for it. It is a no-op when no merge is in progress, and it
|
||||
// runs on the CALLER'S pinned connection because dolt's merge state is
|
||||
// session state. isDoltNothingToCommit still absorbs the race where the merge
|
||||
// closed between the status read and the commit.
|
||||
func (s *DoltStore) concludeOpenMerge(ctx context.Context, conn *sql.Conn, message string) error {
|
||||
var merging bool
|
||||
if err := conn.QueryRowContext(ctx, "SELECT is_merging FROM dolt_merge_status").Scan(&merging); err != nil {
|
||||
// No merge status to read is no evidence of a merge — keep the old
|
||||
// "nothing to commit" behavior rather than failing a resolution.
|
||||
return nil //nolint:nilerr // diagnosis only; never a gate
|
||||
}
|
||||
if !merging {
|
||||
return nil
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_COMMIT('-m', ?, '--author', ?)", message, s.commitAuthorString()); err != nil {
|
||||
if isDoltNothingToCommit(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to conclude merge: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// assertDirtyConfigUserKVOnly returns an error unless every config row dirty in
|
||||
// the working set is this clone's own user KV data (the kv.* namespace, which
|
||||
// includes kv.memory.* memories). The pre-pull auto-commit opts config into the
|
||||
|
|
|
|||
|
|
@ -109,6 +109,15 @@ func (s *DoltStore) GetConflictRows(ctx context.Context, table string) ([]storag
|
|||
return versioncontrolops.GetConflictRows(ctx, s.db, table)
|
||||
}
|
||||
|
||||
// The CLI reaches this through storage.UnwrapStore too.
|
||||
var _ storage.MergeBlockerInspector = (*DoltStore)(nil)
|
||||
|
||||
// GetMergeBlockers reports schema conflicts, constraint violations, and
|
||||
// whether a merge is open. Implements storage.MergeBlockerInspector.
|
||||
func (s *DoltStore) GetMergeBlockers(ctx context.Context) (storage.MergeBlockers, error) {
|
||||
return versioncontrolops.GetMergeBlockers(ctx, s.db)
|
||||
}
|
||||
|
||||
// ResolveConflictRows resolves individual conflicted rows of table by key.
|
||||
// Implements storage.ConflictInspector (backs `bd conflicts resolve <id>`).
|
||||
//
|
||||
|
|
|
|||
|
|
@ -317,6 +317,21 @@ func (s *EmbeddedDoltStore) GetConflictRows(ctx context.Context, table string) (
|
|||
return rows, err
|
||||
}
|
||||
|
||||
// The CLI reaches this through storage.UnwrapStore too.
|
||||
var _ storage.MergeBlockerInspector = (*EmbeddedDoltStore)(nil)
|
||||
|
||||
// GetMergeBlockers reports schema conflicts, constraint violations, and
|
||||
// whether a merge is open. Implements storage.MergeBlockerInspector.
|
||||
func (s *EmbeddedDoltStore) GetMergeBlockers(ctx context.Context) (storage.MergeBlockers, error) {
|
||||
var blockers storage.MergeBlockers
|
||||
err := s.withDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
var err error
|
||||
blockers, err = versioncontrolops.GetMergeBlockers(ctx, db)
|
||||
return err
|
||||
})
|
||||
return blockers, err
|
||||
}
|
||||
|
||||
// ResolveConflictRows resolves individual conflicted rows of table by key.
|
||||
// Implements storage.ConflictInspector (backs `bd conflicts resolve <id>`).
|
||||
// It runs on a PINNED connection: the resolution sets dolt's
|
||||
|
|
|
|||
|
|
@ -77,6 +77,41 @@ type ConflictInspector interface {
|
|||
ResolveConflictRows(ctx context.Context, table string, keys []string, strategy string) (int, error)
|
||||
}
|
||||
|
||||
// ConstraintViolation is one table's outstanding post-merge constraint
|
||||
// violations (dolt_constraint_violations).
|
||||
type ConstraintViolation struct {
|
||||
Table string `json:"table"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// MergeBlockers is the merge state that blocks concluding a merge but never
|
||||
// appears in dolt_conflicts: a schema conflict, or a constraint violation the
|
||||
// auto-repair path (mergesettle.go) declined. Both can be outstanding while
|
||||
// every ROW conflict is resolved, which used to make the commit fail with a
|
||||
// raw dolt error and no guidance (wy-36ilm F12).
|
||||
type MergeBlockers struct {
|
||||
// Merging reports dolt_merge_status.is_merging: a merge is open and
|
||||
// awaiting its commit.
|
||||
Merging bool `json:"merging"`
|
||||
// SchemaConflictTables are the tables in dolt_schema_conflicts.
|
||||
SchemaConflictTables []string `json:"schema_conflict_tables,omitempty"`
|
||||
// ConstraintViolations are the tables with outstanding violations.
|
||||
ConstraintViolations []ConstraintViolation `json:"constraint_violations,omitempty"`
|
||||
}
|
||||
|
||||
// Blocked reports whether anything here would refuse a merge commit.
|
||||
func (b MergeBlockers) Blocked() bool {
|
||||
return len(b.SchemaConflictTables) > 0 || len(b.ConstraintViolations) > 0
|
||||
}
|
||||
|
||||
// MergeBlockerInspector is the optional companion to ConflictInspector: the
|
||||
// non-row merge state (schema conflicts, constraint violations, whether a
|
||||
// merge is even open). Implemented by the Dolt-backed stores only; callers
|
||||
// must type-assert, and a backend without it simply reports nothing.
|
||||
type MergeBlockerInspector interface {
|
||||
GetMergeBlockers(ctx context.Context) (MergeBlockers, error)
|
||||
}
|
||||
|
||||
// VersionControl provides branch, commit, merge, and status operations.
|
||||
type VersionControl interface {
|
||||
Branch(ctx context.Context, name string) error
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package versioncontrolops
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
|
@ -396,3 +397,119 @@ func resolveOneConflictRow(ctx context.Context, db DBConn, table, keyCol, key, s
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMergeBlockers reports the merge state that `bd conflicts` cannot show as
|
||||
// rows: whether a merge is open at all, plus the schema conflicts and
|
||||
// constraint violations that make dolt refuse the merge commit even when
|
||||
// every dolt_conflicts row is resolved (wy-36ilm F12). Without it, that state
|
||||
// surfaced only as a raw dolt error from CommitMergeResolution, after the
|
||||
// operator had been told "0 conflicts remain".
|
||||
//
|
||||
// Each source is read independently and a MISSING source table is not an
|
||||
// error: dolt_schema_conflicts and dolt_constraint_violations are dolt system
|
||||
// tables whose presence has varied across versions, and a diagnosis helper
|
||||
// must never be the thing that fails the command.
|
||||
func GetMergeBlockers(ctx context.Context, db DBConn) (storage.MergeBlockers, error) {
|
||||
var out storage.MergeBlockers
|
||||
var errs []error
|
||||
|
||||
if merging, err := mergeInProgress(ctx, db); err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
out.Merging = merging
|
||||
}
|
||||
if tables, err := schemaConflictTables(ctx, db); err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
out.SchemaConflictTables = tables
|
||||
}
|
||||
if violations, err := constraintViolationCounts(ctx, db); err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
out.ConstraintViolations = violations
|
||||
}
|
||||
// Partial results are still useful — the caller gets what was readable
|
||||
// plus the reason the rest was not.
|
||||
return out, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// mergeInProgress reads dolt_merge_status.is_merging: true between a halted
|
||||
// DOLT_MERGE and its concluding commit. It is what lets `--conclude` tell
|
||||
// "resolved but uncommitted" apart from "nothing to conclude".
|
||||
func mergeInProgress(ctx context.Context, db DBConn) (bool, error) {
|
||||
var merging bool
|
||||
err := db.QueryRowContext(ctx, "SELECT is_merging FROM dolt_merge_status").Scan(&merging)
|
||||
if err != nil {
|
||||
if isMissingSystemTable(err) || errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("query merge status: %w", err)
|
||||
}
|
||||
return merging, nil
|
||||
}
|
||||
|
||||
// schemaConflictTables lists the tables whose SCHEMAS conflict — dolt keeps
|
||||
// them out of dolt_conflicts entirely, so totalConflicts cannot see them.
|
||||
func schemaConflictTables(ctx context.Context, db DBConn) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT table_name FROM dolt_schema_conflicts")
|
||||
if err != nil {
|
||||
if isMissingSystemTable(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("query schema conflicts: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var t string
|
||||
if err := rows.Scan(&t); err != nil {
|
||||
return nil, fmt.Errorf("scan schema conflict: %w", err)
|
||||
}
|
||||
tables = append(tables, t)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate schema conflicts: %w", err)
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
// constraintViolationCounts lists the tables carrying outstanding constraint
|
||||
// violations. mergesettle.go repairs the FK-cascade class on the auto path;
|
||||
// anything it declined lands here, blocking the commit.
|
||||
func constraintViolationCounts(ctx context.Context, db DBConn) ([]storage.ConstraintViolation, error) {
|
||||
rows, err := db.QueryContext(ctx,
|
||||
"SELECT `table`, num_violations FROM dolt_constraint_violations WHERE num_violations > 0")
|
||||
if err != nil {
|
||||
if isMissingSystemTable(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("query constraint violations: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var out []storage.ConstraintViolation
|
||||
for rows.Next() {
|
||||
var v storage.ConstraintViolation
|
||||
if err := rows.Scan(&v.Table, &v.Count); err != nil {
|
||||
return nil, fmt.Errorf("scan constraint violation: %w", err)
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate constraint violations: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// isMissingSystemTable reports whether err is dolt/MySQL's "table does not
|
||||
// exist". Matched on the message because the embedded engine and the MySQL
|
||||
// driver report it as different error types.
|
||||
func isMissingSystemTable(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "table not found") ||
|
||||
strings.Contains(msg, "doesn't exist") ||
|
||||
strings.Contains(msg, "does not exist") ||
|
||||
strings.Contains(msg, "unknown table")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue