fix(sync): gate the merge-abort hard reset on pre-merge cleanliness (bd-578h9.2)
abortMerge fell back to DOLT_RESET('--hard') whenever DOLT_MERGE('--abort')
failed, on the assumption that a merge can only run against a clean working
set so nothing uncommitted could be lost. That assumption is backwards for
the most common --abort failure: a merge that REFUSED TO START because the
working set was dirty ("local changes would be stomped by merge"). No merge
is active, --abort fails, and the hard reset silently destroyed uncommitted
rows the merge never touched - while the pull still returned the merge
error, so a retry would succeed and the loss was invisible.
MergeAndSettle now captures dolt_status cleanliness before the merge runs
and abortMerge only hard-resets when the pre-merge working set was clean
(the case the fallback was designed for: a force-committed violation-only
merge that closed merge state). Errors reading dolt_status count as dirty.
Also close the window that produces dirty pre-pull working sets in the
first place: embedded Pull and PullRemote now CommitPending before pulling,
matching PullFrom and server-mode pullFromRemote (GH#2474). Crash leftovers
are committed instead of colliding with the merge.
While here: the FK-cascade settle test's comments seed lost its DB-side id
default to migration 0051 (bd-2rd37) and was failing on main; it now
supplies an explicit id like the app does (bd-6dnrw.18).
New regression test pins the dirty-refusal scenario end to end against the
embedded engine.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
262b0afd5e
commit
d1a2fc75af
3 changed files with 115 additions and 16 deletions
|
|
@ -64,9 +64,11 @@ var embeddedFKCascadeCases = []embeddedFKCascadeCase{
|
|||
orphanQuery: "SELECT COUNT(*) FROM labels WHERE issue_id = 'fkc-x-%[1]s'",
|
||||
},
|
||||
{
|
||||
// comments.id lost its DB-side default in 0051 (bd-2rd37); inserts
|
||||
// must supply an explicit id like the app does (bd-6dnrw.18).
|
||||
name: "comments",
|
||||
insert: "INSERT INTO comments (issue_id, author, text, created_at) " +
|
||||
"VALUES ('fkc-x-%[1]s', 'peer', 'late comment', NOW())",
|
||||
insert: "INSERT INTO comments (id, issue_id, author, text, created_at) " +
|
||||
"VALUES (UUID(), 'fkc-x-%[1]s', 'peer', 'late comment', NOW())",
|
||||
orphanQuery: "SELECT COUNT(*) FROM comments WHERE issue_id = 'fkc-x-%[1]s'",
|
||||
},
|
||||
}
|
||||
|
|
@ -262,3 +264,71 @@ func TestEmbeddedMergeAndSettleMetadataConflict(t *testing.T) {
|
|||
t.Errorf("expected metadata value 'bbb' (theirs), got %q", value)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmbeddedMergeAndSettleDirtyWorkingSetSurvivesRefusedMerge pins the
|
||||
// bd-578h9.2 fix: when DOLT_MERGE refuses to start because the working set
|
||||
// is dirty, no merge state exists, so DOLT_MERGE('--abort') fails too — and
|
||||
// the old abortMerge fell back to DOLT_RESET('--hard'), silently destroying
|
||||
// the uncommitted rows the merge never touched. The hard reset is now gated
|
||||
// on the working set having been clean before the merge ran.
|
||||
func TestEmbeddedMergeAndSettleDirtyWorkingSetSurvivesRefusedMerge(t *testing.T) {
|
||||
te := newTestEnv(t, "dirtysettle")
|
||||
ctx := t.Context()
|
||||
conn := openSettleConn(t, ctx, te)
|
||||
|
||||
// Seed and commit a baseline, then diverge a peer branch so the merge
|
||||
// has real work to do (a fast-forward can succeed on a dirty set).
|
||||
if _, err := conn.ExecContext(ctx,
|
||||
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES ('dirty-base', 'base', '', '', '', '', 'open', 2, 'task')"); err != nil {
|
||||
t.Fatalf("seed base issue: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'seed base')"); err != nil {
|
||||
t.Fatalf("commit seed: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_BRANCH('dirtypeer', 'HEAD')"); err != nil {
|
||||
t.Fatalf("create peer branch: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT('dirtypeer')"); err != nil {
|
||||
t.Fatalf("checkout peer: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx,
|
||||
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES ('dirty-peer', 'peer', '', '', '', '', 'open', 2, 'task')"); err != nil {
|
||||
t.Fatalf("seed peer issue: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'peer work')"); err != nil {
|
||||
t.Fatalf("commit peer: %v", err)
|
||||
}
|
||||
// Diverge main too so the merge is a true three-way merge.
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT('main')"); err != nil {
|
||||
t.Fatalf("checkout main: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx,
|
||||
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES ('dirty-main', 'main', '', '', '', '', 'open', 2, 'task')"); err != nil {
|
||||
t.Fatalf("seed main issue: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'main work')"); err != nil {
|
||||
t.Fatalf("commit main: %v", err)
|
||||
}
|
||||
|
||||
// The user's uncommitted work: present in the working set, never committed.
|
||||
if _, err := conn.ExecContext(ctx,
|
||||
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES ('dirty-uncommitted', 'precious', '', '', '', '', 'open', 2, 'task')"); err != nil {
|
||||
t.Fatalf("insert uncommitted issue: %v", err)
|
||||
}
|
||||
|
||||
mergeErr := versioncontrolops.MergeAndSettle(ctx, conn, "dirtypeer")
|
||||
if mergeErr == nil {
|
||||
t.Logf("note: this Dolt version merged over a dirty working set without refusing")
|
||||
} else {
|
||||
t.Logf("merge refused as expected: %v", mergeErr)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := conn.QueryRowContext(ctx,
|
||||
"SELECT COUNT(*) FROM issues WHERE id = 'dirty-uncommitted'").Scan(&count); err != nil {
|
||||
t.Fatalf("count uncommitted issue: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("uncommitted working-set row was destroyed by the merge abort path (count=%d)", count)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -254,6 +254,12 @@ func (s *EmbeddedDoltStore) Push(ctx context.Context) error {
|
|||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) Pull(ctx context.Context) error {
|
||||
// GH#2474 / bd-578h9.2: auto-commit pending changes before pull, matching
|
||||
// server-mode pullFromRemote and PullFrom. Leftovers from a crashed
|
||||
// command would otherwise make the merge refuse to start.
|
||||
if _, err := s.CommitPending(ctx, "beads"); err != nil {
|
||||
return fmt.Errorf("commit pending before pull: %w", err)
|
||||
}
|
||||
preHead := s.preMergeHead(ctx)
|
||||
err := s.withPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Pull(ctx, db, defaultRemote, s.branch, remoteAuthUser())
|
||||
|
|
@ -280,6 +286,10 @@ func (s *EmbeddedDoltStore) PushRemote(ctx context.Context, remote string, force
|
|||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) PullRemote(ctx context.Context, remote string) error {
|
||||
// GH#2474 / bd-578h9.2: see Pull.
|
||||
if _, err := s.CommitPending(ctx, "beads"); err != nil {
|
||||
return fmt.Errorf("commit pending before pull: %w", err)
|
||||
}
|
||||
preHead := s.preMergeHead(ctx)
|
||||
err := s.withPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Pull(ctx, db, remote, s.branch, remoteAuthUser())
|
||||
|
|
|
|||
|
|
@ -33,6 +33,11 @@ import (
|
|||
// rolling back, so the settle pass can repair it; SettleMerge's gates ensure
|
||||
// nothing unrepaired survives without an error.
|
||||
func MergeAndSettle(ctx context.Context, db DBConn, ref string) error {
|
||||
// Capture pre-merge cleanliness before anything runs: abortMerge's
|
||||
// hard-reset fallback is only safe when nothing uncommitted predates
|
||||
// the merge (bd-578h9.2).
|
||||
preMergeClean := workingSetClean(ctx, db)
|
||||
|
||||
if _, err := db.ExecContext(ctx, "SET @@dolt_allow_commit_conflicts = 1"); err != nil {
|
||||
return fmt.Errorf("set dolt_allow_commit_conflicts: %w", err)
|
||||
}
|
||||
|
|
@ -45,7 +50,7 @@ func MergeAndSettle(ctx context.Context, db DBConn, ref string) error {
|
|||
// DOLT_PULL swallows "Already up to date." internally; we do the same.
|
||||
mergeErr = nil
|
||||
}
|
||||
return SettleMerge(ctx, db, mergeErr)
|
||||
return SettleMerge(ctx, db, mergeErr, preMergeClean)
|
||||
}
|
||||
|
||||
// SettleMerge finishes a merge that ran on db with the session flags
|
||||
|
|
@ -53,16 +58,17 @@ func MergeAndSettle(ctx context.Context, db DBConn, ref string) error {
|
|||
// cascade violations (bd-6dnrw.4), and leaves the settled working set in
|
||||
// place — or aborts the merge when anything needs the operator. mergeErr is
|
||||
// the merge statement's own error; it is surfaced whenever nothing was
|
||||
// resolved or repaired. The decision logic mirrors server-mode
|
||||
// settleMergeInTx exactly; the abort stands in for that path's transaction
|
||||
// rollback, restoring the pre-merge working set (which the merge required to
|
||||
// be clean) so a retry is possible.
|
||||
func SettleMerge(ctx context.Context, db DBConn, mergeErr error) error {
|
||||
// resolved or repaired. preMergeClean reports whether the working set was
|
||||
// clean before the merge ran; it gates abortMerge's hard-reset fallback.
|
||||
// The decision logic mirrors server-mode settleMergeInTx exactly; the abort
|
||||
// stands in for that path's transaction rollback, restoring the pre-merge
|
||||
// working set so a retry is possible.
|
||||
func SettleMerge(ctx context.Context, db DBConn, mergeErr error, preMergeClean bool) error {
|
||||
// Check for merge conflicts regardless of whether the merge errored.
|
||||
// Some Dolt versions error on conflicts, others leave them in the working set.
|
||||
resolved, resolveErr := TryAutoResolveMergeConflicts(ctx, db)
|
||||
if resolveErr != nil {
|
||||
abortMerge(ctx, db)
|
||||
abortMerge(ctx, db, preMergeClean)
|
||||
if mergeErr != nil {
|
||||
return mergeErr
|
||||
}
|
||||
|
|
@ -75,14 +81,14 @@ func SettleMerge(ctx context.Context, db DBConn, mergeErr error) error {
|
|||
// autocommits, so the abort below is what keeps them out of the database.
|
||||
repairedViol, hadViol, violErr := TryRepairFKCascadeViolations(ctx, db)
|
||||
if violErr != nil {
|
||||
abortMerge(ctx, db)
|
||||
abortMerge(ctx, db, preMergeClean)
|
||||
if mergeErr != nil {
|
||||
return mergeErr
|
||||
}
|
||||
return violErr
|
||||
}
|
||||
if hadViol && !repairedViol {
|
||||
abortMerge(ctx, db)
|
||||
abortMerge(ctx, db, preMergeClean)
|
||||
if mergeErr != nil {
|
||||
return mergeErr
|
||||
}
|
||||
|
|
@ -91,7 +97,7 @@ func SettleMerge(ctx context.Context, db DBConn, mergeErr error) error {
|
|||
|
||||
if mergeErr != nil && !resolved && !repairedViol {
|
||||
// Merge failed for a non-conflict reason, or conflicts include non-metadata tables.
|
||||
abortMerge(ctx, db)
|
||||
abortMerge(ctx, db, preMergeClean)
|
||||
return mergeErr
|
||||
}
|
||||
|
||||
|
|
@ -102,15 +108,28 @@ func SettleMerge(ctx context.Context, db DBConn, mergeErr error) error {
|
|||
// merge — the autocommit-mode stand-in for server mode's tx.Rollback().
|
||||
// DOLT_MERGE('--abort') is the precise tool but only works while merge state
|
||||
// is active; a force-committed violation-only merge may have closed it, so
|
||||
// fall back to a hard reset, which is equivalent here because DOLT_MERGE
|
||||
// refuses to start on a dirty working set (nothing uncommitted can be lost).
|
||||
// fall back to a hard reset — but only when the working set was clean before
|
||||
// the merge ran. The most common reason --abort fails is a merge that
|
||||
// REFUSED TO START on a dirty working set; hard-resetting there would
|
||||
// destroy uncommitted data the merge never touched (bd-578h9.2).
|
||||
// Best-effort: the caller's error is what matters.
|
||||
func abortMerge(ctx context.Context, db DBConn) {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_MERGE('--abort')"); err != nil {
|
||||
func abortMerge(ctx context.Context, db DBConn, preMergeClean bool) {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_MERGE('--abort')"); err != nil && preMergeClean {
|
||||
_, _ = db.ExecContext(ctx, "CALL DOLT_RESET('--hard')")
|
||||
}
|
||||
}
|
||||
|
||||
// workingSetClean reports whether dolt_status is empty. Errors count as
|
||||
// dirty so the hard-reset fallback stays conservative.
|
||||
func workingSetClean(ctx context.Context, db DBConn) bool {
|
||||
rows, err := db.QueryContext(ctx, "SELECT 1 FROM dolt_status LIMIT 1")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer rows.Close()
|
||||
return !rows.Next() && rows.Err() == nil
|
||||
}
|
||||
|
||||
// TryAutoResolveMergeConflicts auto-resolves merge conflicts that are safe to
|
||||
// resolve without operator input, and returns (true, nil) only if ALL conflicts
|
||||
// were resolved. It handles three classes:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue