Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: remove TiDB non-transaction statement handling for TiDB (#19738)

p0nyda0d29464097
diff --git a/backend/plugin/db/tidb/execute_testcontainer_test.go b/backend/plugin/db/tidb/execute_testcontainer_test.go
new file mode 100644
index 0000000000..adf97b3324
--- /dev/null
+++ b/backend/plugin/db/tidb/execute_testcontainer_test.go
@@ -0,0 +1,102 @@
+package tidb
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+)
+
+func openTestDriver(ctx context.Context, t *testing.T, container *testcontainer.Container) *Driver {
+ t.Helper()
+
+ driver := &Driver{}
+ d, err := driver.Open(ctx, storepb.Engine_TIDB, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "root",
+ Host: container.GetHost(),
+ Port: container.GetPort(),
+ },
+ ConnectionContext: db.ConnectionContext{},
+ })
+ require.NoError(t, err)
+
+ tidbDriver, ok := d.(*Driver)
+ require.True(t, ok)
+ return tidbDriver
+}
+
+func TestExecuteCreateIndexInTransaction(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestTiDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ tidbDriver := openTestDriver(ctx, t, container)
+ defer func() {
+ require.NoError(t, tidbDriver.Close(ctx))
+ }()
+
+ _, err := tidbDriver.Execute(ctx, `
+ CREATE DATABASE IF NOT EXISTS test;
+ USE test;
+ DROP TABLE IF EXISTS test.execute_create_index_in_transaction;
+ CREATE TABLE test.execute_create_index_in_transaction (id INT);
+ BEGIN;
+ CREATE INDEX idx_execute_create_index_in_transaction ON test.execute_create_index_in_transaction(id);
+ COMMIT;
+ `, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ var count int
+ query := `
+ SELECT COUNT(*)
+ FROM information_schema.tidb_indexes
+ WHERE table_schema = 'test'
+ AND table_name = 'execute_create_index_in_transaction'
+ AND key_name = 'idx_execute_create_index_in_transaction'
+ `
+ err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
+ require.NoError(t, err)
+ require.Equal(t, 1, count)
+}
+
+func TestExecutePreparedStatementFlowWithCreateIndexString(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestTiDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ tidbDriver := openTestDriver(ctx, t, container)
+ defer func() {
+ require.NoError(t, tidbDriver.Close(ctx))
+ }()
+
+ statement := `
+ CREATE DATABASE IF NOT EXISTS test;
+ USE test;
+ DROP TABLE IF EXISTS test.prepare_statement_flow;
+ CREATE TABLE test.prepare_statement_flow (id INT);
+ SET @sql := 'CREATE INDEX idx_prepare_statement_flow ON test.prepare_statement_flow(id)';
+ PREPARE stmt FROM @sql;
+ EXECUTE stmt;
+ DEALLOCATE PREPARE stmt;
+ `
+ _, err := tidbDriver.Execute(ctx, statement, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ var count int
+ query := `
+ SELECT COUNT(*)
+ FROM information_schema.tidb_indexes
+ WHERE table_schema = 'test'
+ AND table_name = 'prepare_statement_flow'
+ AND key_name = 'idx_prepare_statement_flow'
+ `
+ err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
+ require.NoError(t, err)
+ require.Equal(t, 1, count)
+}
diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.go
index 14d3bf5c2b..149e23b9cf 100644
--- a/backend/plugin/db/tidb/tidb.go
+++ b/backend/plugin/db/tidb/tidb.go
@@ -183,7 +183,6 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
}
slog.Debug("connectionID", slog.String("connectionID", connectionID))
- var nonTransactionStmts []base.Statement
var totalCommands int
var commands []base.Statement
oneshot := true
@@ -196,20 +195,10 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
if len(singleSQLs) == 0 {
return 0, nil
}
- totalCommands = len(singleSQLs)
+ commands = singleSQLs
+ totalCommands = len(commands)
if totalCommands <= common.MaximumCommands {
oneshot = false
- // Find non-transactional statements.
- // TiDB cannot run create table and create index in a single transaction.
- var remainingSQLs []base.Statement
- for _, singleSQL := range singleSQLs {
- if isNonTransactionStatement(singleSQL.Text) {
- nonTransactionStmts = append(nonTransactionStmts, singleSQL)
- continue
- }
- remainingSQLs = append(remainingSQLs, singleSQL)
- }
- commands = remainingSQLs
}
}
if oneshot {
@@ -222,13 +211,13 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
// Execute based on transaction mode
if transactionMode == common.TransactionModeOff {
- return d.executeInAutoCommitMode(ctx, conn, commands, nonTransactionStmts, opts, connectionID)
+ return d.executeInAutoCommitMode(ctx, conn, commands, opts, connectionID)
}
- return d.executeInTransactionMode(ctx, conn, commands, nonTransactionStmts, opts, connectionID)
+ return d.executeInTransactionMode(ctx, conn, commands, opts, connectionID)
}
// executeInTransactionMode executes statements within a single transaction
-func (d *Driver) executeInTransactionMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, nonTransactionStmts []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
+func (d *Driver) executeInTransactionMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
var totalRowsAffected int64
if err := conn.Raw(func(driverConn any) error {
@@ -299,33 +288,18 @@ func (d *Driver) executeInTransactionMode(ctx context.Context, conn *sql.Conn, c
return 0, err
}
- // Run non-transaction statements at the end.
- for _, stmt := range nonTransactionStmts {
- opts.LogCommandExecute(stmt.Range, stmt.Text)
- if _, err := d.db.ExecContext(ctx, stmt.Text); err != nil {
- opts.LogCommandResponse(0, []int64{0}, err.Error())
- return 0, err
- }
- opts.LogCommandResponse(0, []int64{0}, "")
- }
return totalRowsAffected, nil
}
// executeInAutoCommitMode executes statements sequentially in auto-commit mode
-func (d *Driver) executeInAutoCommitMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, nonTransactionStmts []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
+func (d *Driver) executeInAutoCommitMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
var totalRowsAffected int64
- // Execute all statements (including non-transactional ones)
- // In auto-commit mode, execute commands followed by non-transactional statements
- allCommands := make([]base.Statement, 0, len(commands)+len(nonTransactionStmts))
- allCommands = append(allCommands, commands...)
- allCommands = append(allCommands, nonTransactionStmts...)
-
if err := conn.Raw(func(driverConn any) error {
//nolint
exer := driverConn.(driver.ExecerContext)
- for _, command := range allCommands {
+ for _, command := range commands {
opts.LogCommandExecute(command.Range, command.Text)
sqlWithBytebaseAppComment := util.MySQLPrependBytebaseAppComment(command.Text)
@@ -364,16 +338,6 @@ func (d *Driver) executeInAutoCommitMode(ctx context.Context, conn *sql.Conn, co
return totalRowsAffected, nil
}
-var (
- // CREATE INDEX CONCURRENTLY cannot run inside a transaction block.
- // CREATE [ UNIQUE ] [ SPATIAL ] [ FULLTEXT ] INDEX ... ON table_name ...
- createIndexReg = regexp.MustCompile(`(?i)CREATE(\s+(UNIQUE\s+)?(SPATIAL\s+)?(FULLTEXT\s+)?)INDEX(\s+)`)
-)
-
-func isNonTransactionStatement(stmt string) bool {
- return len(createIndexReg.FindString(stmt)) > 0
-}
-
// QueryConn queries a SQL statement in a given connection.
func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {
singleSQLs, err := base.SplitMultiSQL(storepb.Engine_TIDB, statement)
diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.go
index 8d516ff370..0b7da6af78 100644
--- a/backend/plugin/db/tidb/tidb_test.go
+++ b/backend/plugin/db/tidb/tidb_test.go
@@ -51,36 +51,3 @@ func TestTiDBVersionAtLeast(t *testing.T) {
a.Equal(tc.want, got, "version=%s threshold=%s", tc.version, tc.threshold)
}
}
-
-func TestIsNonTransactionStatement(t *testing.T) {
- tests := []struct {
- stmt string
- want bool
- }{
- {
- `CREATE DATABASE "hello" ENCODING "UTF8";`,
- false,
- },
- {
- `CREATE table hello(id integer);`,
- false,
- },
- {
- `CREATE INDEX c1 ON t1 (c1);`,
- true,
- },
- {
- `CREATE UNIQUE INDEX c1 ON t1 (c1);`,
- true,
- },
- {
- `CREATE UNIQUE INDEX c1 ON t1 (c1) INVISIBLE;`,
- true,
- },
- }
-
- for _, test := range tests {
- got := isNonTransactionStatement(test.stmt)
- require.Equal(t, test.want, got)
- }
-}