fix(schema): probe schema_migrations before the HEAD hash read (bd-21dc1)

ReadMigrationContentHashes probes the cursor table and its content_hash
column before its AS OF read, but sent the HEAD read's SELECT without a
probe. On a database without schema_migrations, or with a pre-#4259 cursor
that lacks content_hash, that SELECT fails. A failing statement pins the
pooled Dolt session to its pre-statement catalog (be-bv7x). `bd doctor`
reaches the HEAD read on a database it never migrates once a sync remote
is configured, and the smart remote migrate gate reaches it too.

Give the HEAD arm the same two probes: tableInCurrentDatabase (the probe
currentVersion uses, which succeeds on any session), then showColumnExists.
A missing table or column returns the "table not found" / "unknown column"
errors MissingMigrationObjectErr already classifies, so the doctor and the
gate return the same verdicts as before.

The strict-order sqlmock tests of the HEAD read gain the probe
expectations.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Eddie the Engineer 2026-10-16 17:24:35 +00:00
commit d5e180ff05
6 changed files with 147 additions and 2 deletions

View file

@ -31,6 +31,18 @@ func expectRemoteMigrationShape(mock sqlmock.Sqlmock, ref string) {
WillReturnRows(sqlmock.NewRows([]string{"Field"}).AddRow("content_hash"))
}
// expectLocalMigrationShape mocks the probes ReadMigrationContentHashes issues
// before its HEAD read (be-bv7x): a usable database, the cursor table, then
// its content_hash column.
func expectLocalMigrationShape(mock sqlmock.Sqlmock) {
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.schemata WHERE schema_name = DATABASE\(\)`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery(`^SHOW TABLES$`).
WillReturnRows(sqlmock.NewRows([]string{"Tables_in_beads"}).AddRow("schema_migrations"))
mock.ExpectQuery(`SHOW COLUMNS FROM schema_migrations LIKE 'content_hash'`).
WillReturnRows(sqlmock.NewRows([]string{"Field"}).AddRow("content_hash"))
}
func TestCheckMigrationContentSkew_NoRemote(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
@ -64,6 +76,7 @@ func TestCheckMigrationContentSkew_UsesConfiguredRemote(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(1))
mock.ExpectQuery(`SELECT active_branch\(\)`).
WillReturnRows(sqlmock.NewRows([]string{"b"}).AddRow("main"))
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a"))
expectRemoteMigrationShape(mock, "remotes/upstream/main")
@ -86,6 +99,7 @@ func TestCheckMigrationContentSkew_NoCachedRemoteRef(t *testing.T) {
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a"))
// Real Dolt phrasing for an AS OF ref that is not cached locally.
@ -105,6 +119,7 @@ func TestCheckMigrationContentSkew_CachedRemotePredatesContentHashes(t *testing.
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a"))
mock.ExpectQuery(`SHOW TABLES AS OF 'remotes/origin/main' LIKE 'schema_migrations'`).
@ -134,6 +149,7 @@ func TestCheckMigrationContentSkew_UnexpectedErrorWarns(t *testing.T) {
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a"))
expectRemoteMigrationShape(mock, "remotes/origin/main")
@ -156,6 +172,7 @@ func TestCheckMigrationContentSkew_NoLocalHashes(t *testing.T) {
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
// Old database: rows exist but content_hash is all NULL.
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, nil))
@ -176,6 +193,7 @@ func TestCheckMigrationContentSkew_Matches(t *testing.T) {
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a").AddRow(2, "b"))
expectRemoteMigrationShape(mock, "remotes/origin/main")
@ -195,6 +213,7 @@ func TestCheckMigrationContentSkew_Diverges(t *testing.T) {
}
defer db.Close()
expectRemoteAndBranch(mock, "main")
expectLocalMigrationShape(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).AddRow(1, "a").AddRow(2, "b"))
expectRemoteMigrationShape(mock, "remotes/origin/main")

View file

@ -152,6 +152,7 @@ go_test(
"migration_0068_add_attribution_status_test.go",
"migration_0069_datetime_precision_test.go",
"migration_body_drain_test.go",
"migration_content_hashes_test.go",
"migration_freeze_test.go",
"migration_watchdog_test.go",
"progress_test.go",

View file

@ -44,6 +44,26 @@ func ReadMigrationContentHashes(ctx context.Context, db DBConn, ref string) (map
err error
)
if ref == "" {
// Probe the cursor's shape before selecting, as the AS OF arm below
// does. Here a failing SELECT costs more than a server warning: `bd
// doctor` reaches this read on a database it never migrates, and the
// statement pins the pooled Dolt session to its pre-statement catalog
// (be-bv7x). tableInCurrentDatabase is currentVersion's probe, which
// succeeds on any session.
hasTable, queryErr := tableInCurrentDatabase(ctx, db, "schema_migrations")
if queryErr != nil {
return nil, queryErr
}
if !hasTable {
return nil, fmt.Errorf("table not found: schema_migrations")
}
hasContentHash, queryErr := showColumnExists(ctx, db, "schema_migrations", "content_hash")
if queryErr != nil {
return nil, queryErr
}
if !hasContentHash {
return nil, fmt.Errorf("unknown column content_hash in schema_migrations")
}
rows, err = db.QueryContext(ctx, "SELECT version, content_hash FROM schema_migrations")
} else {
if verr := validateMigrationRef(ref); verr != nil {

View file

@ -0,0 +1,98 @@
package schema
import (
"context"
"maps"
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
// expectHeadContentHashProbes mocks the shape probes ReadMigrationContentHashes
// issues before its HEAD read when the cursor is fully present: the be-bv7x
// cursor-existence probe, then the content_hash column.
func expectHeadContentHashProbes(mock sqlmock.Sqlmock) {
expectCursorProbe(mock, "schema_migrations", true)
mock.ExpectQuery(`SHOW COLUMNS FROM schema_migrations LIKE 'content_hash'`).
WillReturnRows(showColumnsRows("content_hash"))
}
// TestReadMigrationContentHashesAtHeadIssuesNoFailingStatement is the be-bv7x
// rule for the HEAD read (bd-21dc1). `bd doctor` reaches it on a database it
// never migrates, where a SELECT against the absent cursor table, or against a
// pre-#4259 cursor without content_hash, fails and pins the pooled Dolt
// session to its pre-statement catalog. The strict mock admits only the shape
// probes, and the error must still read as nothing-to-compare to the callers.
func TestReadMigrationContentHashesAtHeadIssuesNoFailingStatement(t *testing.T) {
tests := []struct {
name string
expect func(sqlmock.Sqlmock)
}{
{
name: "no usable database",
expect: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
},
},
{
name: "cursor table absent",
expect: func(mock sqlmock.Sqlmock) {
expectCursorProbe(mock, "schema_migrations", false)
},
},
{
name: "content_hash column absent",
expect: func(mock sqlmock.Sqlmock) {
expectCursorProbe(mock, "schema_migrations", true)
mock.ExpectQuery(`SHOW COLUMNS FROM schema_migrations LIKE 'content_hash'`).
WillReturnRows(showColumnsRows())
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
tc.expect(mock)
_, err = ReadMigrationContentHashes(context.Background(), db, "")
if err == nil || !MissingMigrationObjectErr(err) {
t.Fatalf("ReadMigrationContentHashes(HEAD) error = %v, want a missing-object error", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
}
}
// TestReadMigrationContentHashesAtHeadReadsAPresentCursor covers the other
// half: once both probes find the shape, the HEAD read runs and still drops
// NULL and empty hashes.
func TestReadMigrationContentHashesAtHeadReadsAPresentCursor(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
expectHeadContentHashProbes(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}).
AddRow(1, "a").AddRow(2, nil).AddRow(3, ""))
got, err := ReadMigrationContentHashes(context.Background(), db, "")
if err != nil {
t.Fatalf("ReadMigrationContentHashes(HEAD): %v", err)
}
if want := map[int]string{1: "a"}; !maps.Equal(got, want) {
t.Errorf("ReadMigrationContentHashes(HEAD) = %v, want %v", got, want)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}

View file

@ -30,12 +30,13 @@ func hashRows(hashes map[int]string) *sqlmock.Rows {
return rows
}
// expectSmartRemoteRead mocks the smart router's local hash and branch reads,
// historical shape probes, and remote content hash read.
// expectSmartRemoteRead mocks the smart router's local shape probes and hash
// read, branch read, historical shape probes, and remote content hash read.
func expectSmartRemoteReadForRemote(mock sqlmock.Sqlmock, remoteName string, local, remote map[int]string) {
if remoteName == "" {
remoteName = smartGateDefaultRemote
}
expectHeadContentHashProbes(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(hashRows(local))
mock.ExpectQuery(`SELECT active_branch\(\)`).
@ -243,6 +244,7 @@ func TestSmartGateRouting(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
expectSmartFiringGate(mock, floor)
expectHeadContentHashProbes(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(hashRows(map[int]string{floor: "h"}))
mock.ExpectQuery(`SELECT active_branch\(\)`).
@ -272,6 +274,7 @@ func TestSmartGateRouting(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
expectSmartFiringGate(mock, floor)
expectHeadContentHashProbes(mock)
mock.ExpectQuery(`SELECT version, content_hash FROM schema_migrations$`).
WillReturnRows(sqlmock.NewRows([]string{"version", "content_hash"}))

View file

@ -35,6 +35,10 @@ func expectSmartGateDataBehindRoute(mock sqlmock.Sqlmock, ahead, behind int) {
}
ref := "remotes/origin/main"
// The local cursor's shape probes (be-bv7x) precede the HEAD read.
expectCursorProbe(mock, "schema_migrations", true)
mock.ExpectQuery(regexp.QuoteMeta("SHOW COLUMNS FROM schema_migrations LIKE 'content_hash'")).
WillReturnRows(sqlmock.NewRows([]string{"column"}).AddRow("content_hash"))
mock.ExpectQuery(regexp.QuoteMeta("SELECT version, content_hash FROM schema_migrations")).
WillReturnRows(hashes())
mock.ExpectQuery(regexp.QuoteMeta("SELECT active_branch()")).