Skip to content

Warn users about lone dolt_checkout() in dolt sql -q#11012

Open
kevglynn wants to merge 1 commit into
dolthub:mainfrom
kevglynn:fix/warn-lone-dolt-checkout-6876
Open

Warn users about lone dolt_checkout() in dolt sql -q#11012
kevglynn wants to merge 1 commit into
dolthub:mainfrom
kevglynn:fix/warn-lone-dolt-checkout-6876

Conversation

@kevglynn

@kevglynn kevglynn commented May 7, 2026

Copy link
Copy Markdown

Summary

  • When dolt sql -q "call dolt_checkout('branch')" is run as a single statement, print a yellow warning to stderr explaining that the branch change only persists for the current session
  • Multi-statement queries (e.g. call dolt_checkout('branch'); select * from t) do NOT trigger the warning, since combining dolt_checkout() with other statements in a single invocation is valid usage
  • Suggests using dolt checkout <branch> instead for persistent branch changes

Changes

  • go/cmd/dolt/commands/sql.go: Added warnIfLoneDoltCheckout() and isLoneDoltCheckoutCall() — called from queryMode before executing the query
  • go/cmd/dolt/commands/sql_test.go: Added TestIsLoneDoltCheckoutCall with 11 test cases (single call, uppercase, mixed case, -b flag, different procedures, multi-statement, empty, bad syntax)
  • integration-tests/bats/sql-checkout.bats: Added 2 BATS integration tests — one confirming the warning appears for a lone checkout, one confirming it does NOT appear with multiple statements

Test plan

  • go test -run TestIsLoneDoltCheckoutCall ./cmd/dolt/commands/ — all 11 cases pass
  • Manual E2E verification: warning appears for lone dolt_checkout(), does not appear for other procedures or multi-statement input
  • CI: BATS sql-checkout suite

Closes #6876

@zachmu

zachmu commented Jun 10, 2026

Copy link
Copy Markdown
Member

Hi @kevglynn, thanks for the contribution. Sorry for the delay in response!

I like this idea, but the implementation requires parsing the query string an additional time. We already parse all queries in given to sql -q, so this warning logic should take advantage of that fact.

Please make that change and mention me when you have, and I'll take another look for merging.

@kevglynn kevglynn force-pushed the fix/warn-lone-dolt-checkout-6876 branch from 79644c6 to ab8061f Compare June 11, 2026 04:30
@kevglynn

Copy link
Copy Markdown
Author

@zachmu Thanks for the review! Good catch — I've refactored the implementation to eliminate the redundant parse.

What changed:

  • Removed isLoneDoltCheckoutCall() and warnIfLoneDoltCheckout() which called sqlparser.Parse() separately
  • Added a batchExecInfo struct that execBatchMode populates as it processes each already-parsed statement
  • execBatchMode now accepts an optional *batchExecInfo parameter — when non-nil, it tracks statement count and whether any statement is a CALL dolt_checkout(...) using isDoltCheckoutCall() on the sqlparser.Statement that ParseWithOptions already produced
  • After the loop, if stmtCount != 1, isLoneDoltCheckout is cleared — so multi-statement input never triggers the warning
  • queryMode (the -q entry point) passes a &batchExecInfo and checks the result after execution
  • Other callers (Exec batch path, debug.go) pass nil — zero behavior change for them

No extra parse. Same observable behavior. Unit tests and BATS tests still pass.

@kevglynn kevglynn force-pushed the fix/warn-lone-dolt-checkout-6876 branch from ab8061f to d5ea106 Compare June 16, 2026 21:17
Running `dolt sql -q "call dolt_checkout('branch')"` is a common
source of confusion because the branch change only persists for
the session that exits immediately after execution.

Detect when `dolt sql -q` is invoked with a single CALL
dolt_checkout() statement and print a warning to stderr suggesting
`dolt checkout <branch>` instead. Multi-statement queries are not
warned about since combining dolt_checkout() with other statements
in a single invocation is valid usage.

The detection reuses the already-parsed sqlparser.Statement from
execBatchMode via a batchExecInfo struct, avoiding a redundant
parse of the query string.

Closes dolthub#6876

Co-authored-by: Cursor <cursoragent@cursor.com>
@kevglynn kevglynn force-pushed the fix/warn-lone-dolt-checkout-6876 branch from d5ea106 to f945af0 Compare June 29, 2026 14:27
@kevglynn

Copy link
Copy Markdown
Author

@zachmu Rebased onto latest main — the refactored implementation (using batchExecInfo to piggyback on the existing parse in execBatchMode, no redundant parse) is ready for another look whenever you have a chance. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn users about running dolt sql -q "call dolt_checkout(...);"

3 participants