feat(dprocedures): Add dolt_sql_commit procedure for atomic SQL execution + Dolt commit #10139
+157
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description

This PR introduces a new stored procedure dolt_sql_commit that executes a SQL statement and creates a Dolt commit in a single atomic transaction. The procedure ensures SQL execution and Dolt commit are tightly coupled—if either step fails, the entire operation rolls back, preventing partial changes.
Key capabilities:
Accepts a commit message (-m) and SQL statement as arguments
Executes SQL in a transaction (starts one if none exists)
Stages all table changes from the SQL execution
Creates a Dolt commit atomically with the SQL transaction
Rolls back entirely on any failure (SQL execution, staging, commit creation)
Motivation
Currently, users must execute SQL and call dolt_commit separately, which risks inconsistent states (e.g., SQL succeeds but commit fails, leaving uncommitted changes). This procedure solves the atomicity gap, making it safe to pair data modifications with Dolt versioning in a single operation—critical for workflows like ETL, automated updates, or any scenario requiring guaranteed consistency between SQL changes and Dolt commits.
Implementation Details
Parameter Parsing: Extracts commit message (-m flag) and SQL statement from arguments, validates required inputs.
Transaction Management:
Reuses existing transactions or starts a new read-write transaction.
Uses isolated contexts (context.Background()) to prevent external cancellations from breaking atomicity.
Implements a rollbackIfNeeded helper to clean up transactions on failure.
SQL Execution: Runs the input SQL via the Go-MySQL-Server engine, iterates and closes the result iterator properly.
Dolt Commit Workflow:
Loads current database roots and stages all tables (ensuring SQL changes are included).
Creates commit properties (message, author, timestamp) aligned with dolt_commit behavior.
Uses DoltCommit to atomically finalize both the SQL transaction and Dolt commit.
Testing
CALL dolt_sql_commit('-m', 'modify employees10',"INSERT INTO employees (
id,last_name,first_name) VALUES (10, 'Gavin', 'Gao')");Verified the row exists and a Dolt commit is created
Confirmed no partial changes when SQL/commit fails