Skip to content

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

feat: add initial mysql transformer (#3042)

Danny Xu50fe37ff0745
diff --git a/plugin/parser/transform/mysql/transform.go b/plugin/parser/transform/mysql/transform.go
new file mode 100644
index 0000000000..99cf0a5a01
--- /dev/null
+++ b/plugin/parser/transform/mysql/transform.go
@@ -0,0 +1,88 @@
+// Package transform converts the raw schema dump to standard schema style.
+package transform
+
+import (
+ "bytes"
+
+ "github.com/pingcap/tidb/parser"
+ "github.com/pingcap/tidb/parser/ast"
+ "github.com/pingcap/tidb/parser/format"
+ "github.com/pingcap/tidb/parser/model"
+ "github.com/pkg/errors"
+
+ // Register pingcap parser driver.
+ _ "github.com/pingcap/tidb/types/parser_driver"
+)
+
+func transform(schema string) (string, error) {
+ nodes, _, err := parser.New().Parse(schema, "", "")
+ if err != nil {
+ return "", errors.Wrapf(err, "failed to parse schema %q", schema)
+ }
+ var newNodeList []ast.Node
+ for _, node := range nodes {
+ switch newStmt := node.(type) {
+ case *ast.CreateTableStmt:
+ var constraintList []*ast.Constraint
+ var indexList []*ast.CreateIndexStmt
+ for _, constraint := range newStmt.Constraints {
+ switch constraint.Tp {
+ case ast.ConstraintUniq:
+ // This becomes the unique index.
+ indexOption := constraint.Option
+ if indexOption == nil {
+ indexOption = &ast.IndexOption{}
+ }
+ indexList = append(indexList, &ast.CreateIndexStmt{
+ IndexName: constraint.Name,
+ Table: &ast.TableName{
+ Name: model.NewCIStr(newStmt.Table.Name.O),
+ },
+ IndexPartSpecifications: constraint.Keys,
+ IndexOption: indexOption,
+ KeyType: ast.IndexKeyTypeUnique,
+ })
+ case ast.ConstraintIndex:
+ // This becomes the index.
+ indexOption := constraint.Option
+ if indexOption == nil {
+ indexOption = &ast.IndexOption{}
+ }
+ indexList = append(indexList, &ast.CreateIndexStmt{
+ IndexName: constraint.Name,
+ Table: &ast.TableName{
+ Name: model.NewCIStr(newStmt.Table.Name.O),
+ },
+ IndexPartSpecifications: constraint.Keys,
+ IndexOption: indexOption,
+ KeyType: ast.IndexKeyTypeNone,
+ })
+ case ast.ConstraintPrimaryKey, ast.ConstraintKey, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintForeignKey, ast.ConstraintFulltext:
+ constraintList = append(constraintList, constraint)
+ }
+ }
+ newStmt.Constraints = constraintList
+ newNodeList = append(newNodeList, newStmt)
+ for _, node := range indexList {
+ newNodeList = append(newNodeList, node)
+ }
+ default:
+ newNodeList = append(newNodeList, node)
+ }
+ }
+
+ return deparse(newNodeList)
+}
+
+func deparse(newNodeList []ast.Node) (string, error) {
+ var buf bytes.Buffer
+ for _, node := range newNodeList {
+ if err := node.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutCharset, &buf)); err != nil {
+ return "", err
+ }
+ if _, err := buf.Write([]byte(";\n")); err != nil {
+ return "", err
+ }
+ }
+ return buf.String(), nil
+}
diff --git a/plugin/parser/transform/mysql/transform_test.go b/plugin/parser/transform/mysql/transform_test.go
new file mode 100644
index 0000000000..ca892f8843
--- /dev/null
+++ b/plugin/parser/transform/mysql/transform_test.go
@@ -0,0 +1,27 @@
+package transform
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestCreateTableSeparateIndex(t *testing.T) {
+ input := "CREATE TABLE `t1` (\n" +
+ " `id` int DEFAULT NULL,\n" +
+ " `c1` int DEFAULT NULL,\n" +
+ " `c2` int DEFAULT NULL,\n" +
+ " UNIQUE KEY `c1` (`c1`,`c2`),\n" +
+ " UNIQUE KEY `haha` (`c2`),\n" +
+ " KEY `t1` (`c1`)\n" +
+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
+ want := "CREATE TABLE `t1` (`id` INT DEFAULT NULL,`c1` INT DEFAULT NULL,`c2` INT DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_0900_AI_CI;\n" +
+ "CREATE UNIQUE INDEX `c1` ON `t1` (`c1`, `c2`);\n" +
+ "CREATE UNIQUE INDEX `haha` ON `t1` (`c2`);\n" +
+ "CREATE INDEX `t1` ON `t1` (`c1`);\n"
+
+ a := require.New(t)
+ got, err := transform(input)
+ a.NoError(err)
+ a.Equal(want, got)
+}