Skip to content

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

chore: deprecate truncating (#6741)

Danny Xu515e86cb8745
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index cdb5ac96c1..7b8769b1e5 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -2,7 +2,6 @@
package mysql
import (
- "bytes"
"context"
"database/sql"
"fmt"
@@ -166,11 +165,6 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opt
return 0, err
}
}
- trunks, err := splitAndTransformDelimiter(statement)
- if err != nil {
- return 0, err
- }
-
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
return 0, errors.Wrapf(err, "failed to begin execute transaction")
@@ -178,18 +172,16 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opt
defer tx.Rollback()
var totalRowsAffected int64
- for _, trunk := range trunks {
- sqlResult, err := tx.ExecContext(ctx, trunk)
- if err != nil {
- return 0, errors.Wrapf(err, "failed to execute context in a transaction")
- }
- rowsAffected, err := sqlResult.RowsAffected()
- if err != nil {
- // Since we cannot differentiate DDL and DML yet, we have to ignore the error.
- log.Debug("rowsAffected returns error", zap.Error(err))
- }
- totalRowsAffected += rowsAffected
+ sqlResult, err := tx.ExecContext(ctx, statement)
+ if err != nil {
+ return 0, errors.Wrapf(err, "failed to execute context in a transaction")
+ }
+ rowsAffected, err := sqlResult.RowsAffected()
+ if err != nil {
+ // Since we cannot differentiate DDL and DML yet, we have to ignore the error.
+ log.Debug("rowsAffected returns error", zap.Error(err))
}
+ totalRowsAffected += rowsAffected
if err := tx.Commit(); err != nil {
return 0, errors.Wrapf(err, "failed to commit execute transaction")
@@ -227,34 +219,6 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
return util.Query(ctx, driver.dbType, conn, statement, queryContext)
}
-const querySize = 2 * 1024 * 1024 // 2M.
-
-// splitAndTransformDelimiter transform the delimiter to the MySQL default delimiter.
-func splitAndTransformDelimiter(statement string) ([]string, error) {
- var trunks []string
-
- var out bytes.Buffer
- statements, err := bbparser.SplitMultiSQLAndNormalize(bbparser.MySQL, statement)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to split SQL statements")
- }
- for _, singleSQL := range statements {
- stmt := singleSQL.Text
- if _, err = out.Write([]byte(stmt)); err != nil {
- return nil, errors.Wrapf(err, "failed to write SQL statement")
- }
-
- if out.Len() > querySize {
- trunks = append(trunks, out.String())
- out.Reset()
- }
- }
- if out.Len() > 0 {
- trunks = append(trunks, out.String())
- }
- return trunks, nil
-}
-
// QueryConn2 queries a SQL statement in a given connection.
func (driver *Driver) QueryConn2(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
singleSQLs, err := bbparser.SplitMultiSQL(bbparser.MySQL, statement)
diff --git a/backend/plugin/db/mysql/mysql_test.go b/backend/plugin/db/mysql/mysql_test.go
deleted file mode 100644
index 2e199ac638..0000000000
--- a/backend/plugin/db/mysql/mysql_test.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package mysql
-
-import (
- "bytes"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestTransformDelimiter(t *testing.T) {
- tests := []struct {
- statement string
- want string
- }{
- {
- statement: "CREATE TABLE t1(id INT PRIMARY KEY);",
- want: "CREATE TABLE t1(id INT PRIMARY KEY)\n;",
- },
- {
- statement: `
- DELIMITER ;;
- CREATE DEFINER=root@% TRIGGER upd_film AFTER UPDATE ON film FOR EACH ROW BEGIN
- IF (old.title != new.title) OR (old.description != new.description) OR (old.film_id != new.film_id)
- THEN
- UPDATE film_text
- SET title=new.title,
- description=new.description,
- film_id=new.film_id
- WHERE film_id=old.film_id;
- END IF;
- END ;;
- DELIMITER ;
- `,
- want: `CREATE DEFINER=root@% TRIGGER upd_film AFTER UPDATE ON film FOR EACH ROW BEGIN
- IF (old.title != new.title) OR (old.description != new.description) OR (old.film_id != new.film_id)
- THEN
- UPDATE film_text
- SET title=new.title,
- description=new.description,
- film_id=new.film_id
- WHERE film_id=old.film_id;
- END IF;
- END ;`,
- },
- {
- statement: `
- DELIMITER ;;
- CREATE DEFINER=root@% TRIGGER upd_film AFTER UPDATE ON film FOR EACH ROW BEGIN
- IF (old.title != new.title) OR (old.description != new.description) OR (old.film_id != new.film_id)
- THEN
- UPDATE film_text
- SET title=new.title,
- description=new.description,
- film_id=new.film_id
- WHERE film_id=old.film_id;
- END IF;
- END ;;
- DELIMITER ;
- CREATE TABLE t1(id INT PRIMAR KEY);
- DELIMITER ;;
- CREATE DEFINER=root@% TRIGGER del_film AFTER DELETE ON film FOR EACH ROW BEGIN
- DELETE FROM film_text WHERE film_id = old.film_id;
- END ;;
- DELIMITER ;
- `,
- want: `CREATE DEFINER=root@% TRIGGER upd_film AFTER UPDATE ON film FOR EACH ROW BEGIN
- IF (old.title != new.title) OR (old.description != new.description) OR (old.film_id != new.film_id)
- THEN
- UPDATE film_text
- SET title=new.title,
- description=new.description,
- film_id=new.film_id
- WHERE film_id=old.film_id;
- END IF;
- END ;CREATE TABLE t1(id INT PRIMAR KEY);CREATE DEFINER=root@% TRIGGER del_film AFTER DELETE ON film FOR EACH ROW BEGIN
- DELETE FROM film_text WHERE film_id = old.film_id;
- END ;`,
- },
- }
- a := require.New(t)
- for _, test := range tests {
- got, err := splitAndTransformDelimiter(test.statement)
- a.NoError(err)
- a.Len(got, 1)
- a.Equal(test.want, got[0])
- }
-}
-
-func TestTransformDelimiter_Truncate(t *testing.T) {
- var out bytes.Buffer
- for i := 0; i < 200000; i++ {
- _, err := out.WriteString("INSERT INTO hello VALUES (555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555);")
- require.NoError(t, err)
- }
- statements := out.String()
-
- got, err := splitAndTransformDelimiter(statements)
- require.NoError(t, err)
-
- total := 0
- for _, trunk := range got {
- total += len(trunk)
- }
- require.Equal(t, 12, len(got))
- // Make sure all trunks add up.
- // 12 trunks + 1 delimiter
- require.Equal(t, total, len(statements)+1)
-}