From e71d40578452425db33a17a70bb330157ad2b4fa Mon Sep 17 00:00:00 2001 From: Jim Wordelman Date: Tue, 18 Aug 2026 19:20:23 -0700 Subject: [PATCH] =?UTF-8?q?fix(schema):=20green=20=E2=80=94=20probe=20curs?= =?UTF-8?q?or=20table=20existence=20before=20selecting=20from=20it=20(refs?= =?UTF-8?q?=20be-bv7x)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- internal/storage/schema/schema.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/storage/schema/schema.go b/internal/storage/schema/schema.go index 42c631076..25cbff880 100644 --- a/internal/storage/schema/schema.go +++ b/internal/storage/schema/schema.go @@ -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(¤t) if err != nil && err != sql.ErrNoRows {