fix(schema): green — probe cursor table existence before selecting from it (refs be-bv7x)

A Dolt sql-server session that issues a failing statement stays pinned to
its pre-statement catalog snapshot. migrationSource.currentVersion ran a
bare SELECT against the cursor table before it necessarily existed, so on
a fresh database the pooled connection that hit the missing-table error
stayed poisoned for the rest of its life in the pool -- tables created by
migrations afterwards, on other connections, remained invisible to it.

Probe existence first with a query that always succeeds
(information_schema.tables), and only run the original SELECT once the
table is known to exist. The original error-handling path is unchanged
and still covers races.
This commit is contained in:
Jim Wordelman 2026-08-18 19:20:23 -07:00
commit e71d405784

View file

@ -1112,6 +1112,23 @@ func (m migrationSource) atLatest(ctx context.Context, db DBConn) bool {
}
func (m migrationSource) currentVersion(ctx context.Context, db DBConn) (int, error) {
// Probe existence with a query that always SUCCEEDS before ever issuing one
// that can fail. A Dolt session that issues a failing statement stays
// pinned to its pre-statement catalog snapshot, so a bare SELECT against a
// not-yet-created cursor table poisons the pooled connection: tables
// created afterwards on other connections stay invisible to this one for
// the rest of its life in the pool (be-bv7x).
var cursorExists int
if err := db.QueryRowContext(ctx,
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?",
m.cursorTable,
).Scan(&cursorExists); err != nil {
return 0, fmt.Errorf("probing %s existence: %w", m.cursorTable, err)
}
if cursorExists == 0 {
return 0, nil
}
var current int
err := db.QueryRowContext(ctx, "SELECT COALESCE(MAX(version), 0) FROM "+m.cursorTable).Scan(&current)
if err != nil && err != sql.ErrNoRows {