Skip to content

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

feat: support running DELIMITER commands in DDL issue (#3131)

h3n4ld0afbf75a88b
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index f6b1662b53..70d661255e 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -2,9 +2,11 @@
package mysql
import (
+ "bytes"
"context"
"database/sql"
"fmt"
+ "io"
"strings"
"github.com/go-sql-driver/mysql"
@@ -13,6 +15,7 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/util"
+ bbparser "github.com/bytebase/bytebase/plugin/parser"
)
var (
@@ -148,13 +151,18 @@ func (driver *Driver) getVersion(ctx context.Context) (string, error) {
// Execute executes a SQL statement.
func (driver *Driver) Execute(ctx context.Context, statement string) error {
+ var buf bytes.Buffer
+ if err := transformDelimiter(&buf, statement); err != nil {
+ return err
+ }
+ transformedStatement := buf.String()
tx, err := driver.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
- _, err = tx.ExecContext(ctx, statement)
+ _, err = tx.ExecContext(ctx, transformedStatement)
if err == nil {
if err := tx.Commit(); err != nil {
@@ -169,3 +177,30 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
func (driver *Driver) Query(ctx context.Context, statement string, limit int) ([]interface{}, error) {
return util.Query(ctx, driver.dbType, driver.db, statement, limit)
}
+
+// transformDelimiter transform the delimiter to the MySQL default delimiter.
+func transformDelimiter(out io.Writer, statement string) error {
+ statements, err := bbparser.SplitMultiSQL(bbparser.MySQL, statement)
+ if err != nil {
+ return errors.Wrapf(err, "failed to split SQL statements")
+ }
+ delimiter := `;`
+ for _, singleSQL := range statements {
+ stmt := singleSQL.Text
+ if bbparser.IsDelimiter(stmt) {
+ delimiter, err = bbparser.ExtractDelimiter(stmt)
+ if err != nil {
+ return errors.Wrapf(err, "failed to extract delimiter")
+ }
+ continue
+ }
+ if delimiter != ";" {
+ // Trim delimiter
+ stmt = fmt.Sprintf("%s;", stmt[:len(stmt)-len(delimiter)])
+ }
+ if _, err = out.Write([]byte(stmt)); err != nil {
+ return errors.Wrapf(err, "failed to write SQL statement")
+ }
+ }
+ return nil
+}
diff --git a/plugin/db/mysql/mysql_test.go b/plugin/db/mysql/mysql_test.go
new file mode 100644
index 0000000000..d83375b75d
--- /dev/null
+++ b/plugin/db/mysql/mysql_test.go
@@ -0,0 +1,87 @@
+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 PRIMAR KEY);",
+ want: "CREATE TABLE t1(id INT PRIMAR KEY);",
+ },
+ {
+ 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 {
+ var buf bytes.Buffer
+ err := transformDelimiter(&buf, test.statement)
+ a.NoError(err)
+ a.Equal(test.want, buf.String())
+ }
+}
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index 91ea204dbc..058403c5ef 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -359,7 +359,10 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
delete(oldUnsupportMap[tp], newName)
continue
}
- newNodeStmt = append(newNodeStmt, newStmt)
+ // Now, the input of differ comes from the our mysqldump, mysqldump use ;; to separate the CREATE TRIGGER/FUNCTION/PROCEDURE/EVENT statements;
+ // So we should append DELIMITER statement to the newStmt.
+ delimiterNewStmt := fmt.Sprintf("DELIMITER ;;\n%s\nDELIMITER ;\n", newStmt)
+ newNodeStmt = append(newNodeStmt, delimiterNewStmt)
}
}
// drop remaining TiDB unsupported objects
diff --git a/plugin/parser/utils.go b/plugin/parser/utils.go
index aa8cee970a..14f1dfc06b 100644
--- a/plugin/parser/utils.go
+++ b/plugin/parser/utils.go
@@ -139,3 +139,15 @@ func IsDelimiter(stmt string) bool {
re := regexp.MustCompile(delimiterRegex)
return re.MatchString(stmt)
}
+
+// ExtractDelimiter extracts the delimiter from the delimiter statement.
+func ExtractDelimiter(stmt string) (string, error) {
+ delimiterRegex := `(?i)^\s*DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`
+ re := regexp.MustCompile(delimiterRegex)
+ matchList := re.FindStringSubmatch(stmt)
+ index := re.SubexpIndex("DELIMITER")
+ if index >= 0 && index < len(matchList) {
+ return matchList[index], nil
+ }
+ return "", errors.Errorf("cannot extract delimiter from %q", stmt)
+}
diff --git a/plugin/parser/utils_test.go b/plugin/parser/utils_test.go
index e9567d2da1..08d6885b25 100644
--- a/plugin/parser/utils_test.go
+++ b/plugin/parser/utils_test.go
@@ -118,3 +118,57 @@ func TestIsTiDBUnsupportStmt(t *testing.T) {
a.Equalf(test.want, got, "statement: %s\n", test.stmt)
}
}
+
+func TestExtractDelimiter(t *testing.T) {
+ tests := []struct {
+ stmt string
+ want string
+ wantErr bool
+ }{
+ {
+ stmt: "DELIMITER ;;",
+ want: ";;",
+ wantErr: false,
+ },
+ {
+ stmt: "DELIMITER //",
+ want: "//",
+ wantErr: false,
+ },
+ {
+ stmt: "DELIMITER $$",
+ want: "$$",
+ wantErr: false,
+ },
+ {
+ stmt: "DELIMITER @@ ",
+ want: "@@",
+ wantErr: false,
+ },
+ {
+ stmt: "DELIMITER @@//",
+ want: "@@//",
+ wantErr: false,
+ },
+ {
+ stmt: "DELIMITER @@//",
+ want: "@@//",
+ wantErr: false,
+ },
+ // DELIMITER cannot contain a backslash character
+ {
+ stmt: "DELIMITER \\",
+ wantErr: true,
+ },
+ }
+ a := require.New(t)
+ for _, test := range tests {
+ got, err := ExtractDelimiter(test.stmt)
+ if test.wantErr {
+ a.Error(err)
+ } else {
+ a.NoError(err)
+ a.Equal(test.want, got)
+ }
+ }
+}